|
1 | 1 | import logging
|
2 | 2 |
|
3 | 3 | import spotify
|
4 |
| -from mopidy_spotify import playlists, translator, utils, web |
| 4 | +from mopidy_spotify import browse, playlists, translator, utils |
| 5 | +from mopidy_spotify.web import LinkType, WebLink |
5 | 6 |
|
6 | 7 | logger = logging.getLogger(__name__)
|
7 | 8 |
|
|
12 | 13 |
|
13 | 14 | def lookup(config, session, web_client, uri):
|
14 | 15 | try:
|
15 |
| - web_link = web.WebLink.from_uri(uri) |
16 |
| - if web_link.type != web.LinkType.PLAYLIST: |
| 16 | + web_link = WebLink.from_uri(uri) |
| 17 | + if web_link.type not in (LinkType.PLAYLIST, LinkType.YOUR): |
17 | 18 | sp_link = session.get_link(uri)
|
18 | 19 | except ValueError as exc:
|
19 | 20 | logger.info(f"Failed to lookup {uri!r}: {exc}")
|
20 | 21 | return []
|
21 | 22 |
|
22 | 23 | try:
|
23 |
| - if web_link.type == web.LinkType.PLAYLIST: |
| 24 | + if web_link.type == LinkType.PLAYLIST: |
24 | 25 | return _lookup_playlist(config, session, web_client, uri)
|
| 26 | + elif web_link.type == LinkType.YOUR: |
| 27 | + return list(_lookup_your(config, session, web_client, uri)) |
25 | 28 | elif sp_link.type is spotify.LinkType.TRACK:
|
26 | 29 | return list(_lookup_track(config, sp_link))
|
27 | 30 | elif sp_link.type is spotify.LinkType.ALBUM:
|
@@ -92,3 +95,31 @@ def _lookup_playlist(config, session, web_client, uri):
|
92 | 95 | if playlist is None:
|
93 | 96 | raise spotify.Error("Playlist Web API lookup failed")
|
94 | 97 | return playlist.tracks
|
| 98 | + |
| 99 | + |
| 100 | +def _lookup_your(config, session, web_client, uri): |
| 101 | + parts = uri.replace("spotify:your:", "").split(":") |
| 102 | + if len(parts) != 1: |
| 103 | + return |
| 104 | + variant = parts[0] |
| 105 | + |
| 106 | + items = browse._load_your_music(web_client, variant) |
| 107 | + if variant == "tracks": |
| 108 | + for item in items: |
| 109 | + # The extra level here is to also support "saved track objects". |
| 110 | + web_track = item.get("track", item) |
| 111 | + track = translator.web_to_track( |
| 112 | + web_track, bitrate=config["bitrate"] |
| 113 | + ) |
| 114 | + if track is not None: |
| 115 | + yield track |
| 116 | + elif variant == "albums": |
| 117 | + for item in items: |
| 118 | + # The extra level here is to also support "saved album objects". |
| 119 | + web_album = item.get("album", item) |
| 120 | + album_ref = translator.web_to_album_ref(web_album) |
| 121 | + if album_ref is None: |
| 122 | + continue |
| 123 | + sp_link = session.get_link(album_ref.uri) |
| 124 | + if sp_link.type is spotify.LinkType.ALBUM: |
| 125 | + yield from _lookup_album(config, sp_link) |
0 commit comments