Spaces:
Running
Running
Upload 11 files
Browse files
services/browserAuthService.js
CHANGED
|
@@ -56,6 +56,44 @@ const syncCookies = async () => {
|
|
| 56 |
}
|
| 57 |
};
|
| 58 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
export const initPersistentBrowser = async () => {
|
| 60 |
if (browser) {
|
| 61 |
try {
|
|
@@ -110,11 +148,14 @@ export const initPersistentBrowser = async () => {
|
|
| 110 |
await page.goto('https://open.spotify.com/search', { waitUntil: 'networkidle2', timeout: 60000 });
|
| 111 |
await syncCookies();
|
| 112 |
|
| 113 |
-
//
|
|
|
|
|
|
|
|
|
|
| 114 |
setInterval(async () => {
|
| 115 |
try {
|
| 116 |
-
console.log('
|
| 117 |
-
await
|
| 118 |
await syncCookies();
|
| 119 |
} catch (error) {
|
| 120 |
console.error('Error refreshing persistent browser page:', error.message);
|
|
@@ -131,17 +172,17 @@ export const getBrowserCredentials = async () => {
|
|
| 131 |
return cachedCreds;
|
| 132 |
}
|
| 133 |
|
| 134 |
-
console.log('Credentials expired or missing,
|
| 135 |
|
| 136 |
const credsPromise = new Promise((resolve) => {
|
| 137 |
pendingCredsResolver = resolve;
|
| 138 |
});
|
| 139 |
|
| 140 |
try {
|
| 141 |
-
await
|
| 142 |
const creds = await Promise.race([
|
| 143 |
credsPromise,
|
| 144 |
-
new Promise((_, reject) => setTimeout(() => reject(new Error('Credential detection timed out')),
|
| 145 |
]);
|
| 146 |
return creds;
|
| 147 |
} catch (error) {
|
|
|
|
| 56 |
}
|
| 57 |
};
|
| 58 |
|
| 59 |
+
// Plays a song briefly to trigger cookie/token updates
|
| 60 |
+
const playSongToRefreshSession = async () => {
|
| 61 |
+
if (!page) return;
|
| 62 |
+
try {
|
| 63 |
+
console.log('Navigating to a Spotify track to trigger playback session refresh...');
|
| 64 |
+
// Navigate to a popular stable track
|
| 65 |
+
await page.goto('https://open.spotify.com/track/4PTG3Z6ehGkBF3zI7Ywt8q', { waitUntil: 'networkidle2', timeout: 30000 });
|
| 66 |
+
|
| 67 |
+
// Wait for the main play button to be rendered
|
| 68 |
+
const playButtonSelector = 'button[data-testid="play-button"], button[aria-label="Play"]';
|
| 69 |
+
await page.waitForSelector(playButtonSelector, { timeout: 10000 });
|
| 70 |
+
|
| 71 |
+
// Click play
|
| 72 |
+
await page.click(playButtonSelector);
|
| 73 |
+
console.log('Playback command sent. Waiting 6 seconds to let Spotify update session tokens...');
|
| 74 |
+
await new Promise(resolve => setTimeout(resolve, 6000));
|
| 75 |
+
|
| 76 |
+
// Pause playback to prevent running up data/audio streams indefinitely
|
| 77 |
+
const pauseButtonSelector = 'button[data-testid="control-button-playpause"], button[aria-label="Pause"]';
|
| 78 |
+
try {
|
| 79 |
+
await page.waitForSelector(pauseButtonSelector, { timeout: 2000 });
|
| 80 |
+
await page.click(pauseButtonSelector);
|
| 81 |
+
console.log('Playback paused successfully.');
|
| 82 |
+
} catch (e) {
|
| 83 |
+
console.log('Could not find pause button (or already paused), continuing...');
|
| 84 |
+
}
|
| 85 |
+
|
| 86 |
+
// Navigate back to search to be ready for search header intercepts
|
| 87 |
+
await page.goto('https://open.spotify.com/search', { waitUntil: 'networkidle2', timeout: 30000 });
|
| 88 |
+
} catch (error) {
|
| 89 |
+
console.error('Failed to trigger song playback refresh:', error.message);
|
| 90 |
+
// Fallback: make sure we are back on search page
|
| 91 |
+
try {
|
| 92 |
+
await page.goto('https://open.spotify.com/search', { waitUntil: 'networkidle2', timeout: 20000 });
|
| 93 |
+
} catch (e) {}
|
| 94 |
+
}
|
| 95 |
+
};
|
| 96 |
+
|
| 97 |
export const initPersistentBrowser = async () => {
|
| 98 |
if (browser) {
|
| 99 |
try {
|
|
|
|
| 148 |
await page.goto('https://open.spotify.com/search', { waitUntil: 'networkidle2', timeout: 60000 });
|
| 149 |
await syncCookies();
|
| 150 |
|
| 151 |
+
// Play a song on initialization to ensure cookies get updated immediately
|
| 152 |
+
await playSongToRefreshSession();
|
| 153 |
+
|
| 154 |
+
// Refresh every 15 minutes by playing a song and syncing cookies
|
| 155 |
setInterval(async () => {
|
| 156 |
try {
|
| 157 |
+
console.log('Triggering periodic playback session refresh...');
|
| 158 |
+
await playSongToRefreshSession();
|
| 159 |
await syncCookies();
|
| 160 |
} catch (error) {
|
| 161 |
console.error('Error refreshing persistent browser page:', error.message);
|
|
|
|
| 172 |
return cachedCreds;
|
| 173 |
}
|
| 174 |
|
| 175 |
+
console.log('Credentials expired or missing, triggering song play session to refresh...');
|
| 176 |
|
| 177 |
const credsPromise = new Promise((resolve) => {
|
| 178 |
pendingCredsResolver = resolve;
|
| 179 |
});
|
| 180 |
|
| 181 |
try {
|
| 182 |
+
await playSongToRefreshSession();
|
| 183 |
const creds = await Promise.race([
|
| 184 |
credsPromise,
|
| 185 |
+
new Promise((_, reject) => setTimeout(() => reject(new Error('Credential detection timed out')), 25000))
|
| 186 |
]);
|
| 187 |
return creds;
|
| 188 |
} catch (error) {
|