|
@@ -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)
|