File size: 3,354 Bytes
b60b4f4
 
 
 
 
 
 
 
 
 
 
0c2570e
 
 
 
b60b4f4
e8e42e3
 
 
 
 
 
 
 
 
 
 
 
cc893bd
e8e42e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23fafb2
e8e42e3
 
 
 
 
 
23fafb2
e8e42e3
 
 
 
 
 
 
 
 
 
 
 
 
 
23fafb2
 
e8e42e3
23fafb2
e8e42e3
 
 
 
23fafb2
e8e42e3
 
 
 
b60b4f4
c3bdfc2
f55497a
b60b4f4
 
 
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
const Fastify = require('fastify');
const fs = require('fs');
const { chromium } = require('playwright-extra');
const stealth = require('puppeteer-extra-plugin-stealth')();
chromium.use(stealth);

const configFile = fs.readFileSync(__dirname + '/config.json', 'utf8');
const configData = JSON.parse(configFile);
const fastify = Fastify();
const PORT = 7860;

fastify.get('/', async (request, reply) => {
  return reply.send({ message: 'Fastify Playwright API is running' });
});

fastify.post('/search', async (request, reply) => {
  const { searchText } = request.body;
  const url = 'https://chat.deepseek.com';
  const buttonSubmit = '.f6d670';
  const textareaSearchBox = '#chat-input';
  const textMessage = '.ds-markdown';
  const profileButton = '.d65532b2';
  const deleteAllButton = '.ds-dropdown-menu-option__label:has-text("Delete all chats")';
  const confirmDeleteButton = '.ds-button:has-text("Confirm deletion")';
  const totalLoopCount = 60;
  const printIntervalTime = 1000;

  console.log('[INFO] Launching browser...');
  const browser = await chromium.launch({ headless: true, slowMo: 100 }); // Headed mode for debugging
  const context = await browser.newContext({ userAgent: configData.userAgent });

  console.log('[INFO] Setting cookies...');
  await context.addCookies([{
    'name': 'cf_clearance',
    'value': configData.cfClearance,
    'domain': '.deepseek.com',
    'path': '/',
    'httpOnly': true,
    'secure': true
  }]);

  const page = await context.newPage();
  
  console.log(`[INFO] Navigating to ${url}...`);
  await page.goto(url, { waitUntil: 'load', timeout: 60000 });

  console.log('[INFO] Setting localStorage...');
  await page.evaluate(userToken => {
    localStorage.setItem('searchEnabled', 'false');
    localStorage.setItem('userToken', `{"value":"${userToken}","__version":"0"}`);
  }, configData.userToken);
  
  console.log('[INFO] Reloading page after setting localStorage...');
  await page.goto(url, { waitUntil: 'load' });

  console.log('[INFO] Waiting for search input...');
  try {
    await page.waitForSelector(textareaSearchBox, { timeout: 60000, state: 'visible' });
  } catch (error) {
    console.error('[ERROR] Search input not found. Exiting.');
    await browser.close();
    return reply.send({ error: 'Search input not found!' });
  }

  console.log('[INFO] Typing search text...');
  await page.fill(textareaSearchBox, searchText);
  await page.click(buttonSubmit);

  let prevResult = '';
  console.log('[INFO] Waiting for response...');
  
  for (let i = 0; i < totalLoopCount; i++) {
    await new Promise(resolve => setTimeout(resolve, printIntervalTime));
    const result = await page.locator(textMessage).innerText().catch(() => null);
    
    if (result) {
      console.log(`[INFO] Response received: ${result.slice(0, 50)}...`);
      if (prevResult === result && i !== totalLoopCount - 1) break;
      prevResult = result;
    }
  }

  console.log('[INFO] Deleting previous chats...');
  await page.click(profileButton);
  await page.click(deleteAllButton);
  await page.click(confirmDeleteButton);

  console.log('[INFO] Closing browser...');
  await browser.close();
  
  return reply.send({ response: prevResult });
});

fastify.listen({ port: PORT, host: '0.0.0.0' }, (err, address) => {
  if (err) throw err;
  console.log(`Server running at ${address}`);
});