veltrixcode commited on
Commit
2fa42d9
·
verified ·
1 Parent(s): 04b6f1e

Update services/spotifyAuthService.js

Browse files
Files changed (1) hide show
  1. services/spotifyAuthService.js +33 -3
services/spotifyAuthService.js CHANGED
@@ -6,7 +6,37 @@ import { fileURLToPath } from "url";
6
 
7
  dotenv.config();
8
 
9
- const SP_DC = process.env.SP_DC;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  const DEFAULT_SECRETS_URL = "https://git.gay/thereallo/totp-secrets/raw/branch/main/secrets/secretDict.json";
11
  const FALLBACK_SECRETS_URL = "https://raw.githubusercontent.com/xyloflake/spot-secrets-go/refs/heads/main/secrets/secretDict.json";
12
 
@@ -165,7 +195,7 @@ export async function getToken(reason = "init", productType = "mobile-web-player
165
  'User-Agent': userAgent(),
166
  'Origin': 'https://open.spotify.com/',
167
  'Referer': 'https://open.spotify.com/',
168
- 'Cookie': `sp_dc=${SP_DC}`,
169
  },
170
  });
171
 
@@ -192,7 +222,7 @@ async function getServerTime() {
192
  'User-Agent': userAgent(),
193
  'Origin': 'https://open.spotify.com/',
194
  'Referer': 'https://open.spotify.com/',
195
- 'Cookie': `sp_dc=${SP_DC}`,
196
  },
197
  });
198
 
 
6
 
7
  dotenv.config();
8
 
9
+ let _spDcCache = null;
10
+
11
+ function getSpDc() {
12
+ if (_spDcCache) return _spDcCache;
13
+
14
+ // 1. Prefer env var
15
+ if (process.env.SP_DC && process.env.SP_DC !== 'undefined') {
16
+ _spDcCache = process.env.SP_DC;
17
+ return _spDcCache;
18
+ }
19
+
20
+ // 2. Fall back to cookies.txt
21
+ try {
22
+ const cookiePath = new URL('../cookies.txt', import.meta.url).pathname;
23
+ const text = fs.readFileSync(cookiePath, 'utf8');
24
+ for (const line of text.split('\n')) {
25
+ if (!line.trim() || line.startsWith('#')) continue;
26
+ const parts = line.split('\t');
27
+ if (parts.length >= 7 && parts[5] === 'sp_dc') {
28
+ _spDcCache = parts[6].trim();
29
+ console.log('sp_dc loaded from cookies.txt');
30
+ return _spDcCache;
31
+ }
32
+ }
33
+ } catch (_) {
34
+ // cookies.txt not found or unreadable
35
+ }
36
+
37
+ console.warn('Warning: sp_dc not found in env or cookies.txt');
38
+ return undefined;
39
+ }
40
  const DEFAULT_SECRETS_URL = "https://git.gay/thereallo/totp-secrets/raw/branch/main/secrets/secretDict.json";
41
  const FALLBACK_SECRETS_URL = "https://raw.githubusercontent.com/xyloflake/spot-secrets-go/refs/heads/main/secrets/secretDict.json";
42
 
 
195
  'User-Agent': userAgent(),
196
  'Origin': 'https://open.spotify.com/',
197
  'Referer': 'https://open.spotify.com/',
198
+ 'Cookie': `sp_dc=${getSpDc()}`,
199
  },
200
  });
201
 
 
222
  'User-Agent': userAgent(),
223
  'Origin': 'https://open.spotify.com/',
224
  'Referer': 'https://open.spotify.com/',
225
+ 'Cookie': `sp_dc=${getSpDc()}`,
226
  },
227
  });
228