diff --git a/app/src/libs/guilded-scrape.ts b/app/src/libs/guilded-scrape.ts index a8f12a4..72322b9 100644 --- a/app/src/libs/guilded-scrape.ts +++ b/app/src/libs/guilded-scrape.ts @@ -1,20 +1,15 @@ import { userAvatarBucket, userBannerBucket, serverBannerBucket, serverIconBucket } from "./minio" -import puppeteer, { Browser } from "puppeteer"; +import puppeteer from "puppeteer"; -export let browserInstances: Browser[] = [] +const browser = await puppeteer.launch({ + headless: true, + args: ['--no-sandbox', '--disable-setuid-sandbox'], + executablePath: "/usr/bin/chromium", +}) export const guildedUserProfileScrape: (id: string, getElement: 'avatar' | 'banner') => Promise = async (id: string, getElement: 'avatar' | 'banner') => { try { const getClass = getElement === 'avatar' ? '.UserProfilePictureControl-picture' : '.UserProfileBackground-image' - if (browserInstances.length > 5) { - throw new Error('Too many requests') - } - const browser = await puppeteer.launch({ - headless: true, - args: ['--no-sandbox', '--disable-setuid-sandbox'], - executablePath: "/usr/bin/chromium", - }) - browserInstances.push(browser) const page = await browser.newPage() try { await page.goto(`https://www.guilded.gg/profile/${id}`, {waitUntil: 'networkidle0'}) @@ -28,8 +23,6 @@ export const guildedUserProfileScrape: (id: string, getElement: 'avatar' | 'bann await userBannerBucket.uploadImage(id, src) } await page.close() - await browser.close() - browserInstances.pop() return await (await fetch(src)).blob() } catch (e) { return new Error('User not found') @@ -39,15 +32,6 @@ export const guildedUserProfileScrape: (id: string, getElement: 'avatar' | 'bann export const guildedServerProfileScrape: (id: string, getElement: 'icon' | 'banner') => Promise = async (id, getElement) => { try { const getClass = getElement === 'icon' ? '.TeamPlaqueV2-profile-pic' : '.TeamOverviewBanner-banner.TeamPageBanner-overview-banner' - if (browserInstances.length > 5) { - throw new Error('Too many requests') - } - const browser = await puppeteer.launch({ - headless: true, - args: ['--no-sandbox', '--disable-setuid-sandbox'], - executablePath: "/usr/bin/chromium", - }) - browserInstances.push(browser) const page = await browser.newPage() await page.goto(`https://www.guilded.gg/teams/${id}/overview`, {waitUntil: 'networkidle0'}) const src = await page.$eval(getClass, (el: any) => el.src) @@ -57,8 +41,6 @@ export const guildedServerProfileScrape: (id: string, getElement: 'icon' | 'bann await serverBannerBucket.uploadImage(id, src) } await page.close() - await browser.close() - browserInstances.pop() return await (await fetch(src)).blob() } catch (e) { return new Error('Server not found')