Removed outdated scripts, numerous bugfixes to api
[jamaendo] / jamaui / console.py
1 # console interface to jamaui/jamaendo
2
3 # debugging hack - add . to path
4 import os, sys
5 local_module_dir = os.path.join(os.path.dirname(sys.argv[0]), '..')
6 if os.path.isdir(local_module_dir):
7     sys.path.append(local_module_dir)
8
9 import jamaendo
10 from jamaui.player import Player, Playlist
11 import time
12 import gobject
13
14 gobject.threads_init()
15
16 import pprint
17
18 pp = pprint.PrettyPrinter(indent=4)
19
20 #pp.pprint(stuff)
21
22 class Console(object):
23     def run(self):
24         query = sys.argv[1]
25
26         queries = ['albums_of_the_week', 'artists', 'albums']
27         if query in queries:
28             getattr(self, "query_"+query)()
29         else:
30             print "Valid queries: " + ", ".join(queries)
31
32     def query_albums_of_the_week(self):
33         result = jamaendo.albums_of_the_week()
34         pp.pprint([(a.ID, a.name) for a in result])
35         for a in result:
36             self.play_album(a)
37
38     def query_artists(self):
39         result = jamaendo.search_artists(sys.argv[2])
40         pp.pprint([(a.ID, a.name) for a in result])
41         for a in result:
42             albums = jamaendo.get_albums(a.ID)
43             for album in albums:
44                 print "Playing album: %s - %s" % (a.name, album.name)
45                 self.play_album(album)
46
47     def query_albums(self):
48         result = jamaendo.search_albums(sys.argv[2])
49         pp.pprint([(a.ID, a.name) for a in result])
50         for a in result:
51             self.play_album(a)
52
53     def play_tracks(self, tracks):
54         playlist = Playlist(tracks)
55         player = Player()
56         player.play(playlist)
57
58         while player.playing():
59             try:
60                 time.sleep(1)
61             except KeyboardInterrupt:
62                 player.next()
63
64     def play_album(self, album):
65         if not album.tracks:
66             album.load()
67         print "%s - %s" % (album.artist_name, album.name)
68         if album.tracks:
69             self.play_tracks(album.tracks)
70
71 if __name__=="__main__":
72     main()