IP is not actually blocked (api.spotify.com returns 401, not 403/timeout). Previous rate limits were temporary. Reverting to the clean state. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
103 lines
4.7 KiB
Python
103 lines
4.7 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Resolves Spotify URLs via SpotipyFree (SpotAPI + tls_client Chrome fingerprint,
|
|
hits Spotify's internal spclient endpoints — not the rate-limited public REST API).
|
|
"""
|
|
import sys, json, re, time
|
|
|
|
def eprint(*a): print(*a, file=sys.stderr)
|
|
|
|
# ─── APPROACH 1: SpotipyFree ─────────────────────────────────────────────────
|
|
|
|
def _fmt_spotipy_track(t, default_album='', default_art='', default_artist=''):
|
|
if not t or not t.get('name'): return None
|
|
artists = ', '.join(a.get('name','') for a in (t.get('artists') or [])) or default_artist
|
|
alb = t.get('album') or {}
|
|
imgs = alb.get('images') or []
|
|
art = sorted(imgs, key=lambda x: -(x.get('width') or 0))[0].get('url','') if imgs else default_art
|
|
return {
|
|
'trackName': t.get('name',''),
|
|
'artistName': artists,
|
|
'collectionName': alb.get('name','') or default_album,
|
|
'artworkUrl100': art,
|
|
'releaseDate': alb.get('release_date',''),
|
|
'trackNumber': t.get('track_number'),
|
|
'trackExplicitness': 'explicit' if t.get('explicit') else 'notExplicit',
|
|
'durationMs': t.get('duration_ms'),
|
|
}
|
|
|
|
def resolve_via_spotipyfree(sp_type, sp_id):
|
|
from SpotipyFree import Spotify as Sp
|
|
sp = Sp()
|
|
eprint('[SpotipyFree] session created')
|
|
|
|
if sp_type == 'track':
|
|
t = sp.track(sp_id)
|
|
artist = ', '.join(a['name'] for a in (t.get('artists') or []))
|
|
imgs = (t.get('album') or {}).get('images') or []
|
|
art = sorted(imgs, key=lambda x: -(x.get('width') or 0))[0].get('url','') if imgs else ''
|
|
tr = _fmt_spotipy_track(t)
|
|
return {'type':'track','name':t.get('name',''),'artist':artist,'artworkUrl':art,'tracks':[tr]}
|
|
|
|
if sp_type == 'album':
|
|
alb = sp.album(sp_id)
|
|
artist = ', '.join(a['name'] for a in (alb.get('artists') or []))
|
|
imgs = alb.get('images') or []
|
|
art = sorted(imgs, key=lambda x: -(x.get('width') or 0))[0].get('url','') if imgs else ''
|
|
tracks, offset = [], 0
|
|
while True:
|
|
page = sp.album_tracks(sp_id, limit=50, offset=offset)
|
|
for t in (page.get('items') or []):
|
|
tr = _fmt_spotipy_track(t, alb.get('name',''), art, artist)
|
|
if tr: tracks.append(tr)
|
|
if not page.get('next'): break
|
|
offset += len(page.get('items') or [])
|
|
time.sleep(0.3)
|
|
eprint(f'[SpotipyFree] album ok — {len(tracks)} tracks')
|
|
return {'type':'album','name':alb.get('name',''),'artist':artist,'artworkUrl':art,'tracks':tracks}
|
|
|
|
if sp_type == 'playlist':
|
|
pl = sp.playlist(sp_id, fields='name,owner,images')
|
|
imgs = pl.get('images') or []
|
|
art = sorted(imgs, key=lambda x: -(x.get('width') or 0))[0].get('url','') if imgs else ''
|
|
owner = (pl.get('owner') or {}).get('display_name','')
|
|
tracks, skipped, offset = [], 0, 0
|
|
while True:
|
|
page = sp.playlist_items(sp_id, limit=100, offset=offset,
|
|
additional_types=['track'])
|
|
for item in (page.get('items') or []):
|
|
t = item.get('track')
|
|
if not t or not t.get('id'): continue
|
|
if t.get('is_local'): skipped += 1; continue
|
|
tr = _fmt_spotipy_track(t)
|
|
if tr: tracks.append(tr)
|
|
if not page.get('next'): break
|
|
offset += len(page.get('items') or [])
|
|
time.sleep(0.3)
|
|
eprint(f'[SpotipyFree] playlist ok — {len(tracks)} tracks')
|
|
return {'type':'playlist','name':pl.get('name',''),'artist':owner,
|
|
'artworkUrl':art,'skippedLocal':skipped,'tracks':tracks}
|
|
|
|
# ─── MAIN ────────────────────────────────────────────────────────────────────
|
|
|
|
def main():
|
|
if len(sys.argv) < 2: eprint('No URL'); sys.exit(1)
|
|
url = sys.argv[1]
|
|
m = re.search(r'open\.spotify\.com/(?:.*/)?(track|album|playlist)/([A-Za-z0-9]+)', url)
|
|
if not m: eprint('Invalid URL'); sys.exit(1)
|
|
sp_type, sp_id = m.group(1), m.group(2)
|
|
eprint(f'[spotify] resolving {sp_type} {sp_id}')
|
|
|
|
try:
|
|
result = resolve_via_spotipyfree(sp_type, sp_id)
|
|
print(json.dumps(result, ensure_ascii=False))
|
|
except SystemExit: raise
|
|
except Exception as e:
|
|
eprint(f'[SpotipyFree] failed: {type(e).__name__}: {e}')
|
|
sys.exit(1)
|
|
|
|
if __name__ == '__main__':
|
|
try: main()
|
|
except SystemExit: raise
|
|
except Exception as e: eprint(f'fatal: {e}'); sys.exit(1)
|