Move the qml/* files to the src folder. Now it is the main version
[mussorgsky] / src / tracker_backend_dbus.py
1 import dbus
2 import os
3
4 TRACKER = "org.freedesktop.Tracker1"
5 TRACKER_OBJ = "/org/freedesktop/Tracker1/Resources"
6
7 # (album name, artist1|artist2|... , number of artists)
8 ALBUM_ARTISTS_COUNTER = """
9 SELECT nie:title (?album) nmm:artistName (?artist) COUNT (?artist)
10 WHERE
11     {
12      ?album a nmm:MusicAlbum ; 
13             nmm:albumArtist ?artist .
14     }
15 GROUP BY ?album
16 """
17
18 SONGS = """
19 SELECT ?u nmm:artistName(?artist) nmm:albumTitle(?album) WHERE 
20 {
21     ?u a nmm:MusicPiece ;
22       nmm:performer ?artist ;
23       nmm:musicAlbum ?album .
24 }
25 """
26
27 class TrackerBackendDBus:
28
29     def __init__ (self):
30         self.dbus = dbus.SessionBus ()
31         self.tracker = self.dbus.get_object (TRACKER, TRACKER_OBJ)
32         self.resources = dbus.Interface (self.tracker,
33                                          "org.freedesktop.Tracker1.Resources")
34
35     def get_all_albums (self):
36         results = self.resources.SparqlQuery (ALBUM_ARTISTS_COUNTER)
37         return self.__iter_results (results)
38
39     def get_all_songs (self):
40         results = self.resources.SparqlQuery (SONGS)
41         for uri, artist, album in results:
42             yield (uri, artist, album)
43
44     def __iter_results (self, results):
45         for (title, artist, counter) in results:
46             print "Executed the yield", counter
47             if int(counter) > 1:
48                 yield (unicode(title), "Various artists")
49             else:
50                 yield (unicode(title), unicode(artist))
51
52
53 if __name__ == "__main__":
54     b = TrackerBackendDBus ()
55     for pair in b.get_all_songs ():
56         print "Outter loop"
57         print pair
58     print "ok"
59