Advertisement
WILDAN_IZZUDIN

[JS] C.AI GET ID FROM CHARACTER

May 11th, 2024
820
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const cheerio = require('cheerio')
  2. const puppeteer = require('puppeteer')
  3. const iPhone = puppeteer.devices['iPhone 12']
  4.  
  5. module.exports = async q => {
  6.    const browser = await puppeteer.launch({
  7.       headless: 'new',
  8.       args: [
  9.          "--fast-start",
  10.          "--disable-extensions",
  11.          "--no-sandbox",
  12.          "--disable-setuid-sandbox",
  13.          "--no-gpu",
  14.          "--disable-background-timer-throttling",
  15.          "--disable-renderer-backgrounding",
  16.          "--override-plugin-power-saver-for-testing=never",
  17.          "--disable-extensions-http-throttling",
  18.       ]
  19.    })
  20.    try {
  21.       const page = await browser.newPage()
  22.       await page.emulate(iPhone)
  23.       await page.setUserAgent('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3');
  24.       await page.setExtraHTTPHeaders({
  25.          cookie: 'your_cookie_here'      
  26.       })
  27.       await page.goto('https://character.ai/', {
  28.          waitUntil: 'networkidle2',
  29.          timeout: 0
  30.       })
  31.       await page.type('input[placeholder="Search for Characters"]', q)
  32.       await page.keyboard.press('Enter')
  33.       await page.waitForTimeout(1200)
  34.       const html = await page.content()
  35.       await browser.close()
  36.       const $ = cheerio.load(html)
  37.       const data = []
  38.       $('div[class="flex flex-col sm:gap-3 w-full h-full overflow-y-auto overflow-x-hidden"]').find('a[class="group flex w-full flex-row justify-between hover:bg-surface-elevation-3 hover:rounded-spacing-m p-2 items-center"]').each((i,e) => data.push($(e).attr('href').split`/`[2]))
  39.       if (data.length < 1) return ({
  40.          status: false,
  41.          msg: '[Character.AI]: I can\'t find the character!'
  42.       })
  43.       const result = data.splice(0, 20)
  44.       return ({
  45.          status: true,
  46.          data: result
  47.       })
  48.    } catch (e) {
  49.       await browser.close()
  50.       return ({
  51.          status: false,
  52.          msg: '[Character.AI]: ' + e.message
  53.       })
  54.    }
  55. }
  56.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement