diff --git a/mopidy_soundcloud/library.py b/mopidy_soundcloud/library.py index 574200f..9c67739 100644 --- a/mopidy_soundcloud/library.py +++ b/mopidy_soundcloud/library.py @@ -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__) @@ -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 diff --git a/mopidy_soundcloud/soundcloud.py b/mopidy_soundcloud/soundcloud.py index 58f34dd..70e93bd 100644 --- a/mopidy_soundcloud/soundcloud.py +++ b/mopidy_soundcloud/soundcloud.py @@ -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 @@ -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]