Skip to content

Commit

Permalink
fix: remove browser process creation
Browse files Browse the repository at this point in the history
  • Loading branch information
sosweetham committed May 5, 2024
1 parent a8937fc commit 9dc74de
Showing 1 changed file with 6 additions and 24 deletions.
30 changes: 6 additions & 24 deletions app/src/libs/guilded-scrape.ts
Original file line number Diff line number Diff line change
@@ -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<Blob|Error> = 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'})
Expand All @@ -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')
Expand All @@ -39,15 +32,6 @@ export const guildedUserProfileScrape: (id: string, getElement: 'avatar' | 'bann
export const guildedServerProfileScrape: (id: string, getElement: 'icon' | 'banner') => Promise<Blob | Error> = 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)
Expand All @@ -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')
Expand Down

0 comments on commit 9dc74de

Please sign in to comment.