Skip to content

Commit

Permalink
Return track images via library get_images(), fixing mopidy#111
Browse files Browse the repository at this point in the history
Previously, track images were provided via album images. That is
obsolete in Mopidy 3, and was removed in e4efb79. This uses the
new LibraryProvider get_images() API to provide images.

The same SoundCloud API request used to lookup() a track also
provides the image URL. These requests are now cached via
_get_track_data().

Like in the previous implementation, user avatar is used if there
is no track image.
  • Loading branch information
dreamlayers committed Jan 12, 2020
1 parent 310ef8a commit 5320324
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
14 changes: 13 additions & 1 deletion mopidy_soundcloud/library.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import urllib.parse

from mopidy import backend, models
from mopidy.models import SearchResult, Track
from mopidy.models import SearchResult, Track, Image
from mopidy_soundcloud.soundcloud import safe_url

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -157,3 +157,15 @@ def lookup(self, uri):
except Exception as error:
logger.error(f"Failed to lookup {uri}: {error}")
return []

def get_images(self, uris):
images = {}
for uri in uris:
if uri.startswith("soundcloud:song/"):
track_id = self.backend.remote.parse_track_uri(uri)
imguri = self.backend.remote.get_track_image(track_id)
if imguri is not None:
image = Image(uri=imguri)
images[uri] = [image]

return images
18 changes: 17 additions & 1 deletion mopidy_soundcloud/soundcloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,19 @@ def get_tracks(self, user_id=None):
def get_track(self, track_id, streamable=False):
logger.debug("Getting info for track with ID {track_id}")
try:
return self.parse_track(self._get(f"tracks/{track_id}"), streamable)
return self.parse_track(self._get_track_data(track_id), streamable)
except Exception:
return None

def get_track_image(self, track_id):
try:
data = self._get_track_data(track_id)
image = data.get("artwork_url")
if image is None:
image = data.get("user", {}).get("avatar_url")
if image is not None:
image = image.replace("large", "t500x500")
return image
except Exception:
return None

Expand Down Expand Up @@ -291,6 +303,10 @@ def _get(self, url, limit=None):
logger.error(f"SoundCloud API request failed: {e}")
return {}

@cache()
def _get_track_data(self, track_id):
return self._get(f"tracks/{track_id}")

def sanitize_tracks(self, tracks):
return [t for t in tracks if t]

Expand Down

0 comments on commit 5320324

Please sign in to comment.