Spaces:
Paused
Paused
//!/usr/bin/env node | |
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.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; | |
const browser = await chromium.launch({ headless: true, timeout: 30000 }); | |
const context = await browser.newContext({ userAgent: configData.userAgent }); | |
await context.addCookies([{ | |
'name': 'cf_clearance', | |
'value': configData.cfClearance, | |
'domain': '.deepseek.com', | |
'path': '/', | |
'httpOnly': true, | |
'secure': true | |
}]); | |
const page = await context.newPage(); | |
await page.goto(url, { waitUntil: 'domcontentloaded' }); | |
await page.evaluate(userToken => { | |
localStorage.setItem('searchEnabled', 'true'); | |
localStorage.setItem('userToken', `{"value":"${userToken}","__version":"0"}`); | |
}, configData.userToken); | |
await page.goto(url, { waitUntil: 'domcontentloaded' }); | |
await page.fill(textareaSearchBox, searchText); | |
await page.click(buttonSubmit); | |
let prevResult = ''; | |
for (let i = 0; i < totalLoopCount; i++) { | |
await new Promise(resolve => setTimeout(resolve, printIntervalTime)); | |
const result = await page.locator(textMessage).innerText(); | |
if (prevResult === result && i !== totalLoopCount - 1) break; | |
prevResult = result; | |
} | |
await page.click(profileButton); | |
await page.click(deleteAllButton); | |
await page.click(confirmDeleteButton); | |
await browser.close(); | |
return reply.send({ response: prevResult }); | |
}); | |
fastify.listen({ port: PORT }, (err, address) => { | |
if (err) throw err; | |
console.log(`Server running at ${address}`); | |
}); | |