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

Commit

Permalink
Merge 7007be0 into d3b307e
Browse files Browse the repository at this point in the history
  • Loading branch information
jason-a69 committed Nov 10, 2017
2 parents d3b307e + 7007be0 commit bb112ac
Showing 1 changed file with 56 additions and 14 deletions.
70 changes: 56 additions & 14 deletions mopidy_gmusic/library.py
Expand Up @@ -23,7 +23,9 @@ def __init__(self, *args, **kwargs):
# in our library.
self.tracks = {}
self.albums = {}
self.albums_ungrouped = {}
self.artists = {}
self.artists_ungrouped = {}

# aa_* caches are *only* used for temporary objects. Library
# objects will never make it here.
Expand Down Expand Up @@ -55,10 +57,10 @@ def all_access(self):

def _browse_tracks(self):
tracks = list(self.tracks.values())
tracks.sort(key=lambda ref: ref.name)
refs = []
tracks = sorted(tracks, key=lambda t: (t.name))
for track in tracks:
refs.append(track_to_ref(track))
refs.append(track_to_ref(track, False))
return refs

def _browse_albums(self):
Expand All @@ -71,7 +73,7 @@ def _browse_albums(self):
def _browse_album(self, uri):
refs = []
for track in self._lookup_album(uri):
refs.append(track_to_ref(track, True))
refs.append(track_to_ref(track, False))
return refs

def _browse_artists(self):
Expand All @@ -96,7 +98,7 @@ def _browse_artist(self, uri):
def _browse_artist_all_tracks(self, uri):
artist_uri = ':'.join(uri.split(':')[:3])
refs = []
tracks = self._lookup_artist(artist_uri, True)
tracks = self._lookup_artist(artist_uri, False)
for track in tracks:
refs.append(track_to_ref(track))
return refs
Expand Down Expand Up @@ -233,7 +235,7 @@ def _lookup_album(self, uri):
# up here (as a fallback) because purchased tracks can have a
# store ID, but only show up in your library.
try:
album = self.albums[uri]
album = self.albums_ungrouped[uri]
except KeyError:
logger.debug('Failed to lookup %r', uri)
return []
Expand Down Expand Up @@ -297,7 +299,7 @@ def sorter(track):
except KeyError:
pass
try:
artist = self.artists[uri]
artist = self.artists_ungrouped[uri]
except KeyError:
logger.debug('Failed to lookup %r', uri)
return []
Expand All @@ -311,14 +313,27 @@ def sorter(track):
def refresh(self, uri=None):
self.tracks = {}
self.albums = {}
self.albums_ungrouped = {}
self.artists = {}
self.artists_ungrouped = {}

album_tracks = {}
for track in self.backend.session.get_all_songs():
mopidy_track = self._to_mopidy_track(track)

self.tracks[mopidy_track.uri] = mopidy_track
self.albums[mopidy_track.album.uri] = mopidy_track.album
# Need another albums list that will store all albumIds so they can
# be referenced in the browsing later. Problem caused by Google
# assigning multiple albumIds for the same album
self.albums_ungrouped[mopidy_track.album.uri] = mopidy_track.album
album_found = False
current_artist_name = track.get('albumArtist', '')
for album_chk in self.albums.values():
chk_artist_name = [artist.name for artist in album_chk.artists]
if album_chk.name == mopidy_track.album.name and \
current_artist_name == chk_artist_name[0]:
album_found = True
if not album_found:
self.albums[mopidy_track.album.uri] = mopidy_track.album

# We don't care about the order because we're just using
# this as a temporary variable to grab the proper album
Expand All @@ -330,18 +345,39 @@ def refresh(self, uri=None):

# Yes, this is awful. No, I don't have a better solution. Yes,
# I'm annoyed at Google for not providing album artist IDs.

for album in self.albums.values():
artist_found = False
for album_artist in album.artists:
for track in album_tracks[album.uri]:
for artist in track.artists:
if album_artist.name == artist.name:
artist_found = True
self.artists[artist.uri] = artist

# Check to see if the artist exists in the
# self.artist list already, if it does, do not add it
# again. Problem is caused by Google assigning
# multiple artistIDs to the same artist name.
# Duplicates are stored in self.artists_ungrouped

artist_found_2 = False
for artist_chk in self.artists.values():
if artist_chk.name == artist.name:
artist_found_2 = True
if not artist_found_2:
self.artists[artist.uri] = artist

self.artists_ungrouped[artist.uri] = artist

if not artist_found:
for artist in album.artists:
self.artists[artist.uri] = artist
artist_found_2 = False
for artist_chk in self.artists.values():
if artist_chk.name == artist.name:
artist_found_2 = True
if not artist_found_2:
self.artists[artist.uri] = artist
self.artists_ungrouped[artist.uri] = artist

def search(self, query=None, uris=None, exact=False):
if exact:
Expand Down Expand Up @@ -432,14 +468,20 @@ def track_name_filter(track):
def album_filter(track):
return q in getattr(track, 'album', Album()).name.lower()

# Reason we have to use startswith in search is because if you
# filter by artist Air for example, you could get Jefferson
# Airplane back if you search any part of the string.
# Same if you seach for Abba, you could get Black Sabbath back.
# This is based on experience :)
def artist_filter(track):
return (
any(q in a.name.lower() for a in track.artists) or
albumartist_filter(track))
return (any(a.name.lower().startswith(q)
for a in track.artists)
or albumartist_filter(track))

def albumartist_filter(track):
album_artists = getattr(track, 'album', Album()).artists
return any(q in a.name.lower() for a in album_artists)
return (any(a.name.lower().startswith(q)
for a in album_artists))

def track_no_filter(track):
return track.track_no == q
Expand Down

0 comments on commit bb112ac

Please sign in to comment.