Tons of fixes/tweaks/changes in general
[jamaendo] / jamaui / console.py
1 #!/usr/bin/env python
2 #
3 # This file is part of Jamaendo.
4 # Copyright (c) 2010 Kristoffer Gronlund
5 #
6 # Jamaendo is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # Jamaendo is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with Jamaendo.  If not, see <http://www.gnu.org/licenses/>.
18 #
19 # Player code heavily based on http://thpinfo.com/2008/panucci/:
20 #  A resuming media player for Podcasts and Audiobooks
21 #  Copyright (c) 2008-05-26 Thomas Perl <thpinfo.com>
22 #  (based on http://pygstdocs.berlios.de/pygst-tutorial/seeking.html)
23 #
24 # console interface to jamaui/jamaendo
25
26 # debugging hack - add . to path
27 import os, sys
28 local_module_dir = os.path.join(os.path.dirname(sys.argv[0]), '..')
29 if os.path.isdir(local_module_dir):
30     sys.path.append(local_module_dir)
31
32 import jamaendo
33 from jamaui.player import Player, Playlist, the_player
34 import time
35 import gobject
36
37 gobject.threads_init()
38
39 import pprint
40
41 pp = pprint.PrettyPrinter(indent=4)
42
43 #pp.pprint(stuff)
44
45 class Console(object):
46     def run(self):
47         query = sys.argv[1]
48
49         queries = ['albums_of_the_week', 'artists', 'albums']
50         if query in queries:
51             getattr(self, "query_"+query)()
52         else:
53             print "Valid queries: " + ", ".join(queries)
54
55     def query_albums_of_the_week(self):
56         result = jamaendo.albums_of_the_week()
57         pp.pprint([(a.ID, a.name) for a in result])
58         for a in result:
59             self.play_album(a)
60
61     def query_artists(self):
62         result = jamaendo.search_artists(sys.argv[2])
63         pp.pprint([(a.ID, a.name) for a in result])
64         for a in result:
65             albums = jamaendo.get_albums(a.ID)
66             for album in albums:
67                 print "Playing album: %s - %s" % (a.name, album.name)
68                 self.play_album(album)
69
70     def query_albums(self):
71         result = jamaendo.search_albums(sys.argv[2])
72         pp.pprint([(a.ID, a.name) for a in result])
73         for a in result:
74             self.play_album(a)
75
76     def play_tracks(self, tracks):
77         playlist = Playlist(tracks)
78         player = the_player
79         player.play(playlist)
80
81         while player.playing():
82             try:
83                 time.sleep(1)
84             except KeyboardInterrupt:
85                 player.next()
86
87     def play_album(self, album):
88         if not album.tracks:
89             album.load()
90         print "%s - %s" % (album.artist_name, album.name)
91         if album.tracks:
92             self.play_tracks(album.tracks)
93
94 if __name__=="__main__":
95     main()