Skip to content

Commit

Permalink
fix: add scrape error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
sosweetham committed May 5, 2024
1 parent 4a6d7da commit 089e608
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 41 deletions.
3 changes: 3 additions & 0 deletions app/src/controllers/server-banner.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ export const serverBannerController = new Elysia()
return new Response(await streamToBuffer(banner), { headers: { 'Content-Type': 'image/webp' } })
}
const imageBlob = await guildedServerProfileScrape(params.id, 'banner')
if (imageBlob instanceof Error) {
return new Response('User not found', { status: 404 })
}
return new Response(imageBlob, { headers: { 'Content-Type': 'image/webp' } })
}, {
params: t.Object({
Expand Down
3 changes: 3 additions & 0 deletions app/src/controllers/server-icon.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ export const serverIconController = new Elysia()
return res
}
const imageBlob = await guildedServerProfileScrape(params.id, 'icon')
if (imageBlob instanceof Error) {
return new Response('User not found', { status: 404 })
}
const res = new Response(imageBlob, { headers: { 'Content-Type': 'image/webp' } })
return res
}, {
Expand Down
3 changes: 3 additions & 0 deletions app/src/controllers/user-avatar.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ export const userAvatarController = new Elysia()
return res
}
const imageBlob = await guildedUserProfileScrape(params.id, 'avatar')
if (imageBlob instanceof Error) {
return new Response('User not found', { status: 404 })
}
const res = new Response(imageBlob, { headers: { 'Content-Type': 'image/webp' } })
return res
}, {
Expand Down
86 changes: 45 additions & 41 deletions app/src/libs/guilded-scrape.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,54 +3,58 @@ import puppeteer, { Browser } from "puppeteer";

export let browserInstances: Browser[] = []

export const guildedUserProfileScrape = async (id: string, getElement: 'avatar' | 'banner') => {
const getClass = getElement === 'avatar' ? '.UserProfilePictureControl-picture' : '.UserProfileBackground-image'
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()
export const guildedUserProfileScrape: (id: string, getElement: 'avatar' | 'banner') => Promise<Blob|Error> = async (id: string, getElement: 'avatar' | 'banner') => {
try {
await page.goto(`https://www.guilded.gg/profile/${id}`, {waitUntil: 'networkidle0', timeout: 13000})
const getClass = getElement === 'avatar' ? '.UserProfilePictureControl-picture' : '.UserProfileBackground-image'
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', timeout: 13000})
} catch (e) {
throw new Error(`User not found,\nError:\n${e}`)
}
const src = await page.$eval(getClass, (el: any) => el.src)
if (getElement === 'avatar') {
await userAvatarBucket.uploadImage(id, src)
} else if (getElement === 'banner') {
await userBannerBucket.uploadImage(id, src)
}
await page.close()
await browser.close()
browserInstances.pop()
return await (await fetch(src)).blob()
} catch (e) {
throw new Error(`User not found,\nError:\n${e}`)
return new Error('User not found')
}
const src = await page.$eval(getClass, (el: any) => el.src)
if (getElement === 'avatar') {
await userAvatarBucket.uploadImage(id, src)
} else if (getElement === 'banner') {
await userBannerBucket.uploadImage(id, src)
}
await page.close()
await browser.close()
browserInstances.pop()
return await (await fetch(src)).blob()
}

export const guildedServerProfileScrape = async (id: string, getElement: 'icon' | 'banner') => {
const getClass = getElement === 'icon' ? '.TeamPlaqueV2-profile-pic' : '.TeamOverviewBanner-banner.TeamPageBanner-overview-banner'
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()
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'
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', timeout: 13000})
const src = await page.$eval(getClass, (el: any) => el.src)
if (getElement === 'icon') {
await serverIconBucket.uploadImage(id, src)
} else if (getElement === 'banner') {
await serverBannerBucket.uploadImage(id, src)
}
await page.close()
await browser.close()
browserInstances.pop()
return await (await fetch(src)).blob()
} catch (e) {
throw new Error(`Server not found,\nError:\n${e}`)
}
const src = await page.$eval(getClass, (el: any) => el.src)
if (getElement === 'icon') {
await serverIconBucket.uploadImage(id, src)
} else if (getElement === 'banner') {
await serverBannerBucket.uploadImage(id, src)
return new Error('Server not found')
}
await page.close()
await browser.close()
browserInstances.pop()
return await (await fetch(src)).blob()
}

0 comments on commit 089e608

Please sign in to comment.