A thousand cuts..
[jamaendo] / jamaui / console.py
index 88622ea..56e795c 100644 (file)
@@ -1,3 +1,26 @@
+#!/usr/bin/env python
+#
+# This file is part of Jamaendo.
+# Copyright (c) 2010 Kristoffer Gronlund
+#
+# Jamaendo is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# Jamaendo is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Jamaendo.  If not, see <http://www.gnu.org/licenses/>.
+#
+# Player code heavily based on http://thpinfo.com/2008/panucci/:
+#  A resuming media player for Podcasts and Audiobooks
+#  Copyright (c) 2008-05-26 Thomas Perl <thpinfo.com>
+#  (based on http://pygstdocs.berlios.de/pygst-tutorial/seeking.html)
+#
 # console interface to jamaui/jamaendo
 
 # debugging hack - add . to path
@@ -6,98 +29,53 @@ local_module_dir = os.path.join(os.path.dirname(sys.argv[0]), '..')
 if os.path.isdir(local_module_dir):
     sys.path.append(local_module_dir)
 
-from jamaendo.api import LocalDB, Query, Queries, refresh_dump
-from jamaui.player import Player, Playlist
+import jamaendo
+from jamaui.player import Player, Playlist, the_player
 import time
 import gobject
 
 gobject.threads_init()
 
-class Refresher(object):
-    def __init__(self):
-        self.done = False
-        self.last_percent = 0
-        print "Preparing local database..."
-    def complete(self):
-        self.done = True
-    def progress(self, percent):
-        if percent - self.last_percent >= 5:
-            print "\r%d%%" % (percent),
-            self.last_percent = percent
-
-    def run(self):
-        refresh_dump(self.complete, self.progress, force=False)
-        while not self.done:
-            time.sleep(1)
+import pprint
 
+pp = pprint.PrettyPrinter(indent=4)
 
-def pprint(x):
-    import simplejson
-    print simplejson.dumps(x, sort_keys=True, indent=4)
+#pp.pprint(stuff)
 
 class Console(object):
     def run(self):
-        Refresher().run()
-
         query = sys.argv[1]
 
-        queries = ['today',
-                   'tracks_this_month',
-                   'artist',
-                   'album',
-                   'play_track',
-                   'play_album']
+        queries = ['albums_of_the_week', 'artists', 'albums']
         if query in queries:
             getattr(self, "query_"+query)()
         else:
             print "Valid queries: " + ", ".join(queries)
 
-    def query_today(self):
-        result = Queries.albums_today()
-        pprint(result)
-
-    def query_tracks_this_month(self):
-        result = Queries.tracks_this_month()
-        pprint(result)
-
-    def query_artist(self):
-        q = sys.argv[2]
-        db = LocalDB()
-        db.connect()
-        for artist in db.search_artists(q):
-            pprint(artist)
-
-    def query_album(self):
-        q = sys.argv[2]
-        db = LocalDB()
-        db.connect()
-        for album in db.search_albums(q):
-            print "%s: %s - %s" % (album['id'], album['artist'], album['name'])
-
-    def query_play_track(self):
-        trackid = int(sys.argv[2])
-        uri = Query.track_mp3(trackid)
-        playlist = Playlist([uri])
-        player = Player()
-        player.play(playlist)
-
-        while player.playing():
-            time.sleep(1)
-
-    def query_play_album(self):
-        albumid = int(sys.argv[2])
-        db = LocalDB()
-        db.connect()
-        album = None
-        for a in db.get_albums([albumid]):
-            album = a
-            break
-        if not album:
-            return
-        print "%s - %s" % (album['artist'], album['name'])
-
-        playlist = Playlist(album['tracks'])
-        player = Player()
+    def query_albums_of_the_week(self):
+        result = jamaendo.albums_of_the_week()
+        pp.pprint([(a.ID, a.name) for a in result])
+        for a in result:
+            self.play_album(a)
+
+    def query_artists(self):
+        result = jamaendo.search_artists(sys.argv[2])
+        pp.pprint([(a.ID, a.name) for a in result])
+        for a in result:
+            albums = jamaendo.get_albums(a.ID)
+            for album in albums:
+                print "Playing album: %s - %s" % (a.name, album.name)
+                self.play_album(album)
+
+    def query_albums(self):
+        result = jamaendo.search_albums(sys.argv[2])
+        pp.pprint([(a.ID, a.name) for a in result])
+        for a in result:
+            self.play_album(a)
+
+    def play_tracks(self, tracks):
+        playlist = Playlist(tracks)
+        player = the_player
         player.play(playlist)
 
         while player.playing():
@@ -106,5 +84,12 @@ class Console(object):
             except KeyboardInterrupt:
                 player.next()
 
+    def play_album(self, album):
+        if not album.tracks:
+            album.load()
+        print "%s - %s" % (album.artist_name, album.name)
+        if album.tracks:
+            self.play_tracks(album.tracks)
+
 if __name__=="__main__":
     main()