UNPKG

filehost-meta

Version:

Fetch basic file information from download links

70 lines (53 loc) 1.75 kB
const { connectBrowser } = require('../browser') const File = require('../classes/File') exports.domains = ['workupload.com'] exports.get = async (url, proxy) => { let cleanup try { const connectOptions = { turnstile: true } if (proxy) { const parsed = new URL(proxy) connectOptions.proxy = { host: parsed.hostname, port: parsed.port, username: parsed.username || undefined, password: parsed.password || undefined, } } const result = await connectBrowser(connectOptions) cleanup = result.cleanup const page = result.page await page.goto(url, { waitUntil: 'networkidle2', timeout: 60000 }) // The site may show a robot/security check page that solves a proof-of-work let onSecurityPage = false try { onSecurityPage = await page.evaluate(() => typeof window.captchaCallback !== 'undefined') } catch { onSecurityPage = true } if (onSecurityPage) { await page.waitForNavigation({ waitUntil: 'networkidle2', timeout: 60000 }) } const pageContent = await page.content() if (pageContent.includes('file does not exist')) { throw new Error('File does not exist') } const fileData = await page.evaluate(() => { const el = document.querySelector('#notice table') if (!el) return null const name = el.querySelectorAll('td')[1]?.innerText const size = el.querySelectorAll('td')[3]?.innerText.split(' ')[0] return { name, size } }) if (!fileData) { throw new Error('Could not find file data on page') } return [ new File({ name: fileData.name, size: fileData.size }) ] } finally { if (cleanup) { await cleanup() } } }