File size: 11,054 Bytes
3c56493
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
import { json } from '@sveltejs/kit';
import type { RequestHandler } from './$types';
import crypto from 'crypto';

// Browser version for headers
const BROWSER_VERSION = '131';

// Common headers for Spotify requests
const COMMON_HEADERS = {
	'Content-Type': 'application/json',
	'User-Agent': `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/${BROWSER_VERSION}.0.0.0 Safari/537.36`,
	'Sec-Ch-Ua': `"Chromium";v="${BROWSER_VERSION}", "Not(A:Brand";v="24", "Google Chrome";v="${BROWSER_VERSION}"`
};

// Fallback secret (from SpotAPI code)
const FALLBACK_SECRET = [
	44, 55, 47, 42, 70, 40, 34, 114, 76, 74, 50, 111, 120, 97, 75, 76, 94, 102, 43, 69, 49, 120, 118,
	80, 64, 78
];

// Function to fetch latest secret
async function getLatestTotpSecret() {
	return { version: 61, secret: FALLBACK_SECRET };
}

// Generate TOTP
function generateTotp(secret: number[]) {
	const transformed = secret.map((e, t) => e ^ ((t % 33) + 9));
	const joined = transformed.map((num) => num.toString()).join('');
	const hexStr = Buffer.from(joined, 'ascii').toString('hex');
	const base32Secret = Buffer.from(hexStr, 'hex').toString('base64').replace(/=/g, '');

	// Simple TOTP generation (using crypto for HMAC)
	const timeStep = Math.floor(Date.now() / 1000 / 30);
	const timeHex = timeStep.toString(16).padStart(16, '0');
	const hmac = crypto.createHmac('sha1', Buffer.from(base32Secret, 'base64'));
	hmac.update(Buffer.from(timeHex, 'hex'));
	const digest = hmac.digest();
	const offset = digest[19] & 0xf;
	const code = (digest.readUInt32BE(offset) & 0x7fffffff) % 1000000;
	return code.toString().padStart(6, '0');
}

// Extract all JavaScript links from HTML
function extractJsLinks(html: string): string[] {
	const jsLinks: string[] = [];
	const scriptTagRegex = /<script[^>]+src="([^"]+\.js)"[^>]*>/g;
	let match;

	while ((match = scriptTagRegex.exec(html)) !== null) {
		jsLinks.push(match[1]);
	}

	return jsLinks;
}

// Get session and extract data
async function getSessionData() {
	const response = await fetch('https://open.spotify.com', {
		headers: COMMON_HEADERS
	});
	const html = await response.text();
	const cookie = response.headers.get('set-cookie')?.match(/sp_t=([^;]+)/)?.[1] || '';

	// Extract base64-encoded appServerConfig
	const appServerConfigMatch = html.match(
		/<script id="appServerConfig" type="text\/plain">([^<]+)<\/script>/
	);

	let clientVersion = '';
	if (appServerConfigMatch) {
		try {
			const base64Config = appServerConfigMatch[1];
			const decodedConfig = Buffer.from(base64Config, 'base64').toString('utf-8');
			const serverConfig = JSON.parse(decodedConfig);
			clientVersion = serverConfig.clientVersion || '';
		} catch (e) {
			console.error('Failed to parse appServerConfig, falling back to regex');
			// Fallback to old method if parsing fails
			clientVersion = html.match(/"clientVersion":"([^"]+)"/)?.[1] || '';
		}
	} else {
		// Fallback to old method if appServerConfig not found
		clientVersion = html.match(/"clientVersion":"([^"]+)"/)?.[1] || '';
	}

	// Extract all JS links and find the web-player one
	const allJsLinks = extractJsLinks(html);
	const jsPackRelative =
		allJsLinks.find((link) => link.includes('web-player/web-player') && link.endsWith('.js')) || '';
	const jsPack = jsPackRelative.startsWith('http')
		? jsPackRelative
		: `https://open.spotify.com${jsPackRelative}`;

	return { deviceId: cookie, clientVersion, jsPack };
}

// Get access token
async function getAccessToken(totp: string, totpVer: number) {
	const params = new URLSearchParams({
		reason: 'init',
		productType: 'web-player',
		totp,
		totpVer: totpVer.toString(),
		totpServer: totp
	});
	const response = await fetch(`https://open.spotify.com/api/token?${params}`, {
		headers: COMMON_HEADERS
	});
	const data = await response.json();
	return { accessToken: data.accessToken, clientId: data.clientId };
}

// Get client token
async function getClientToken(clientVersion: string, clientId: string, deviceId: string) {
	const payload = {
		client_data: {
			client_version: clientVersion,
			client_id: clientId,
			js_sdk_data: {
				device_brand: 'unknown',
				device_model: 'unknown',
				os: 'windows',
				os_version: 'NT 10.0',
				device_id: deviceId,
				device_type: 'computer'
			}
		}
	};
	const response = await fetch('https://clienttoken.spotify.com/v1/clienttoken', {
		method: 'POST',
		headers: {
			...COMMON_HEADERS,
			Authority: 'clienttoken.spotify.com',
			Accept: 'application/json'
		},
		body: JSON.stringify(payload)
	});
	const data = await response.json();
	return data.granted_token.token;
}

// Extract mappings from JS code
function extractMappings(jsCode: string): [Record<string, string>, Record<string, string>] {
	// Pattern to match objects like: {123:"value",456:"another"}
	const pattern = /\{\d+:"[^"]+"(?:,\d+:"[^"]+")*\}/g;
	const matches = jsCode.match(pattern);

	if (!matches || matches.length < 5) {
		console.warn(`Found only ${matches?.length || 0} mappings, need at least 5`);
		return [{}, {}];
	}

	// Parse the 4th match (index 3) as mapping1 (chunk names)
	const mapping1: Record<string, string> = {};
	const match3 = matches[3];
	const entries3 = match3.slice(1, -1).split(/,(?=\d+:)/);

	for (const entry of entries3) {
		const colonIndex = entry.indexOf(':');
		if (colonIndex === -1) continue;

		const key = entry.substring(0, colonIndex).trim();
		const value = entry
			.substring(colonIndex + 1)
			.trim()
			.replace(/^"|"$/g, '');
		mapping1[key] = value;
	}

	// Parse the 5th match (index 4) as mapping2 (chunk hashes)
	const mapping2: Record<string, string> = {};
	const match4 = matches[4];
	const entries4 = match4.slice(1, -1).split(/,(?=\d+:)/);

	for (const entry of entries4) {
		const colonIndex = entry.indexOf(':');
		if (colonIndex === -1) continue;

		const key = entry.substring(0, colonIndex).trim();
		const value = entry
			.substring(colonIndex + 1)
			.trim()
			.replace(/^"|"$/g, '');
		mapping2[key] = value;
	}

	return [mapping1, mapping2];
}

// Combine chunks from mappings
function combineChunks(
	strMapping: Record<string, string>,
	hashMapping: Record<string, string>
): string[] {
	const chunks: string[] = [];
	for (const [key, str] of Object.entries(strMapping)) {
		const hash = hashMapping[key];
		if (hash) {
			chunks.push(`${str}.${hash}.js`);
		}
	}
	return chunks;
}

// Get sha256 hash
async function getSha256Hash(jsPack: string): Promise<string> {
	if (!jsPack) {
		console.warn('No JS pack URL, using fallback hash');
		return 'a67612f8c59f4cb4a9723d8e0e0e7b7cb8c5c3d45e3d8c4f5e6f7e8f9a0b1c2d';
	}

	try {
		// Fetch the main JS pack
		const response = await fetch(jsPack, {
			headers: COMMON_HEADERS
		});
		let rawHashes = await response.text();

		// Extract mappings and combine chunks
		const [strMapping, hashMapping] = extractMappings(rawHashes);
		const chunks = combineChunks(strMapping, hashMapping);

		// Fetch additional chunks
		for (const chunk of chunks) {
			const chunkUrl = `https://open.spotifycdn.com/cdn/build/web-player/${chunk}`;
			try {
				const chunkResponse = await fetch(chunkUrl, {
					headers: COMMON_HEADERS
				});
				rawHashes += await chunkResponse.text();
			} catch (e) {
				console.warn(`Failed to fetch chunk ${chunk}:`, e);
			}
		}

		// Extract the fetchPlaylist hash
		let hash = '';
		try {
			// Try as query first
			hash = rawHashes.split('"fetchPlaylist","query","')[1].split('"')[0];
		} catch (e) {
			try {
				// Try as mutation
				hash = rawHashes.split('"fetchPlaylist","mutation","')[1].split('"')[0];
			} catch (e2) {
				console.warn('Failed to extract fetchPlaylist hash, using fallback');
				hash = 'a67612f8c59f4cb4a9723d8e0e0e7b7cb8c5c3d45e3d8c4f5e6f7e8f9a0b1c2d';
			}
		}
		return hash;
	} catch (error) {
		console.error('Failed to get sha256 hash:', error);
		return 'a67612f8c59f4cb4a9723d8e0e0e7b7cb8c5c3d45e3d8c4f5e6f7e8f9a0b1c2d';
	}
}

// Fetch playlist data
async function fetchPlaylist(
	accessToken: string,
	clientToken: string,
	clientVersion: string,
	playlistId: string,
	jsPack: string,
	offset = 0,
	limit = 25
) {
	const sha256Hash = await getSha256Hash(jsPack);
	const variables = {
		uri: `spotify:playlist:${playlistId}`,
		offset,
		limit,
		enableWatchFeedEntrypoint: false
	};
	const extensions = {
		persistedQuery: {
			version: 1,
			sha256Hash
		}
	};
	const params = JSON.stringify({
		operationName: 'fetchPlaylist',
		variables,
		extensions
	});
	console.log(params);
	const response = await fetch('https://api-partner.spotify.com/pathfinder/v2/query', {
		method: 'POST',
		headers: {
			'User-Agent': `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/${BROWSER_VERSION}.0.0.0 Safari/537.36`,
			'Sec-Ch-Ua': `"Chromium";v="${BROWSER_VERSION}", "Not(A:Brand";v="24", "Google Chrome";v="${BROWSER_VERSION}"`,
			Authorization: `Bearer ${accessToken}`,
			'Client-Token': clientToken,
			'Spotify-App-Version': clientVersion,
			'Content-Type': 'application/json;charset=UTF-8'
		},
		body: params
	});
	const data = await response.json();
	console.log(data);
	return data;
}

// Paginate playlist
async function getAllTracks(
	accessToken: string,
	clientToken: string,
	clientVersion: string,
	playlistId: string,
	jsPack: string
) {
	const tracks = [];
	let offset = 0;
	const limit = 343; // Upper limit
	while (true) {
		const data = await fetchPlaylist(
			accessToken,
			clientToken,
			clientVersion,
			playlistId,
			jsPack,
			offset,
			limit
		);
		const content = data?.data?.playlistV2?.content;
		if (!content) break;
		tracks.push(...content.items);
		if (content.totalCount <= offset + limit) break;
		offset += limit;
	}
	return tracks;
}

export const POST: RequestHandler = async ({ request }) => {
	try {
		const { playlistUrl } = await request.json();

		// Extract playlist ID from URL
		const playlistId = playlistUrl.includes('playlist/')
			? playlistUrl.split('playlist/')[1].split('?')[0]
			: playlistUrl;

		// Get session
		const { deviceId, clientVersion, jsPack } = await getSessionData();

		// Get TOTP
		const { secret, version } = await getLatestTotpSecret();
		const totp = generateTotp(secret);

		// Get tokens
		const { accessToken, clientId } = await getAccessToken(totp, version);
		const clientToken = await getClientToken(clientVersion, clientId, deviceId);

		// Get tracks
		const tracks = await getAllTracks(accessToken, clientToken, clientVersion, playlistId, jsPack);

		// Extract song links (track URIs in the format spotify:track:id)
		const songLinks = tracks
			.filter((item) => item?.itemV2?.data?.uri)
			.map((item) => {
				const uri = item.itemV2.data.uri;
				const trackId = uri.split(':')[2];
				return `https://open.spotify.com/track/${trackId}`;
			});

		return json({ songLinks, totalTracks: songLinks.length });
	} catch (error) {
		console.error('Spotify playlist fetch error:', error);
		return json(
			{
				error: 'Failed to fetch playlist',
				details: error instanceof Error ? error.message : 'Unknown error'
			},
			{ status: 500 }
		);
	}
};