fix: Spotify locale URL support + improved token headers

- parseSpotifyUrl now handles intl-de/, intl-en/ and other locale prefixes
- getSpotifyToken uses full browser headers + sp_t cookie for the public endpoint

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
HoneyandClaude Sonnet 4.6 committed 2026-04-27 01:37:57 +02:00
1 parent cfa8a8cd49
commit 6f419d24ff
1 file changed
+11 -4
+11 -4
View File
@@ -397,22 +397,29 @@ async function getSpotifyToken() {
'https://open.spotify.com/get_access_token?reason=transport&productType=web_player', 'https://open.spotify.com/get_access_token?reason=transport&productType=web_player',
{ {
headers: { headers: {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36',
Accept: 'application/json', Accept: 'application/json',
'Accept-Language': 'de-DE,de;q=0.9,en;q=0.8',
Cookie: 'sp_t=1',
Referer: 'https://open.spotify.com/',
'sec-fetch-dest': 'empty',
'sec-fetch-mode': 'cors',
'sec-fetch-site': 'same-origin',
}, },
signal: AbortSignal.timeout(8000), signal: AbortSignal.timeout(8000),
} }
); );
if (!r.ok) throw new Error(`Spotify public token fehlgeschlagen (${r.status})`); if (!r.ok) throw new Error(`Spotify token fehlgeschlagen (${r.status}) — Server-IP möglicherweise von Spotify geblockt`);
const d = await r.json(); const d = await r.json();
if (!d.accessToken) throw new Error('Kein Spotify-Token erhalten — Spotify möglicherweise nicht erreichbar'); if (!d.accessToken) throw new Error('Kein Spotify-Token erhalten');
_spTok = d.accessToken; _spTok = d.accessToken;
_spTokExp = d.accessTokenExpirationTimestampMs || (Date.now() + 3600000); _spTokExp = d.accessTokenExpirationTimestampMs || (Date.now() + 3600000);
return _spTok; return _spTok;
} }
function parseSpotifyUrl(url) { function parseSpotifyUrl(url) {
const m = url.match(/open\.spotify\.com\/(track|album|playlist)\/([A-Za-z0-9]+)/); // Handles locale prefixes: open.spotify.com/intl-de/track/... or open.spotify.com/track/...
const m = url.match(/open\.spotify\.com\/(?:[a-z]{2,5}-[a-z]{2,5}\/)?(?:intl-[a-z-]+\/)?(track|album|playlist)\/([A-Za-z0-9]+)/);
if (!m) throw new Error('Ungültige Spotify-URL — erwartet: open.spotify.com/track|album|playlist/...'); if (!m) throw new Error('Ungültige Spotify-URL — erwartet: open.spotify.com/track|album|playlist/...');
return { type: m[1], id: m[2] }; return { type: m[1], id: m[2] };
} }