ubercrawler/index.js

74 lines
1.9 KiB
JavaScript
Raw Normal View History

2023-08-30 22:23:10 +00:00
import puppeteer from "puppeteer-core"
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms))
}
2023-08-30 23:02:26 +00:00
const settlePromises = async (promiseArray) => {
const settledArray = await Promise.allSettled(promiseArray)
let errors = settledArray.filter((resolved) => resolved.reason)
if (errors.length) {
throw errors
}
return settledArray.map((resolved) => resolved.value)
}
2023-08-30 22:23:10 +00:00
async function main() {
try {
const browser = await puppeteer.launch({
headless: false,
ignoreHTTPSErrors: true,
2023-08-30 23:02:26 +00:00
executablePath: "/usr/bin/chromium",
2023-08-30 22:23:10 +00:00
})
console.log("Opened Browser")
const page = await browser.newPage()
console.log("Opened Page")
await page.setRequestInterception(true)
let usefulRequestHeaders = {}
page.on("request", (request) => {
let url = request.url()
console.log("request url", url)
2023-08-30 23:02:26 +00:00
if (url.includes("getWebActivityFeed")) {
2023-08-30 22:23:10 +00:00
usefulRequestHeaders = { ...request.headers() }
2023-08-30 23:02:26 +00:00
console.log("hello!", usefulRequestHeaders)
2023-08-30 22:23:10 +00:00
}
request.continue()
})
2023-08-30 23:02:26 +00:00
await page.goto("https://drivers.uber.com/earnings/activities")
2023-08-30 22:23:10 +00:00
console.log("Went to Page")
2023-08-30 23:02:26 +00:00
await page.waitForSelector('input[aria-label="Select a date range."]')
console.log("We're in.", usefulRequestHeaders)
2023-08-30 22:23:10 +00:00
for (let i = 0; i < 10; i++) {
2023-08-30 23:02:26 +00:00
await sleep(500)
if (usefulRequestHeaders["content-type"]) {
console.log("success")
break
}
if (i == 9) {
throw "fail"
2023-08-30 22:23:10 +00:00
}
}
console.log("after sleep loop")
2023-08-30 23:02:26 +00:00
const response = await fetch("https://www.google.com", {
method: "POST",
headers: usefulRequestHeaders,
body: JSON.stringify({ Request }),
})
2023-08-30 22:23:10 +00:00
// if (response.ok) {
// console.log(await response.json());
// }
} catch (err) {
2023-08-30 23:02:26 +00:00
console.error("Critical failure", err)
} finally {
await browser.close()
2023-08-30 22:23:10 +00:00
}
}
main()