Goodbye to the old code
[mussorgsky] / src / qml / tracker_backend_gi.py
1 import gi
2 from gi.repository import Tracker
3
4 # (album name, artist1|artist2|... , number of artists)
5 ALBUM_ARTISTS_COUNTER = """
6 SELECT nie:title (?album) nmm:artistName (?artist) COUNT (?artist)
7 WHERE
8     {
9      ?album a nmm:MusicAlbum ; 
10             nmm:albumArtist ?artist .
11     }
12 GROUP BY ?album
13 """
14
15 class TrackerBackendGI:
16
17     def __init__ (self):
18         self.conn = Tracker.SparqlConnection.get (None)
19
20     def get_all_albums (self):
21         """
22         Generator returning (album_title, album_artist)
23         If the album has more than one artist, "Various artists"
24         """
25         cursor = self.conn.query (ALBUM_ARTISTS_COUNTER, None)
26         while cursor.next (None):
27             album_title = cursor.get_string (0)[0]
28             if cursor.get_integer(2) > 1:
29                 album_artist = "Various artists"
30             else:
31                 album_artist = cursor.get_string (1)[0]
32
33             yield (album_title, album_artist)
34             
35
36
37 if __name__ == "__main__":
38
39     tracker = TrackerBackendGI()
40     tracker.get_all_albums ()