Log In
New Account
  
Home My Page Project Cloud Code Snippets Project Openings MussOrgsky: Music Organizer for Maemo
Summary Forums Tracker Lists Tasks Docs News SCM Files
1 #!/usr/bin/env python2.5
2 import dbus
3 import os
5 TRACKER = 'org.freedesktop.Tracker'
6 TRACKER_OBJ = '/org/freedesktop/Tracker/Metadata'
7 TRACKER_SEARCH_OBJ = '/org/freedesktop/Tracker/Search'
9 RDF_NO_PROPX = """
10 <rdfq:Condition>
11   <rdfq:and>
12     <rdfq:equals>
13       <rdfq:Property name="%s" />
14       <rdf:String></rdf:String> 
15     </rdfq:equals>
16   </rdfq:and>
17 </rdfq:Condition>
18 """
20 RDF_NO_ARTIST = RDF_NO_PROPX % "Audio:Artist"
21 RDF_NO_ALBUM = RDF_NO_PROPX % "Audio:Album"
22 RDF_NO_TITLE = RDF_NO_PROPX % "Audio:Title"
24 RDF_ANY_MISSING_METADATA = """
25 <rdfq:Condition>
26   <rdfq:or>
27     <rdfq:equals>
28       <rdfq:Property name="Audio:Artist" />
29       <rdf:String></rdf:String> 
30     </rdfq:equals>
31     <rdfq:equals>
32       <rdfq:Property name="Audio:Title" />
33       <rdf:String></rdf:String> 
34     </rdfq:equals>
35     <rdfq:equals>
36       <rdfq:Property name="Audio:Album" />
37       <rdf:String></rdf:String> 
38     </rdfq:equals>
39   </rdfq:or>
40 </rdfq:Condition>
41 """
44 class TrackerBackend:
46     def __init__ (self):
47         print "Tracker backend up"
48         bus = dbus.SessionBus ()
49         self.tracker_metadata = bus.get_object (TRACKER, TRACKER_OBJ)
50         self.iface_metadata = dbus.Interface (self.tracker_metadata,
51                                               "org.freedesktop.Tracker.Metadata")
53         self.tracker_search = bus.get_object (TRACKER, TRACKER_SEARCH_OBJ)
54         self.iface_search = dbus.Interface (self.tracker_search,
55                                             "org.freedesktop.Tracker.Search")
56         
57     def count_songs_wo_artist (self):
58         return self.iface_metadata.GetCount ("Music", "*", RDF_NO_ARTIST)
60     def count_songs_wo_title (self):
61         return self.iface_metadata.GetCount ("Music", "*", RDF_NO_TITLE)
63     def count_songs_wo_album (self):
64         return self.iface_metadata.GetCount ("Music", "*", RDF_NO_ALBUM)
66     def __run_rdf_query (self, rdf_query):
67         results = self.iface_search.Query (-1, "Music",
68                                            ["Audio:Artist",
69                                             "Audio:Title",
70                                             "Audio:Album",
71                                             "File:Mime"],
72                                            "", [], rdf_query, False,
73                                            ["Audio:DateAdded"], True, 0, 32000)
74         return results
76     def get_all_broken_songs (self):
77         """
78         Return tuples with the following fields:
79         (uri, "Music", artist, title, album, mimetype)
80         """
81         return self.__run_rdf_query (RDF_ANY_MISSING_METADATA)
82     
83     def get_all_songs (self):
84         """
85         Return tuples with the following fields:
86         (uri, "Music", artist, title, album, mimetype)
87         """
88         return self.__run_rdf_query ("")
91     def get_list_of_known_albums (self):
92         return self.iface_metadata.GetUniqueValues ("Music",
93                                                     ["Audio:Album"],
94                                                     "", False, 0, 32000)
96     def get_list_of_known_artists (self):
97         return self.iface_metadata.GetUniqueValues ("Music",
98                                                     ["Audio:Artist"],
99                                                     "", False, 0, 32000)
101     def get_all_pairs_album_artist (self):
102         return self.iface_metadata.GetUniqueValuesWithAggregates ("Music",
103                                                                   ["Audio:Album"],
104                                                                   "",
105                                                                   ["CONCAT"],
106                                                                   ["Audio:Artist"],
107                                                                   False, 0, 32000)
109 # Test
110 if __name__ == "__main__":
112     import sys
113     from optparse import OptionParser
115     parser = OptionParser()
116     parser.add_option ("-n", "--numbers", dest="print_numbers",
117                        action="store_true", default=True,
118                        help="Print stats about broken files")
119     
120     parser.add_option ("-p", "--pairs", dest="pairs_artist_album",
121                        action="store_true", default=True,
122                        help="Print all pairs (album, artist)")
124     (options, args) = parser.parse_args ()
126     if (not options.print_numbers and not options.pairs_artist_album):
127         parser.print_help ()
128         sys.exit (-1)
130     tracker = TrackerBackend ()
131     if (options.print_numbers):
132         print tracker.count_songs_wo_artist (), "Songs without artist"
133         print tracker.count_songs_wo_title (), "Songs without title"
134         print tracker.count_songs_wo_album (), "Songs without album"
136     if (options.pairs_artist_album):
137         for (album, artist) in tracker.get_all_pairs_artist_album ():
138             print album,"-",artist
140     

Terms of Use    Privacy Policy    Contribution Guidelines    Feedback

Powered By GForge Collaborative Development Environment