1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import puppeteer from "https://deno.land/x/puppeteer@16.2.0/mod.ts";
import { OpenAI } from "https://esm.town/v/std/openai?v=4";
import { Browserbase } from "npm:@browserbasehq/sdk";
const browserbase = new Browserbase();
const session = await browserbase.createSession();
const browser = await puppeteer.connect({
browserWSEndpoint: browserbase.getConnectURL({ sessionId: session.id, proxy: true }),
});
const page = (await browser.pages())[0];
// Navigate to Google
await page.goto("https://www.google.com");
// Type into the search box
await page.type("input", "james blake concert");
// Press Enter to submit the search
await page.keyboard.press("Enter");
// Wait for the search results page to load
await page.waitForSelector("#search");
await page.click(".PBBEhf");
// get html contents of g-accordion html element
const html = await page.$eval("g-accordion", (el) => el.innerHTML);
console.log(html);
// ask chat gpt for list of concert dates
const openai = new OpenAI();
const completion = await openai.chat.completions.create({
messages: [
{ role: "system", content: "Return concert dates as JSON array. No code fences." },
{ role: "user", content: html },
],
model: "gpt-3.5-turbo",
max_tokens: 3000,
});
console.log(completion.choices[0].message.content);
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
July 28, 2024