CoddityTeam / movaicode

Concours mensuel du Pire Développeur de France
123 stars 10 forks source link

LMGTFY.ts #280

Open CGjupoulton opened 1 year ago

CGjupoulton commented 1 year ago

What is LMGTFY?

Ben comme n'importe qui avec un ordi je vais demander à Google, je ne suis pas fou

Usage

step 1

npm init
npm i puppeteer
npm i ts-node

step 2

passez 5 heures à régler package.json et votre installation nodejs

step 3

import puppeteer from 'puppeteer'

// NOTE insérez la date ici, puis lancez "ts-node-esm main.ts"
const date = '01/05/1991'
const debug = false;

(async () => {
  const browser = await puppeteer.launch()
  const page = await browser.newPage()

  await page.goto('https://google.fr/')

  // Set screen size
  await page.setViewport({ width: 1080, height: 1024 })

  // Refuse cookies
  if (debug) { await page.screenshot({ path: 'googling.jpg' }) }
  const [button] = await page.$x("//button[contains(., 'Reject all')]")
  if (button) {
    await (button as any).click()
  } else {
    const [buttonFR] = await page.$x("//button[contains(., 'Tout refuser')]")
    await (buttonFR as any).click()
  }

  // Type into search box
  await page.type('input', 'Signe astrologique ' + date)
  if (debug) { await page.screenshot({ path: 'typed.jpg' }) }

  // Submit
  await page.keyboard.press('Enter')

  // Wait and click on first result
  const searchResultSelector = '[role="heading"]'
  await page.waitForSelector(searchResultSelector)
  if (debug) { await page.screenshot({ path: 'result.jpg' }) }

  const textSelector = await page.waitForSelector(
    searchResultSelector
  )
  const fullText = await textSelector.evaluate(el => el.textContent)

  // Print the signe astrologique :)
  console.log(fullText)

  await browser.close()
})()

Examples

Balance

input: date = 01/05/1991

output: Signe Astro de la Balance - Dates et subtilités

Verseau

input: date = 13/02/1992

output: Le signe astrologique du Verseau, de symbole ♒︎, est lié aux personnes nées entre le 21 janvier et le 18 février (compris) en astrologie tropicale.

Scorpion

input: date = 21/11/1993

output: Né entre le 24 octobre et le 22 novembre, vous êtes du signe du Scorpion.