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}'
|
url = f'https://open.spotify.com/{sp_type}/{sp_id}'
|
||||||
|
|
||||||
if sp_type == 'track':
|
if sp_type == 'track':
|
||||||
# song_info expects a Spotify URI, not a full URL
|
pub = sp_mod.Public()
|
||||||
uri = f'spotify:track:{sp_id}'
|
# Try URI first, then full URL
|
||||||
raw = sp_mod.Public().song_info(uri)
|
raw = None
|
||||||
eprint(f'[spotapi] track raw type={type(raw).__name__} keys={list(raw.keys()) if isinstance(raw, dict) else "?"}')
|
for arg in [f'spotify:track:{sp_id}', url]:
|
||||||
if isinstance(raw, dict):
|
try:
|
||||||
for k, v in raw.items():
|
r = pub.song_info(arg)
|
||||||
eprint(f'[spotapi] track[{k!r}] = {repr(v)[:160]}')
|
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 {}
|
d = (raw.get('data') or {}) if isinstance(raw, dict) else {}
|
||||||
tu = d.get('trackUnion') or d
|
tu = d.get('trackUnion') or d
|
||||||
tr = _parse_track({'itemV2': {'data': {'trackUnion': tu}}}) or {
|
tr = _parse_track({'itemV2': {'data': {'trackUnion': tu}}}) or {
|
||||||
@@ -89,7 +99,7 @@ def resolve(sp_type, sp_id):
|
|||||||
|
|
||||||
if sp_type == 'album':
|
if sp_type == 'album':
|
||||||
# PublicAlbum.get_album_info() → metadata (data.albumUnion.{name,artists,coverArt})
|
# 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)
|
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 {}
|
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 {}
|
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 []
|
cover_items = (au.get('coverArt') or {}).get('sources') or []
|
||||||
art = cover_items[0].get('url', '') if cover_items else ''
|
art = cover_items[0].get('url', '') if cover_items else ''
|
||||||
|
|
||||||
# album_info(url) yields track items — consume the generator
|
# Flatten pages from the generator: each page is a list of track dicts
|
||||||
raw_tracks = list(sp_mod.Public().album_info(url))
|
raw_tracks = [item for page in 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"}')
|
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):
|
def _alb_track(item):
|
||||||
# items may be dicts with various shapes — log on parse failure
|
# Track items are flat: {track: {name, artists, contentRating, duration, trackNumber}}
|
||||||
tr = _parse_track({'itemV2': item}, default_album=name, default_art=art, default_artist=artist)
|
t = item.get('track') or item
|
||||||
if not tr:
|
if not isinstance(t, dict): return None
|
||||||
tr = _parse_track(item, default_album=name, default_art=art, default_artist=artist)
|
artist_items2 = (t.get('artists') or {}).get('items') or []
|
||||||
return tr
|
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]
|
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