import fs from "fs/promises";
const API_URL = "https://api.webunlocker.gologin.com/api/parsing/v1";
const API_KEY = "YOUR_API_KEY";
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
// Step 1: Create task
const createRes = await fetch(`${API_URL}/tasks?async=true`, {
method: "POST",
headers: { "X-API-Key": API_KEY, "Content-Type": "application/json" },
body: JSON.stringify({ url: "https://example.com" }),
});
const { task_id } = await createRes.json();
console.log(`Task created: ${task_id}`);
// Step 2: Poll for completion
while (true) {
const { status } = await fetch(`${API_URL}/tasks/${task_id}/status`, {
headers: { "X-API-Key": API_KEY },
}).then((r) => r.json());
console.log(`Status: ${status}`);
if (status === "completed") break;
if (status === "failed" || status === "cancelled") {
throw new Error(`Task ${status}`);
}
await sleep(5000);
}
// Step 3: Download results
const html = await fetch(`${API_URL}/results/${task_id}/html`, {
headers: { "X-API-Key": API_KEY },
}).then((r) => r.text());
await fs.writeFile("page.html", html, "utf-8");
const screenshot = await fetch(`${API_URL}/results/${task_id}/screenshot`, {
headers: { "X-API-Key": API_KEY },
}).then((r) => r.arrayBuffer());
await fs.writeFile("screenshot.png", Buffer.from(screenshot));
console.log("Done.");