fix: song_info needs URI not URL; album_info returns generator of track items
- Track: pass spotify:track:ID URI instead of full https:// URL - Album: Public().album_info() yields track items (generator); metadata from PublicAlbum Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
1 parent
a401c7a607
commit
b74d0da294
1 file changed
+20
-22
+20
-22
@@ -64,7 +64,9 @@ def resolve(sp_type, sp_id):
|
|||||||
url = f'https://open.spotify.com/{sp_type}/{sp_id}'
|
url = f'https://open.spotify.com/{sp_type}/{sp_id}'
|
||||||
|
|
||||||
if sp_type == 'track':
|
if sp_type == 'track':
|
||||||
raw = sp_mod.Public().song_info(url)
|
# song_info expects a Spotify URI, not a full URL
|
||||||
|
uri = f'spotify:track:{sp_id}'
|
||||||
|
raw = sp_mod.Public().song_info(uri)
|
||||||
eprint(f'[spotapi] track raw type={type(raw).__name__} keys={list(raw.keys()) if isinstance(raw, dict) else "?"}')
|
eprint(f'[spotapi] track raw type={type(raw).__name__} keys={list(raw.keys()) if isinstance(raw, dict) else "?"}')
|
||||||
if isinstance(raw, dict):
|
if isinstance(raw, dict):
|
||||||
for k, v in raw.items():
|
for k, v in raw.items():
|
||||||
@@ -86,33 +88,29 @@ def resolve(sp_type, sp_id):
|
|||||||
'tracks': [tr]}
|
'tracks': [tr]}
|
||||||
|
|
||||||
if sp_type == 'album':
|
if sp_type == 'album':
|
||||||
alb_info = sp_mod.Public().album_info(url)
|
# PublicAlbum.get_album_info() → metadata (data.albumUnion.{name,artists,coverArt})
|
||||||
eprint(f'[spotapi] album raw type={type(alb_info).__name__} keys={list(alb_info.keys()) if isinstance(alb_info, dict) else "?"}')
|
# Public().album_info(url) → generator that yields track items
|
||||||
if isinstance(alb_info, dict):
|
cls_name = next((n for n in ('PublicAlbum', 'Album') if n in available), None)
|
||||||
for k, v in alb_info.items():
|
alb_meta = getattr(sp_mod, cls_name)(url).get_album_info() if cls_name else {}
|
||||||
eprint(f'[spotapi] alb[{k!r}] = {repr(v)[:160]}')
|
d = (alb_meta.get('data') or {}) if isinstance(alb_meta, dict) else {}
|
||||||
# Partner API: data.albumUnion.{name, artists, coverArt, tracks.items[]}
|
au = d.get('albumUnion') or d.get('albumV2') or {}
|
||||||
d = (alb_info.get('data') or {}) if isinstance(alb_info, dict) else {}
|
|
||||||
au = d.get('albumUnion') or d.get('albumV2') or (alb_info if isinstance(alb_info, dict) else {})
|
|
||||||
name = au.get('name', '')
|
name = au.get('name', '')
|
||||||
# artist(s)
|
|
||||||
artist_items = (au.get('artists') or {}).get('items') or []
|
artist_items = (au.get('artists') or {}).get('items') or []
|
||||||
artist = ', '.join(
|
artist = ', '.join(
|
||||||
(a.get('profile') or {}).get('name', '') or a.get('name', '') for a in artist_items
|
(a.get('profile') or {}).get('name', '') or a.get('name', '') for a in artist_items
|
||||||
) or au.get('artist', '')
|
)
|
||||||
# artwork
|
cover_items = (au.get('coverArt') or {}).get('sources') or []
|
||||||
cover_items = (au.get('coverArt') or {}).get('sources') or (au.get('images') or {}).get('items') or []
|
|
||||||
art = cover_items[0].get('url', '') if cover_items else ''
|
art = cover_items[0].get('url', '') if cover_items else ''
|
||||||
# tracks
|
|
||||||
raw_tracks = (au.get('tracks') or {}).get('items') or []
|
# album_info(url) yields track items — consume the generator
|
||||||
eprint(f'[spotapi] album tracks count={len(raw_tracks)} first={repr(raw_tracks[0])[:200] if raw_tracks else "none"}')
|
raw_tracks = list(sp_mod.Public().album_info(url))
|
||||||
# album track items: {uid, track: {__typename, data: {trackUnion: {...}}}}
|
eprint(f'[spotapi] album_info generator → {len(raw_tracks)} items; first={repr(raw_tracks[0])[:200] if raw_tracks else "none"}')
|
||||||
# wrap as {itemV2: track_obj} so _parse_track can navigate itemV2.data.trackUnion
|
|
||||||
def _alb_track(item):
|
def _alb_track(item):
|
||||||
track_obj = item.get('track') or item
|
# items may be dicts with various shapes — log on parse failure
|
||||||
return _parse_track({'itemV2': track_obj},
|
tr = _parse_track({'itemV2': item}, default_album=name, default_art=art, default_artist=artist)
|
||||||
default_album=name, default_art=art,
|
if not tr:
|
||||||
default_artist=artist)
|
tr = _parse_track(item, default_album=name, default_art=art, default_artist=artist)
|
||||||
|
return tr
|
||||||
tracks = [t for t in (_alb_track(i) for i in raw_tracks) if t]
|
tracks = [t for t in (_alb_track(i) for i in raw_tracks) if t]
|
||||||
eprint(f'[spotapi] album ok — {len(tracks)} tracks')
|
eprint(f'[spotapi] album ok — {len(tracks)} tracks')
|
||||||
return {'type': 'album', 'name': name, 'artist': artist, 'artworkUrl': art, 'tracks': tracks}
|
return {'type': 'album', 'name': name, 'artist': artist, 'artworkUrl': art, 'tracks': tracks}
|
||||||
|
|||||||
Reference in new issue
Block a user