File size: 2,653 Bytes
226daaa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import puppeteer from 'puppeteer-core';
import fs from 'fs';

const parseNetscapeCookies = (text) => {
  const cookies = [];
  const lines = text.split('\n');
  for (const line of lines) {
    if (!line.trim() || line.startsWith('#')) continue;
    const parts = line.split('\t');
    if (parts.length >= 7) {
      cookies.push({
        name: parts[5],
        value: parts[6].trim(),
        domain: parts[0],
        path: parts[2],
        secure: parts[3] === 'TRUE',
        expires: parseFloat(parts[4])
      });
    }
  }
  return cookies;
};

async function testNetworkInterception() {
  const cookieText = fs.readFileSync('cookies.txt', 'utf8');
  const cookies = parseNetscapeCookies(cookieText);

  const executablePath = '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome';
  const browser = await puppeteer.launch({
    executablePath,
    headless: true,
    args: ['--no-sandbox', '--disable-setuid-sandbox']
  });

  const page = await browser.newPage();
  await page.setCookie(...cookies);

  let searchResponseData = null;

  console.log("Listening for Pathfinder query responses...");
  page.on('response', async (response) => {
    const url = response.url();
    if (url.includes('api-partner.spotify.com/pathfinder/v1/query')) {
      const request = response.request();
      const postData = request.postData() || '';
      
      // Check if it's the search request
      if (url.includes('search') || postData.includes('search') || postData.includes('Search') || url.includes('operationName=search')) {
        try {
          const text = await response.text();
          const json = JSON.parse(text);
          console.log("FOUND SEARCH GRAPHQL RESPONSE!");
          searchResponseData = json;
        } catch (e) {
          // Ignore parse errors or text errors
        }
      }
    }
  });

  const query = "Kill Bill SZA";
  console.log(`Navigating to search page for: ${query}...`);
  // Navigate directly to the search URL for the query
  const searchUrl = `https://open.spotify.com/search/${encodeURIComponent(query)}`;
  await page.goto(searchUrl, { waitUntil: 'networkidle2', timeout: 30000 });

  // Wait another 5 seconds to ensure requests finish loading
  await new Promise(resolve => setTimeout(resolve, 5000));

  console.log("Closing browser...");
  await browser.close();

  if (searchResponseData) {
    console.log("GraphQL Search Data (truncated):", JSON.stringify(searchResponseData, null, 2).substring(0, 1500));
  } else {
    console.log("Search response not captured via network interception. Let's list all requests we saw:");
  }
}

testNetworkInterception().catch(console.error);