fix: album_info yields pages (flatten), track items use flat format; track GenericError handling
- Album: flatten generator pages with list comprehension, parse flat track.{name,artists,...} directly
- Track: try both URI and URL, report GenericError clearly instead of crashing
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
1 parent
b74d0da294
commit
4f52e7b883
1 file changed
+44
-16
+44
-16
@@ -64,13 +64,23 @@ def resolve(sp_type, sp_id):
|
||||
url = f'https://open.spotify.com/{sp_type}/{sp_id}'
|
||||
|
||||
if sp_type == 'track':
|
||||
# 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 "?"}')
|
||||
if isinstance(raw, dict):
|
||||
for k, v in raw.items():
|
||||
eprint(f'[spotapi] track[{k!r}] = {repr(v)[:160]}')
|
||||
pub = sp_mod.Public()
|
||||
# Try URI first, then full URL
|
||||
raw = None
|
||||
for arg in [f'spotify:track:{sp_id}', url]:
|
||||
try:
|
||||
r = pub.song_info(arg)
|
||||
tu_type = ((r.get('data') or {}).get('trackUnion') or {}).get('__typename', '')
|
||||
if tu_type == 'GenericError':
|
||||
eprint(f'[spotapi] song_info({arg!r}) → GenericError, trying next')
|
||||
continue
|
||||
raw = r
|
||||
break
|
||||
except Exception as e:
|
||||
eprint(f'[spotapi] song_info({arg!r}) failed: {e}')
|
||||
if raw is None:
|
||||
raise Exception(f'song_info returned GenericError for all inputs — track may require auth or be region-locked')
|
||||
eprint(f'[spotapi] track raw keys={list(raw.keys())}')
|
||||
d = (raw.get('data') or {}) if isinstance(raw, dict) else {}
|
||||
tu = d.get('trackUnion') or d
|
||||
tr = _parse_track({'itemV2': {'data': {'trackUnion': tu}}}) or {
|
||||
@@ -89,7 +99,7 @@ def resolve(sp_type, sp_id):
|
||||
|
||||
if sp_type == 'album':
|
||||
# PublicAlbum.get_album_info() → metadata (data.albumUnion.{name,artists,coverArt})
|
||||
# Public().album_info(url) → generator that yields track items
|
||||
# Public().album_info(url) → generator that yields PAGES (lists of track dicts)
|
||||
cls_name = next((n for n in ('PublicAlbum', 'Album') if n in available), None)
|
||||
alb_meta = getattr(sp_mod, cls_name)(url).get_album_info() if cls_name else {}
|
||||
d = (alb_meta.get('data') or {}) if isinstance(alb_meta, dict) else {}
|
||||
@@ -102,15 +112,33 @@ def resolve(sp_type, sp_id):
|
||||
cover_items = (au.get('coverArt') or {}).get('sources') or []
|
||||
art = cover_items[0].get('url', '') if cover_items else ''
|
||||
|
||||
# album_info(url) yields track items — consume the generator
|
||||
raw_tracks = list(sp_mod.Public().album_info(url))
|
||||
eprint(f'[spotapi] album_info generator → {len(raw_tracks)} items; first={repr(raw_tracks[0])[:200] if raw_tracks else "none"}')
|
||||
# Flatten pages from the generator: each page is a list of track dicts
|
||||
raw_tracks = [item for page in sp_mod.Public().album_info(url)
|
||||
for item in (page if isinstance(page, list) else [page])]
|
||||
eprint(f'[spotapi] album_info → {len(raw_tracks)} tracks; first={repr(raw_tracks[0])[:200] if raw_tracks else "none"}')
|
||||
|
||||
def _alb_track(item):
|
||||
# items may be dicts with various shapes — log on parse failure
|
||||
tr = _parse_track({'itemV2': item}, default_album=name, default_art=art, default_artist=artist)
|
||||
if not tr:
|
||||
tr = _parse_track(item, default_album=name, default_art=art, default_artist=artist)
|
||||
return tr
|
||||
# Track items are flat: {track: {name, artists, contentRating, duration, trackNumber}}
|
||||
t = item.get('track') or item
|
||||
if not isinstance(t, dict): return None
|
||||
artist_items2 = (t.get('artists') or {}).get('items') or []
|
||||
artists = ', '.join(
|
||||
(a.get('profile') or {}).get('name', '') for a in artist_items2
|
||||
) or artist
|
||||
dur_ms = (t.get('duration') or {}).get('totalMilliseconds')
|
||||
rating = (t.get('contentRating') or {}).get('label', '')
|
||||
track_name = t.get('name', '')
|
||||
if not track_name: return None
|
||||
return {
|
||||
'trackName': track_name,
|
||||
'artistName': artists,
|
||||
'collectionName': name,
|
||||
'artworkUrl100': art,
|
||||
'releaseDate': '',
|
||||
'trackNumber': t.get('trackNumber'),
|
||||
'trackExplicitness': 'explicit' if rating == 'EXPLICIT' else 'notExplicit',
|
||||
'durationMs': dur_ms,
|
||||
}
|
||||
tracks = [t for t in (_alb_track(i) for i in raw_tracks) if t]
|
||||
eprint(f'[spotapi] album ok — {len(tracks)} tracks')
|
||||
return {'type': 'album', 'name': name, 'artist': artist, 'artworkUrl': art, 'tracks': tracks}
|
||||
|
||||
Reference in new issue
Block a user