A games launcher for GOG, Amazon and Epic Games for Linux, Windows and macOS.
TypeScript amazon-primeelectronepic-games-launcherepic-games-storeepic-storegaminggoggog-galaxyheroic-games-launcherlinuxplay-gamesprotonsteamwine
import { GameScreenshot } from 'common/types'
export const MAX_SCREENSHOTS = 12
const GOG_FULL_FORMATTER = '1600'
const GOG_THUMBNAIL_FORMATTER = 'product_card_screenshot_748'
interface ScreenshotCandidate {
full?: unknown
thumbnail?: unknown
}
export function isSafeImageUrl(url: unknown): url is string {
if (typeof url !== 'string') return false
try {
return new URL(url).protocol === 'https:'
} catch {
return false
}
}
export function buildScreenshots(
candidates: ScreenshotCandidate[]
): GameScreenshot[] {
const seen = new Set<string>()
const screenshots: GameScreenshot[] = []
for (const { full, thumbnail } of candidates) {
if (!isSafeImageUrl(full) || seen.has(full)) continue
seen.add(full)
screenshots.push(isSafeImageUrl(thumbnail) ? { full, thumbnail } : { full })
if (screenshots.length === MAX_SCREENSHOTS) break
}
return screenshots
}
function asArray(value: unknown): unknown[] {
return Array.isArray(value) ? value : []
}
function asRecord(value: unknown): Record<string, unknown> {
return value && typeof value === 'object'
? (value as Record<string, unknown>)
: {}
}
export function getGogScreenshots(gamesData: unknown): GameScreenshot[] {
const embedded = asRecord(asRecord(gamesData)._embedded)
return buildScreenshots(
asArray(embedded.screenshots).map((entry) => {
const href = asRecord(asRecord(asRecord(entry)._links).self).href
if (typeof href !== 'string' || !href.includes('{formatter}')) return {}
return {
full: href.replace('{formatter}', GOG_FULL_FORMATTER),
thumbnail: href.replace('{formatter}', GOG_THUMBNAIL_FORMATTER)
}
})
)
}
export function getEpicScreenshots(carousel: unknown): GameScreenshot[] {
const items = asArray(asRecord(carousel).items).map(asRecord)
const withImage = items.filter(
(item) => typeof asRecord(item.image).src === 'string'
)
const stills = withImage.filter((item) => !asRecord(item.video).recipes)
const source = stills.length > 0 ? stills : withImage
return buildScreenshots(
source.map((item) => ({ full: asRecord(item.image).src }))
)
}
export function getSteamScreenshots(screenshots: unknown): GameScreenshot[] {
return buildScreenshots(
asArray(screenshots).map((entry) => {
const { path_full, path_thumbnail } = asRecord(entry)
return { full: path_full, thumbnail: path_thumbnail }
})
)
}
export function getUrlScreenshots(urls: unknown): GameScreenshot[] {
return buildScreenshots(asArray(urls).map((full) => ({ full })))
}