Skip to content

[wip] feat: download #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,10 @@ yarn-error.log*
# typescript
*.tsbuildinfo
next-env.d.ts

# Playwright
node_modules/
/test-results/
/playwright-report/
/blob-report/
/playwright/.cache/
26 changes: 26 additions & 0 deletions app/api/authsession/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Browserbase } from "@browserbasehq/sdk";

async function createAuthSession(contextId: string) {
const bb = new Browserbase({ apiKey: process.env.BROWSERBASE_API_KEY! });

const session = await bb.sessions.create({
projectId: process.env.BROWSERBASE_PROJECT_ID!,
browserSettings: {
context: {
id: contextId,
persist: true,
},
// advancedStealth: true, // TODO: need to be enabled by browserbase team.
},
proxies: true,
});

console.log("Session URL: https://browserbase.com/sessions/" + session.id);
return session;
}

export async function GET(request: Request) {
const session = await createAuthSession(process.env.BROWSERBASE_CONTEXT_ID!);
console.log("Session URL: https://browserbase.com/sessions/" + session.id);
return Response.json(session);
}
25 changes: 25 additions & 0 deletions app/api/context/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Browserbase } from "@browserbasehq/sdk";

// CREATE A CONTEXT
async function createContext() {
const projectId = process.env.BROWSERBASE_PROJECT_ID;
const options = {
method: "POST",
headers: {
"X-BB-API-Key": `${process.env.BROWSERBASE_API_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify({ projectId }),
};
const response = await fetch(
`https://api.browserbase.com/v1/contexts`,
options
);
const json = await response.json();
return json;
}

export async function GET(request: Request) {
const context = await createContext();
return Response.json(context);
}
54 changes: 54 additions & 0 deletions app/api/download/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import puppeteer from "puppeteer-core";
import Browserbase from "@browserbasehq/sdk";

export async function GET(request: Request) {
const bb = new Browserbase({ apiKey: process.env.BROWSERBASE_API_KEY! });

const session = await bb.sessions.create({
projectId: process.env.BROWSERBASE_PROJECT_ID!,
browserSettings: {
context: {
id: process.env.BROWSERBASE_CONTEXT_ID!,
persist: true,
},
// advancedStealth: true, // TODO: need to be enabled by browserbase team.
},
proxies: true,
});

const browser = await puppeteer.connect({
browserWSEndpoint: session.connectUrl,
});
const pages = await browser.pages();
const page = pages[0];

const client = await page.createCDPSession();
await client.send("Browser.setDownloadBehavior", {
behavior: "allow",
downloadPath: "downloads",
eventsEnabled: true,
});

await page.goto("https://www.linkedin.com/in/luke-harries", {
waitUntil: "networkidle0",
timeout: 60000,
});

const [downloadPath] = await Promise.all([
// Puppeteer doesn't have a download event, so we need to use the CDP session to listen for the download progress
new Promise<string>((resolve) => {
client.on("Browser.downloadProgress", (event) => {
if (event.state === "completed") {
resolve(event.guid);
}
});
}),
page.locator('div[aria-label="Save to PDF"]').click(),
]);

// Store the session ID to retrieve downloads later
console.log("Download completed. Session ID:", session.id);

await page.close();
await browser.close();
}
Loading