deepak191z commited on
Commit
23fafb2
·
verified ·
1 Parent(s): 567ad9e

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +83 -47
server.js CHANGED
@@ -1,5 +1,3 @@
1
- //!/usr/bin/env node
2
-
3
  const Fastify = require('fastify');
4
  const fs = require('fs');
5
  const { chromium } = require('playwright-extra');
@@ -8,6 +6,8 @@ chromium.use(stealth);
8
 
9
  const configFile = fs.readFileSync(__dirname + '/config.json', 'utf8');
10
  const configData = JSON.parse(configFile);
 
 
11
  const fastify = Fastify();
12
  const PORT = 7860;
13
 
@@ -16,53 +16,89 @@ fastify.get('/', async (request, reply) => {
16
  });
17
 
18
  fastify.post('/search', async (request, reply) => {
19
- const { searchText } = request.body;
20
- const url = 'https://chat.deepseek.com';
21
- const buttonSubmit = '.f6d670';
22
- const textareaSearchBox = '#chat-input';
23
- const textMessage = '.ds-markdown';
24
- const profileButton = '.d65532b2';
25
- const deleteAllButton = '.ds-dropdown-menu-option__label:has-text("Delete all chats")';
26
- const confirmDeleteButton = '.ds-button:has-text("Confirm deletion")';
27
- const totalLoopCount = 60;
28
- const printIntervalTime = 1000;
29
-
30
- const browser = await chromium.launch({ headless: true, timeout: 30000 });
31
- const context = await browser.newContext({ userAgent: configData.userAgent });
32
- await context.addCookies([{
33
- 'name': 'cf_clearance',
34
- 'value': configData.cfClearance,
35
- 'domain': '.deepseek.com',
36
- 'path': '/',
37
- 'httpOnly': true,
38
- 'secure': true
39
- }]);
40
-
41
- const page = await context.newPage();
42
- await page.goto(url, { waitUntil: 'domcontentloaded' });
43
- await page.evaluate(userToken => {
44
- localStorage.setItem('searchEnabled', 'false');
45
- localStorage.setItem('userToken', `{"value":"${userToken}","__version":"0"}`);
46
- }, configData.userToken);
47
- await page.goto(url, { waitUntil: 'domcontentloaded' });
48
-
49
- await page.fill(textareaSearchBox, searchText);
50
- await page.click(buttonSubmit);
51
-
52
- let prevResult = '';
53
- for (let i = 0; i < totalLoopCount; i++) {
54
- await new Promise(resolve => setTimeout(resolve, printIntervalTime));
55
- const result = await page.locator(textMessage).innerText();
56
- if (prevResult === result && i !== totalLoopCount - 1) break;
57
- prevResult = result;
58
- }
 
59
 
60
- await page.click(profileButton);
61
- await page.click(deleteAllButton);
62
- await page.click(confirmDeleteButton);
63
 
64
- await browser.close();
65
- return reply.send({ response: prevResult });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66
  });
67
 
68
  fastify.listen({ port: PORT, host: '0.0.0.0' }, (err, address) => {
 
 
 
1
  const Fastify = require('fastify');
2
  const fs = require('fs');
3
  const { chromium } = require('playwright-extra');
 
6
 
7
  const configFile = fs.readFileSync(__dirname + '/config.json', 'utf8');
8
  const configData = JSON.parse(configFile);
9
+ console.log('Loaded config data:', configData);
10
+
11
  const fastify = Fastify();
12
  const PORT = 7860;
13
 
 
16
  });
17
 
18
  fastify.post('/search', async (request, reply) => {
19
+ try {
20
+ const { searchText } = request.body;
21
+ console.log('Received searchText:', searchText);
22
+
23
+ const url = 'https://chat.deepseek.com';
24
+ const buttonSubmit = '.f6d670';
25
+ const textareaSearchBox = '#chat-input';
26
+ const textMessage = '.ds-markdown';
27
+ const profileButton = '.d65532b2';
28
+ const deleteAllButton = '.ds-dropdown-menu-option__label:has-text("Delete all chats")';
29
+ const confirmDeleteButton = '.ds-button:has-text("Confirm deletion")';
30
+ const totalLoopCount = 60;
31
+ const printIntervalTime = 1000;
32
+
33
+ const browser = await chromium.launch({ headless: true, timeout: 30000 });
34
+ console.log('Browser launched');
35
+
36
+ const context = await browser.newContext({ userAgent: configData.userAgent });
37
+ console.log('Context created with userAgent:', configData.userAgent);
38
+
39
+ await context.addCookies([{
40
+ name: 'cf_clearance',
41
+ value: configData.cfClearance,
42
+ domain: '.deepseek.com',
43
+ path: '/',
44
+ httpOnly: true,
45
+ secure: true
46
+ }]);
47
+ console.log('Cookies added');
48
+
49
+ const page = await context.newPage();
50
+ console.log('New page created');
51
+
52
+ await page.goto(url, { waitUntil: 'domcontentloaded' });
53
+ console.log('Navigated to:', url);
54
+
55
+ await page.evaluate(userToken => {
56
+ localStorage.setItem('searchEnabled', 'false');
57
+ localStorage.setItem('userToken', `{"value":"${userToken}","__version":"0"}`);
58
+ console.log('Local storage set for userToken');
59
+ }, configData.userToken);
60
 
61
+ await page.goto(url, { waitUntil: 'domcontentloaded' });
62
+ console.log('Page reloaded after setting local storage');
 
63
 
64
+ await page.fill(textareaSearchBox, searchText);
65
+ console.log('Filled search text into textarea');
66
+
67
+ await page.click(buttonSubmit);
68
+ console.log('Clicked submit button');
69
+
70
+ let prevResult = '';
71
+ for (let i = 0; i < totalLoopCount; i++) {
72
+ console.log(`Iteration ${i + 1}: waiting for ${printIntervalTime}ms`);
73
+ await new Promise(resolve => setTimeout(resolve, printIntervalTime));
74
+
75
+ const result = await page.locator(textMessage).innerText();
76
+ console.log(`Iteration ${i + 1}: result:`, result);
77
+
78
+ if (prevResult === result && i !== totalLoopCount - 1) {
79
+ console.log('No change in result, breaking the loop.');
80
+ break;
81
+ }
82
+ prevResult = result;
83
+ }
84
+
85
+ await page.click(profileButton);
86
+ console.log('Clicked profile button');
87
+
88
+ await page.click(deleteAllButton);
89
+ console.log('Clicked delete all button');
90
+
91
+ await page.click(confirmDeleteButton);
92
+ console.log('Confirmed deletion');
93
+
94
+ await browser.close();
95
+ console.log('Browser closed');
96
+
97
+ return reply.send({ response: prevResult });
98
+ } catch (error) {
99
+ console.error('Error during search operation:', error);
100
+ return reply.status(500).send({ error: 'Internal Server Error' });
101
+ }
102
  });
103
 
104
  fastify.listen({ port: PORT, host: '0.0.0.0' }, (err, address) => {