3 Commity e30984d36f ... 52e0ae3d57

Autor SHA1 Wiadomość Data
  chaosmonk 52e0ae3d57 fix library, get channels working 5 lat temu
  chaosmonk bfb545ccd0 simplify library class 5 lat temu
  chaosmonk 33bf3a908e simplify library class 5 lat temu

+ 33 - 20
libricia_music/app.py

@@ -32,7 +32,8 @@ from .search import Search
 class App:
     def __init__(self):
         self.builder = Gtk.Builder()
-        self.builder.add_from_file(os.path.dirname(__file__) + "/data/libricia-music.glade")
+        self.builder.add_from_file(os.path.dirname(
+            __file__) + "/data/libricia-music.glade")
         self.builder.connect_signals(self)
 
         self.window = self.builder.get_object("window")
@@ -49,17 +50,31 @@ class App:
         self.player = Player()
         GLib.timeout_add(250, self.update_gui)
         self.search = Search()
-        self.permanent_library = Library.load()
-        self.temporary_library = Library.load()
-        self.temporary_library.populate_liststore(self.libraryliststore)
+
+        self.tracks_saved = Library()
+        self.tracks_saved.load()
+        self.tracks_playing = Library()
+        self.tracks_displayed = Library()
+        self.tracks_displayed.update(self.tracks_saved)
+        self.tracks_displayed.populate_liststore(self.libraryliststore)
 
         self.window.show_all()
 
     def update_gui(self):
+        now_playing = self.player.get_now_playing()
+
+        if now_playing is not None and now_playing['track_id'] not in self.tracks_playing:
+            self.tracks_playing = Library()
+            self.tracks_playing.add_track(now_playing)
+            self.tracks_displayed = Library()
+            self.tracks_displayed.update(self.tracks_saved)
+            self.tracks_displayed.update(self.tracks_playing)
+            self.libraryliststore.clear()
+            self.tracks_displayed.populate_liststore(self.libraryliststore)
+
         self.player.update_gui(self.libraryliststore, self.playback_bar,
                                self.playback_bar_is_pressed, self.timestamp, self.playpause_button)
-        now_playing = self.player.get_now_playing()
-        if now_playing is None or self.permanent_library.get_track(now_playing) is None:
+        if now_playing is None or now_playing['track_id'] not in self.tracks_saved:
             self.savebutton.set_active(False)
         else:
             self.savebutton.set_active(True)
@@ -78,9 +93,9 @@ class App:
     def on_search_activate(self, searchentry):
         if len(self.search.results) > 0:
             self.searchpopup.popdown()
-            self.channel = Channel(self.search.results[0])
-            self.temporary_library.update(self.channel.pool)
-            self.temporary_library.populate_liststore(self.libraryliststore)
+            self.player.channel = Channel(self.search.results[0])
+            self.player.request_track()
+            self.player.start_track(0)
 
     def on_search_row_activated(self, treeview, path, column):
         self.searchpopup.popdown()
@@ -125,29 +140,27 @@ class App:
 
     def on_track_activated(self, treeview, path, column):
         treeiter = self.libraryliststore.get_iter(path)
-        track_id, track_data, album_id, album_data, artist_id, artist_data = self.temporary_library.get_track(
+        track = self.tracks_displayed.get_track(
             self.libraryliststore.get_value(treeiter, 0))
-        if track_id is not None:
-            self.player.queue_track(track_id, track_data)
+        if track is not None:
+            self.player.queue_track(track)
             self.player.start_track(len(self.player.queue) - 1)
         self.update_gui()
 
     def on_save_button_toggled(self, button):
-        track_id = self.player.queue[self.player.index]['id']
-        if track_id is None:
+        now_playing = self.player.get_now_playing()
+        if now_playing is None:
             button.set_active(False)
         elif button.get_active():
-            track_id, track_data, album_id, album_data, artist_id, artist_data = self.temporary_library.get_track(
-                track_id)
-            self.permanent_library.add_track(
-                artist_id, artist_data, album_id, album_data, track_id, track_data)
+            self.tracks_saved.add_track(now_playing)
         else:
-            self.permanent_library.remove_track(track_id)
-        self.permanent_library.save()
+            self.tracks_saved.remove_track(now_playing['track_id'])
+        self.tracks_saved.save()
 
     def on_window_destroy(self, widget):
         Gtk.main_quit()
 
+
 def main():
     App()
     Gtk.main()

+ 6 - 1
libricia_music/channels.py

@@ -15,6 +15,7 @@
 #    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 #
 
+import random
 import requests
 
 from .library import Library
@@ -42,7 +43,7 @@ class Channel:
                 for tag in musicbrainz_artist_data['tag-list'] if int(tag['count']) > 0]
 
         tags_options = options + [
-            ('limit', '25'),
+            ('limit', 'all'),
             ('fuzzytags', '+'.join(tags)),
         ]
 
@@ -51,3 +52,7 @@ class Channel:
 
         for result in requests.get(url + '&'.join(['='.join(option) for option in tags_options])).json()['results']:
             self.pool.add_jamendo_track(result)
+
+    def get_random_track(self):
+        track_id = random.choice(list(self.pool))
+        return self.pool.get_track(track_id)

+ 40 - 75
libricia_music/library.py

@@ -15,6 +15,7 @@
 #    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 #
 
+import os
 import yaml
 
 import gi
@@ -26,89 +27,53 @@ from .settings import CONFIG_DIR
 
 
 class Library(dict):
-    def __init__(self):
-        self['artists'] = {}
-
-    @classmethod
-    def load(cls):
-        library = cls()
-        try:
-            with open(CONFIG_DIR + '/library.yaml', 'r') as f:
-                library.update(yaml.safe_load(f))
-            return library
-        except:
-            return library
-
     def save(self):
         with open(CONFIG_DIR + '/library.yaml', 'w') as f:
             yaml.dump(dict(self), f)
 
-    def add_artist(self, artist_id, artist_data):
-        if artist_id not in self['artists']:
-            self['artists'][artist_id] = artist_data
-            self['artists'][artist_id]['albums'] = {}
-
-    def add_album(self, artist_id, artist_data, album_id, album_data):
-        self.add_artist(artist_id, artist_data)
-        if album_id not in self['artists'][artist_id]['albums']:
-            self['artists'][artist_id]['albums'][album_id] = album_data
-            self['artists'][artist_id]['albums'][album_id]['tracks'] = {}
-
-    def add_track(self, artist_id, artist_data, album_id, album_data, track_id, track_data):
-        self.add_album(artist_id, artist_data, album_id, album_data)
-        if track_id not in self['artists'][artist_id]['albums'][album_id]['tracks']:
-            self['artists'][artist_id]['albums'][album_id]['tracks'][track_id] = track_data
+    def load(self):
+        if os.path.isfile(CONFIG_DIR + '/library.yaml'):
+            with open(CONFIG_DIR + '/library.yaml', 'r') as f:
+                self.update(yaml.safe_load(f))
 
-    def remove_track(self, ID):
-        for artist_id, artist_data in self['artists'].items():
-            for album_id, album_data in artist_data['albums'].items():
-                for track_id, track_data in album_data['tracks'].items():
-                    if track_id == ID:
-                        return self['artists'][artist_id]['albums'][album_id]['tracks'].pop(track_id)
+    def add_track(self, track):
+        self.update({track['track_id']: track})
 
     def add_jamendo_track(self, data):
-        artist_id = 'jamendo:' + data['artist_id']
-        artist_data = {
-            'name': data['artist_name'],
+        track = {
+            'track_id': 'jamendo:' + data['id'],
+            'track_name': data['name'],
+            'track_duration': data['duration'],
+            'track_position': data['position'],
+            'artist_id': 'jamendo:' + data['artist_id'],
+            'artist_name': data['artist_name'],
+            'album_id': 'jamendo:' + data['album_id'],
+            'album_name': data['album_name'],
+            'album_date': data['releasedate'],
+            'album_image': data['album_image'],
         }
-        album_id = 'jamendo:' + data['album_id']
-        album_data = {
-            'name': data['album_name'],
-            'date': data['releasedate'],
-            'image': data['album_image'],
-        }
-        track_id = 'jamendo:' + data['id']
-        track_data = {
-            'name': data['name'],
-            'duration': data['duration'],
-            'position': data['position'],
-        }
-        self.add_track(artist_id, artist_data, album_id,
-                       album_data, track_id, track_data)
+        self.add_track(track)
 
-    def get_track(self, ID):
-        for artist_id, artist_data in self['artists'].items():
-            for album_id, album_data in artist_data['albums'].items():
-                for track_id, track_data in album_data['tracks'].items():
-                    if track_id == ID:
-                        return track_id, track_data, album_id, album_data, artist_id, artist_data
+    def get_track(self, track_id):
+        if track_id in self:
+            return self[track_id]
 
-    def populate_liststore(self, liststore):
-        for artist_id, artist_data in self['artists'].items():
-            for album_id, album_data in artist_data['albums'].items():
-                for track_id, track_data in album_data['tracks'].items():
-                    icon = None
-                    track = track_data['position']
-                    title = track_data['name']
-                    artist = artist_data['name']
-                    album = album_data['name']
-                    length = seconds_to_timestamp(
-                        [track_data['duration']])[0]
-                    date = album_data['date']
+    def remove_track(self, track_id):
+        return self.pop(track_id, False)
 
-                    liststore.append(
-                        [track_id, icon, track, title, artist, album, length, date])
-
-                    liststore.set_sort_column_id(2, Gtk.SortType.ASCENDING)
-                    liststore.set_sort_column_id(7, Gtk.SortType.ASCENDING)
-                    liststore.set_sort_column_id(4, Gtk.SortType.ASCENDING)
+    def populate_liststore(self, liststore):
+        for track_id, track_data in self.items():
+            icon = None
+            track = track_data['track_position']
+            title = track_data['track_name']
+            artist = track_data['artist_name']
+            album = track_data['album_name']
+            length = seconds_to_timestamp([track_data['track_duration']])[0]
+            date = track_data['album_date']
+
+            liststore.append([track_id, icon, track, title,
+                              artist, album, length, date])
+
+            liststore.set_sort_column_id(2, Gtk.SortType.ASCENDING)
+            liststore.set_sort_column_id(7, Gtk.SortType.ASCENDING)
+            liststore.set_sort_column_id(4, Gtk.SortType.ASCENDING)

+ 15 - 7
libricia_music/playback.py

@@ -34,6 +34,8 @@ class Player:
         self.queue = []
         self.index = None
 
+        self.channel = None
+
         self.is_playing = False
         self.position_changed = False
         self.position = 0
@@ -57,7 +59,9 @@ class Player:
         if len(self.queue) > 0:
             if index < 0:
                 index = 0
-            elif index > len(self.queue) - 1:
+            if index == len(self.queue):
+                self.request_track()
+            if index > len(self.queue) - 1:
                 index = len(self.queue) - 1
             if self.index != index:
                 self.index = index
@@ -71,10 +75,10 @@ class Player:
             self.set_index(index)
             self.play()
 
-    def queue_track(self, track_id, track_data):
+    def queue_track(self, track):
         uri = 'http://mp3l.jamendo.com/?trackid=' + \
-            track_id.split(':')[1] + '&format=mp31'
-        self.queue.append({'id': track_id, 'uri': uri})
+            track['track_id'].split(':')[1] + '&format=mp31'
+        self.queue.append({'track': track, 'uri': uri})
 
     def play(self):
         if self.is_ready():
@@ -101,7 +105,7 @@ class Player:
 
     def get_now_playing(self):
         if self.is_ready():
-            return self.queue[self.index]['id']
+            return self.queue[self.index]['track']
 
     def update_gui(self, liststore, playback_bar, playback_bar_is_pressed, playback_timestamp, playpause_button):
         if self.is_ready():
@@ -109,8 +113,8 @@ class Player:
             # update playback marker
             now_playing = self.get_now_playing()
 
-            def update_icon(model, path, itr, data):
-                if model.get_value(itr, 0) == data:
+            def update_icon(model, path, itr, track):
+                if model.get_value(itr, 0) == track['track_id']:
                     if self.is_playing:
                         model.set_value(itr, 1, 'gtk-media-play')
                     else:
@@ -171,5 +175,9 @@ class Player:
                         Gst.Format.TIME, Gst.SeekFlags.FLUSH | Gst.SeekFlags.KEY_UNIT, self.position)
                 self.position_changed = False
 
+    def request_track(self):
+        if self.channel is not None:
+            self.queue_track(self.channel.get_random_track())
+
     def on_about_to_finish(self, data):
         self.set_index(self.index + 1)