Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unhandled KeyError exception in get_images for relinked tracks #171

Closed
kingosticks opened this issue Jan 14, 2018 · 2 comments
Closed

Unhandled KeyError exception in get_images for relinked tracks #171

kingosticks opened this issue Jan 14, 2018 · 2 comments
Assignees
Labels
C-bug Category: This is a bug
Milestone

Comments

@kingosticks
Copy link
Member

When a track is relinked, a different id is returned than what was used in the request.

Getting the images for "spotify:track:4nqN0p0FjfH39G3hxeuKad" contains the following in the response:

    "id" : "39S0DVDKeneEjsq4pV45PT",
    "linked_from" : {
      "external_urls" : {
        "spotify" : "https://open.spotify.com/track/4nqN0p0FjfH39G3hxeuKad"
      },
      "href" : "https://api.spotify.com/v1/tracks/4nqN0p0FjfH39G3hxeuKad",
      "id" : "4nqN0p0FjfH39G3hxeuKad",
      "type" : "track",
      "uri" : "spotify:track:4nqN0p0FjfH39G3hxeuKad"
    },

The relinked track has id "39S0DVDKeneEjsq4pV45PT" rather than "4nqN0p0FjfH39G3hxeuKad" and causes the following exception:

ERROR    2018-01-14 14:17:25,204 [11350:Core-15] mopidy.core.library
  SpotifyBackend backend caused an exception.
Traceback (most recent call last):
  File "/home/nick/Dev/mopidy-dev/mopidy/mopidy/core/library.py", line 19, in _backend_error_handling
    yield
  File "/home/nick/Dev/mopidy-dev/mopidy/mopidy/core/library.py", line 174, in get_images
    if future.get() is None:
  File "/home/nick/.virtualenvs/mopidy/local/lib/python2.7/site-packages/pykka/threading.py", line 52, in get
    compat.reraise(*self._data['exc_info'])
  File "/home/nick/.virtualenvs/mopidy/local/lib/python2.7/site-packages/pykka/compat.py", line 12, in reraise
    exec('raise tp, value, tb')
  File "/home/nick/.virtualenvs/mopidy/local/lib/python2.7/site-packages/pykka/actor.py", line 201, in _actor_loop
    response = self._handle_receive(message)
  File "/home/nick/.virtualenvs/mopidy/local/lib/python2.7/site-packages/pykka/actor.py", line 295, in _handle_receive
    return callee(*message['args'], **message['kwargs'])
  File "/home/nick/Dev/mopidy-dev/mopidy-spotify/mopidy_spotify/library.py", line 29, in get_images
    return images.get_images(self._backend._web_client, uris)
  File "/home/nick/Dev/mopidy-dev/mopidy-spotify/mopidy_spotify/images.py", line 39, in get_images
    result.update(_process_uris(web_client, uri_type, batch))
  File "/home/nick/Dev/mopidy-dev/mopidy-spotify/mopidy_spotify/images.py", line 75, in _process_uris
    uri = ids_to_uris[item['id']]

The above example track should demonstrate the problem regardless of where you are (i.e. all "markets") since it's apparently no longer available anywhere. The fix is probably just a case of using the id field from the linked_from section if it is defined.

@jodal jodal added the C-bug Category: This is a bug label Jan 15, 2018
@adamcik
Copy link
Member

adamcik commented Dec 19, 2019

Following seems to do the trick, can make a proper PR later today.

diff --git a/mopidy_spotify/images.py b/mopidy_spotify/images.py
index 9d8b90b..7bcfee2 100644
--- a/mopidy_spotify/images.py
+++ b/mopidy_spotify/images.py
@@ -77,7 +77,12 @@ def _process_uris(web_client, uri_type, uris):
     for item in data.get(uri_type + "s", []):
         if not item:
             continue
-        uri = ids_to_uris[item["id"]]
+
+        if "linked_from" in item:
+            uri = ids_to_uris[item["linked_from"]["id"]]
+        else:
+            uri = ids_to_uris[item["id"]]
+
         if uri["key"] not in _cache:
             if uri_type == "track":
                 album_key = _parse_uri(item["album"]["uri"])["key"]

@adamcik
Copy link
Member

adamcik commented Dec 19, 2019

Should probably also test with the track that is gone everywhere and not found.

adamcik added a commit that referenced this issue Dec 19, 2019
Follow linked-from to get id in image fetching (fixes #171)
@jodal jodal added this to the v4.0 milestone Dec 20, 2019
kingosticks pushed a commit to kingosticks/mopidy-spotify that referenced this issue Dec 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug
Projects
None yet
Development

No branches or pull requests

3 participants