|
@@ -22,63 +22,13 @@ import requests
|
|
|
|
|
|
import gi
|
|
|
gi.require_version('Gtk', '3.0')
|
|
|
-gi.require_version('Gst', '1.0')
|
|
|
-from gi.repository import Gtk, Gst, Gdk, GLib
|
|
|
-
|
|
|
-import musicbrainzngs
|
|
|
-musicbrainzngs.set_useragent('Libricia', '0.x')
|
|
|
+from gi.repository import Gtk, Gdk, GLib
|
|
|
|
|
|
import channels
|
|
|
+import library
|
|
|
import playback
|
|
|
import search
|
|
|
|
|
|
-JAMENDO_BASE_URL = 'https://api.jamendo.com/v3.0/tracks/?'
|
|
|
-
|
|
|
-JAMENDO_OPTIONS = [
|
|
|
- ('client_id', '8e4fe531'),
|
|
|
- ('format', 'jsonpretty'),
|
|
|
- ('ccnc', '0'),
|
|
|
- ('ccnd', '0'),
|
|
|
-]
|
|
|
-
|
|
|
-
|
|
|
-class Artist():
|
|
|
- def __init__(self, data):
|
|
|
- self.data = data
|
|
|
-
|
|
|
- def get_tags(self, count_threshold):
|
|
|
- tags = sorted(self.data['tag-list'],
|
|
|
- key=lambda k: k['count'], reverse=True)
|
|
|
- return [tag['name'] for tag in tags if int(tag['count']) > count_threshold]
|
|
|
-
|
|
|
-
|
|
|
-class Channel():
|
|
|
- def __init__(self, artist):
|
|
|
- self.tags = artist.get_tags(0)
|
|
|
- self.pool = set()
|
|
|
- self.get_tracks_similar_to_artist(artist, 25)
|
|
|
- self.playlist = []
|
|
|
-
|
|
|
- def get_tracks_similar_to_artist(self, artist, num_results):
|
|
|
- by_xartist = JAMENDO_OPTIONS + [
|
|
|
- ('limit', str(num_results)),
|
|
|
- ('xartist', artist.data['name']),
|
|
|
- ]
|
|
|
-
|
|
|
- by_tags = JAMENDO_OPTIONS + [
|
|
|
- ('limit', str(num_results)),
|
|
|
- ('fuzzytags', '+'.join(artist.get_tags(0))),
|
|
|
- ]
|
|
|
-
|
|
|
- search_results = (requests.get(JAMENDO_BASE_URL + '&'.join(['='.join(option) for option in by_xartist])).json()[
|
|
|
- 'results'] + requests.get(JAMENDO_BASE_URL + '&'.join(['='.join(option) for option in by_tags])).json()['results'])
|
|
|
-
|
|
|
- for result in search_results:
|
|
|
- self.pool.add(playback.Track.new_from_jamendo_data(result))
|
|
|
-
|
|
|
- def get_track(self):
|
|
|
- return random.sample(self.pool, 1)[0]
|
|
|
-
|
|
|
|
|
|
class App:
|
|
|
def __init__(self):
|
|
@@ -94,17 +44,26 @@ class App:
|
|
|
self.timestamp = self.builder.get_object("timestamp")
|
|
|
self.playliststore = Gtk.ListStore()
|
|
|
self.libraryliststore = self.builder.get_object("libraryliststore")
|
|
|
+ self.savebutton = self.builder.get_object("savebutton")
|
|
|
|
|
|
self.playback_bar_is_pressed = False
|
|
|
self.player = playback.Player()
|
|
|
GLib.timeout_add(250, self.update_gui)
|
|
|
self.search = search.Search()
|
|
|
+ self.permanent_library = library.Library.load()
|
|
|
+ self.temporary_library = library.Library.load()
|
|
|
+ self.temporary_library.populate_liststore(self.libraryliststore)
|
|
|
|
|
|
self.window.show_all()
|
|
|
|
|
|
def update_gui(self):
|
|
|
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:
|
|
|
+ self.savebutton.set_active(False)
|
|
|
+ else:
|
|
|
+ self.savebutton.set_active(True)
|
|
|
return True
|
|
|
|
|
|
def on_search_clicked(self, button):
|
|
@@ -121,16 +80,12 @@ class App:
|
|
|
if len(self.search.results) > 0:
|
|
|
self.searchpopup.popdown()
|
|
|
self.channel = channels.Channel(self.search.results[0])
|
|
|
- self.channel.library.populate_liststore(self.libraryliststore)
|
|
|
+ self.temporary_library.update(self.channel.pool)
|
|
|
+ self.temporary_library.populate_liststore(self.libraryliststore)
|
|
|
|
|
|
def on_search_row_activated(self, treeview, path, column):
|
|
|
self.searchpopup.popdown()
|
|
|
channel = Channel(self.search.results[path.get_indices()[0]])
|
|
|
- playlist = playback.Playlist(self.playliststore)
|
|
|
- for i in range(4):
|
|
|
- track = channel.get_track()
|
|
|
- playlist.append_track(track)
|
|
|
- self.player.set_playlist(playlist)
|
|
|
|
|
|
def on_play_pause(self, button):
|
|
|
if self.player.is_playing:
|
|
@@ -171,13 +126,26 @@ class App:
|
|
|
|
|
|
def on_track_activated(self, treeview, path, column):
|
|
|
treeiter = self.libraryliststore.get_iter(path)
|
|
|
- track_id, track_data = self.channel.library.get_track(
|
|
|
+ track_id, track_data, album_id, album_data, artist_id, artist_data = self.temporary_library.get_track(
|
|
|
self.libraryliststore.get_value(treeiter, 0))
|
|
|
if track_id is not None:
|
|
|
self.player.queue_track(track_id, track_data)
|
|
|
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:
|
|
|
+ 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)
|
|
|
+ else:
|
|
|
+ self.permanent_library.remove_track(track_id)
|
|
|
+ self.permanent_library.save()
|
|
|
+
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
App()
|