|
| 1 | +// GamerPower URL resolution module |
| 2 | +// Resolves giveaway URLs to store URLs (Epic Games, Steam, etc.) |
| 3 | +// Transparently handles caching and browser-based resolution |
| 4 | + |
| 5 | +import { chromium } from 'patchright'; |
| 6 | +import { jsonDb, datetime, handleSIGINT } from './util.js'; |
| 7 | +import { cfg } from './config.js'; |
| 8 | + |
| 9 | +const gpCache = await jsonDb('gp-cache.json', {}); |
| 10 | + |
| 11 | +/** |
| 12 | + * Fetches giveaways from a GamerPower API URL |
| 13 | + * @param {string} apiUrl - The GamerPower API URL (e.g., https://www.gamerpower.com/api/giveaways?platform=steam&type=game) |
| 14 | + * @returns {Promise<Array>} - Array of giveaway objects from the API |
| 15 | + */ |
| 16 | +async function fetchGamerPowerGiveaways(apiUrl) { |
| 17 | + console.log('[GamerPower] Fetching giveaways from API...'); |
| 18 | + const response = await fetch(apiUrl); |
| 19 | + |
| 20 | + if (!response.ok) { |
| 21 | + throw new Error(`Failed to fetch GamerPower data: ${response.statusText}`); |
| 22 | + } |
| 23 | + |
| 24 | + const items = await response.json(); |
| 25 | + console.log(`[GamerPower] Fetched ${items.length} giveaways`); |
| 26 | + return items; |
| 27 | +} |
| 28 | + |
| 29 | +/** |
| 30 | + * Checks if a giveaway URL is already cached |
| 31 | + * @param {string} giveawayUrl - The GamerPower open_giveaway_url |
| 32 | + * @returns {Object|null} - Cached entry or null if not cached |
| 33 | + */ |
| 34 | +function getCachedUrl(giveawayUrl) { |
| 35 | + return gpCache.data[giveawayUrl] || null; |
| 36 | +} |
| 37 | + |
| 38 | +/** |
| 39 | + * Caches a resolved giveaway URL |
| 40 | + * @param {string} giveawayUrl - The GamerPower open_giveaway_url |
| 41 | + * @param {string} storeUrl - The resolved store URL |
| 42 | + */ |
| 43 | +function cacheUrl(giveawayUrl, storeUrl) { |
| 44 | + gpCache.data[giveawayUrl] = { |
| 45 | + storeUrl, |
| 46 | + time: datetime() |
| 47 | + }; |
| 48 | + console.log(`[GamerPower] Cached: ${giveawayUrl} -> ${storeUrl}`); |
| 49 | +} |
| 50 | + |
| 51 | +/** |
| 52 | + * Resolves giveaway URLs that aren't cached yet using a browser |
| 53 | + * @param {Array} urlsToResolve - Array of giveaway URLs to resolve |
| 54 | + * @returns {Promise<Object>} - Map of giveawayUrl -> storeUrl |
| 55 | + */ |
| 56 | +async function resolveUrlsWithBrowser(urlsToResolve) { |
| 57 | + if (urlsToResolve.length === 0) { |
| 58 | + return {}; |
| 59 | + } |
| 60 | + |
| 61 | + console.log(`[GamerPower] Resolving ${urlsToResolve.length} URLs with browser...`); |
| 62 | + |
| 63 | + const context = await chromium.launchPersistentContext(cfg.dir.browser, { |
| 64 | + headless: cfg.headless, |
| 65 | + viewport: { width: cfg.width, height: cfg.height }, |
| 66 | + locale: 'en-US', |
| 67 | + handleSIGINT: false, |
| 68 | + args: ['--hide-crash-restore-bubble'], |
| 69 | + }); |
| 70 | + |
| 71 | + handleSIGINT(context); |
| 72 | + |
| 73 | + const page = context.pages().length ? context.pages()[0] : await context.newPage(); |
| 74 | + |
| 75 | + const resolved = {}; |
| 76 | + |
| 77 | + try { |
| 78 | + for (const giveawayUrl of urlsToResolve) { |
| 79 | + console.log(`[GamerPower] Resolving: ${giveawayUrl}`); |
| 80 | + await page.goto(giveawayUrl, { waitUntil: 'domcontentloaded' }); |
| 81 | + const storeUrl = page.url(); |
| 82 | + |
| 83 | + cacheUrl(giveawayUrl, storeUrl); |
| 84 | + resolved[giveawayUrl] = storeUrl; |
| 85 | + } |
| 86 | + |
| 87 | + await gpCache.write(); |
| 88 | + } finally { |
| 89 | + await context.close(); |
| 90 | + } |
| 91 | + |
| 92 | + return resolved; |
| 93 | +} |
| 94 | + |
| 95 | +/** |
| 96 | + * Main function: Fetches giveaways from GamerPower API and returns resolved store URLs |
| 97 | + * |
| 98 | + * This function: |
| 99 | + * 1. Fetches giveaways from the API (no browser needed) |
| 100 | + * 2. Returns cached store URLs immediately for known giveaways |
| 101 | + * 3. Opens a browser to resolve any new giveaway URLs (transparent to caller) |
| 102 | + * 4. Returns a list of { giveawayUrl, storeUrl } objects |
| 103 | + * |
| 104 | + * @param {string} apiUrl - The GamerPower API URL |
| 105 | + * @returns {Promise<Array<{giveawayUrl: string, storeUrl: string}>>} - Array of resolved URLs |
| 106 | + */ |
| 107 | +export async function gpUrlToStoreUrls(apiUrl) { |
| 108 | + const giveaways = await fetchGamerPowerGiveaways(apiUrl); |
| 109 | + |
| 110 | + const cachedResults = []; |
| 111 | + const urlsToResolve = []; |
| 112 | + |
| 113 | + for (const giveaway of giveaways) { |
| 114 | + const giveawayUrl = giveaway.open_giveaway_url; |
| 115 | + const cached = getCachedUrl(giveawayUrl); |
| 116 | + |
| 117 | + if (cached) { |
| 118 | + cachedResults.push({ |
| 119 | + giveawayUrl, |
| 120 | + storeUrl: cached.storeUrl, |
| 121 | + title: giveaway.title |
| 122 | + }); |
| 123 | + } else { |
| 124 | + urlsToResolve.push(giveawayUrl); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + console.log(`[GamerPower] ${cachedResults.length} cached, ${urlsToResolve.length} need resolution`); |
| 129 | + |
| 130 | + // Resolve any uncached URLs with browser |
| 131 | + const newlyResolved = await resolveUrlsWithBrowser(urlsToResolve); |
| 132 | + |
| 133 | + // Combine cached and newly resolved |
| 134 | + const allResults = [...cachedResults]; |
| 135 | + |
| 136 | + for (const giveaway of giveaways) { |
| 137 | + const giveawayUrl = giveaway.open_giveaway_url; |
| 138 | + if (newlyResolved[giveawayUrl]) { |
| 139 | + allResults.push({ |
| 140 | + giveawayUrl, |
| 141 | + storeUrl: newlyResolved[giveawayUrl], |
| 142 | + title: giveaway.title |
| 143 | + }); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + return allResults; |
| 148 | +} |
0 commit comments