Last active
January 27, 2025 10:19
-
-
Save styoe/3fd856243e5026e9f90b160728254b10 to your computer and use it in GitHub Desktop.
A completely innocent gist to help you get kickstarted with deepseek
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import puppeteer from 'puppeteer'; | |
| // Or import puppeteer from 'puppeteer-core'; | |
| const SIGNIN_PAGE = "https://chat.deepseek.com/sign_in" | |
| const CHAT_PAGE = "https://chat.deepseek.com/chat" | |
| const SIGN_IN_MAIL = "Your dummy email" | |
| const SIGN_IN_PASSWORD = "your dummy password" | |
| const sleep = async (ms) => { | |
| return new Promise(resolve => setTimeout(resolve, ms)); | |
| } | |
| const turnOnButtons = async () => { | |
| try { | |
| const buttons = await page.$$('.ds-button') | |
| let buttonsClicked = 0 | |
| for (const el of buttons) { | |
| if (buttonsClicked >= 1) break; | |
| console.log("Clicking button"); | |
| await sleep(1000); | |
| // await el.click() | |
| buttonsClicked++ | |
| } | |
| console.log("buttonsClicked: ", buttonsClicked); | |
| if(buttonsClicked === 0) { | |
| await sleep(1000); | |
| await turnOnButtons(); | |
| } | |
| for (const button of buttons) { | |
| await button.click(); | |
| } | |
| } catch(e) { | |
| console.log("Error finding buttons", e); | |
| } | |
| } | |
| const getLastResponseContent = async () => { | |
| const maxAttempts = 60; | |
| // Get starting number of elements | |
| let totalResponses = 0; | |
| try { | |
| const responses = await page.$eval( | |
| '.ds-markdown', | |
| (el) => {return el.textContent; } | |
| ); | |
| totalResponses = responses.length; | |
| console.log("totalResponses: ", totalResponses); | |
| } catch(e) { | |
| console.log("totalResponses: ", totalResponses, ".ds-markdown not found"); | |
| } | |
| let finalResponse = null | |
| for (let i = 0; i < maxAttempts; i++) { | |
| if (i == maxAttempts) break; | |
| let responses = [] | |
| // Try getting the elements | |
| try { | |
| responses = await page.$$eval( | |
| '.ds-markdown > p', | |
| (els) => { | |
| return els.map(el => el.innerHTML); | |
| } | |
| ); | |
| // If there are no new elements, wait and try again | |
| if (!responses || responses.length === 0 || responses.length === totalResponses) { | |
| console.log("No responses found, retrying..."); | |
| await sleep(1000); | |
| continue; | |
| } | |
| } catch(e) { | |
| console.log("Responses error, no elements found, retrying..."); | |
| // console.log("e: ", e); | |
| // if there is no .ds-markdown, wait and try again | |
| await sleep(1000); | |
| continue; | |
| } | |
| const currentResponse = responses[responses.length - 1]; | |
| console.log("currentResponse: ", currentResponse); | |
| console.log("typeof currentResponse: ", typeof currentResponse); | |
| console.log("finalResponse.textContent: ", finalResponse); | |
| // If the response has changed, update the response and wait | |
| if (!currentResponse || currentResponse !== finalResponse) { | |
| finalResponse = currentResponse; | |
| await sleep(1000); | |
| continue; | |
| } | |
| return finalResponse; | |
| } | |
| return null; | |
| } | |
| // Launch the browser and open a new blank page | |
| const browser = await puppeteer.launch({ | |
| headless: false, | |
| userDataDir: "./user_data" | |
| }); | |
| const page = await browser.newPage(); | |
| // Navigate to chat page | |
| console.log("Navigating to chat page"); | |
| await page.goto(CHAT_PAGE); | |
| await page.setViewport({width: 1080, height: 1024}); | |
| console.log("Waiting for redirect"); | |
| if (page.url() === SIGNIN_PAGE) { | |
| console.log("Redirected to signin page"); | |
| // await page.goto(SIGNIN_PAGE); | |
| await page.locator('input[placeholder="Phone number / email address"]').fill(SIGN_IN_MAIL); | |
| await page.locator('input[placeholder="Password"]').fill(SIGN_IN_PASSWORD); | |
| await page.locator('.ds-checkbox').click(); | |
| await page.locator('.ds-sign-up-form__register-button').click(); | |
| console.log("Signed in and redirected to chat page"); | |
| } | |
| // We should be at the chat page now, turn on the buttons | |
| await turnOnButtons(); | |
| await page.locator('textarea[id="chat-input"]').fill("Hello, how are you?"); | |
| await page.keyboard.press('Enter'); | |
| const lastResponse = await getLastResponseContent(); | |
| console.log("Last response: ", lastResponse); | |
| await browser.close(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment