Skip to content
This repository has been archived by the owner on Dec 7, 2020. It is now read-only.

Commit

Permalink
Merge 6a2c042 into 722a0e3
Browse files Browse the repository at this point in the history
  • Loading branch information
belak committed Jul 5, 2016
2 parents 722a0e3 + 6a2c042 commit 13653ee
Showing 1 changed file with 54 additions and 17 deletions.
71 changes: 54 additions & 17 deletions mopidy_gmusic/library.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def __init__(self, *args, **kwargs):
self.tracks = {}
self.albums = {}
self.artists = {}
self.aa_artists = {}
self.aa_artists = LRUCache(1024)
self.aa_tracks = LRUCache(1024)
self.aa_albums = LRUCache(1024)
self.all_access = False
Expand All @@ -47,6 +47,35 @@ def __init__(self, *args, **kwargs):
def set_all_access(self, all_access):
self.all_access = all_access

def cache_track(self, track):
"""Cache a track and related information.
Arguments:
track -- a mopidy track to cache
"""
self.aa_tracks[track.uri] = track
self.cache_album(track.album)
for artist in track.artists:
self.cache_artist(artist)

def cache_album(self, album):
"""Cache an album and related information.
Arguments:
album -- a mopidy album to cache
"""
self.aa_albums[album.uri] = album
for artist in album.artists:
self.cache_artist(artist)

def cache_artist(self, artist):
"""Cache an artist and related information.
Arguments:
artist -- a mopidy artist to cache
"""
self.aa_artists[artist.uri] = artist

def _browse_albums(self):
refs = []
for album in self.albums.values():
Expand Down Expand Up @@ -286,7 +315,6 @@ def refresh(self, uri=None):
self.tracks = {}
self.albums = {}
self.artists = {}
self.aa_artists = {}
for song in self.backend.session.get_all_songs():
self._to_mopidy_track(song)

Expand Down Expand Up @@ -459,7 +487,6 @@ def _to_mopidy_album(self, song):
# (Difference being that non aa albums don't have albumId)
try:
album = self._aa_to_mopidy_album(song)
return album
except KeyError:
name = song.get('album', '')
artist = self._to_mopidy_album_artist(song)
Expand All @@ -475,8 +502,8 @@ def _to_mopidy_album(self, song):
'totalDiscCount', song.get('discNumber', 1)),
date=date,
images=images)
self.albums[uri] = album
return album
self.albums[album.uri] = album
return album

def _to_mopidy_artist(self, song):
name = song.get('artist', '')
Expand Down Expand Up @@ -520,7 +547,7 @@ def _aa_to_mopidy_track(self, song):
date=album.date,
length=int(song['durationMillis']),
bitrate=320)
self.aa_tracks[uri] = track
self.cache_track(track)
return track

def _aa_to_mopidy_album(self, song):
Expand All @@ -529,37 +556,42 @@ def _aa_to_mopidy_album(self, song):
artist = self._aa_to_mopidy_album_artist(song)
date = unicode(song.get('year', 0))
images = self._get_images(song)
return Album(
album = Album(
uri=uri,
name=name,
artists=[artist],
date=date,
images=images)
self.cache_album(album)
return album

def _aa_to_mopidy_artist(self, song):
name = song.get('artist', '')
artist_id = self._create_id(name)
uri = 'gmusic:artist:' + artist_id
self.aa_artists[artist_id] = song['artistId'][0]
return Artist(
artist = Artist(
uri=uri,
name=name)
self.cache_artist(artist)
return artist

def _aa_to_mopidy_album_artist(self, song):
name = song.get('albumArtist', '')
if name.strip() == '':
name = song['artist']
uri = 'gmusic:artist:' + self._create_id(name)
return Artist(
artist = Artist(
uri=uri,
name=name)
self.cache_artist(artist)
return artist

def _aa_search_track_to_mopidy_track(self, search_track):
track = search_track['track']

aa_artist_id = self._create_id(track['artist'])
if 'artistId' in track:
self.aa_artists[aa_artist_id] = track['artistId'][0]
aa_artist_id = track['artistId'][0]
else:
logger.warning('No artistId for Track %r', track)

Expand All @@ -573,7 +605,7 @@ def _aa_search_track_to_mopidy_track(self, search_track):
artists=[artist],
date=unicode(track.get('year', 0)))

return Track(
track = Track(
uri='gmusic:track:' + track['storeId'],
name=track['title'],
artists=[artist],
Expand All @@ -584,26 +616,31 @@ def _aa_search_track_to_mopidy_track(self, search_track):
length=int(track['durationMillis']),
bitrate=320)

self.cache_track(track)
return track

def _aa_search_artist_to_mopidy_artist(self, search_artist):
artist = search_artist['artist']
artist_id = self._create_id(artist['name'])
uri = 'gmusic:artist:' + artist_id
self.aa_artists[artist_id] = artist['artistId']
return Artist(
uri = 'gmusic:artist:' + artist['artistId']
artist = Artist(
uri=uri,
name=artist['name'])
self.cache_artist(artist)
return artist

def _aa_search_album_to_mopidy_album(self, search_album):
album = search_album['album']
uri = 'gmusic:album:' + album['albumId']
name = album['name']
artist = self._aa_search_artist_album_to_mopidy_artist_album(album)
date = unicode(album.get('year', 0))
return Album(
album = Album(
uri=uri,
name=name,
artists=[artist],
date=date)
self.cache_album(album)
return album

def _aa_search_artist_album_to_mopidy_artist_album(self, album):
name = album.get('albumArtist', '')
Expand Down

0 comments on commit 13653ee

Please sign in to comment.