Spaces:
Paused
Paused
| 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}`); | |
| }); | |