const fs = require('fs'); const { getAvatar, preparePost, encodeString, getActionDescription, getMediaUrl } = require("./utils"); require('dotenv').config(); const KEY = process.env.ITCH_API_KEY; const ITCH_SVG = 'Itch.io'; const TAGS_MAP = { 40801: "lifeisstrange", 1826237: "incremental", 969362: "incremental", 938153: "incremental", 2074815: "incremental", 730177: "incremental", 62179: "incremental", 814897: "incremental", 993566: "incremental" }; (async () => { const url = `https://itch.io/api/1/${KEY}/my-games`; const resp = await fetch(url).then(r => r.text()); let response; try { response = JSON.parse(resp); } catch (err) { console.error("Unexpected response from itch:", url, resp, err); process.exit(0); } if (!response.games?.length) { console.error("No games received from itch:", url, resp); process.exit(0); } console.log(`Checking ${response.games.length} games`); for await (const game of response.games) { if (!game.published) continue; let timestamp = new Date(game.published_at).getTime(); const tag = TAGS_MAP[game.id] ?? "gaming"; let embed = ''; if (game.embed) { const uploadsResponse = await fetch(`https://itch.io/api/1/${KEY}/game/${game.id}/uploads`).then(r => r.text()); embed = await getEmbed(game, uploadsResponse); } let path; for (timestamp--; path == null || fs.existsSync(path);) { path = `./site/article/${getTimestampPath(++timestamp)}`; } fs.mkdirSync(path, { recursive: true }); const fd = fs.openSync(path + "/index.md", "w+"); fs.writeSync(fd, preparePost(`--- kind: article title: ${encodeString(game.title)} published: ${timestamp} next: false prev: false tags: [${encodeString(tag, 2)}] ---
${getActionDescription({ timestamp, kind: "article" })}
${await getAvatar({ timestamp, tags: [tag], syndications: [{ type: ITCH_SVG, url: game.url }], kind: 'article' })}

${game.title}

${game.short_text}
${embed}
`)); fs.closeSync(fd); console.log(`Created post for "${game.title}": ${path}/index.md`); } })(); async function getEmbed(game, resp) { // Skip games that don't fit on the paper // TODO hsve play button that full screens the game if ([993566, 40801, 184114, 162503, 129905, 87387, 79940, 62179, 60029, 50055].includes(game.id)) { return ""; } let response; try { response = JSON.parse(resp); } catch (err) { console.error("Unexpected response from itch:", game.title, resp, err); return ""; } if (!response.uploads?.length ?? 0 < 1) { console.error("No uploads received for game:", game.title, resp); return ""; } const upload = response.uploads?.filter(up => up.type === "html")[0]; if (!upload) { console.error("No html uploads received for game:", game.title, resp); return ""; } const dist = upload.filename === "dist.zip" ? '/dist' : ''; const src = `https://html-classic.itch.zone/html/${upload.id}${dist}/index.html`; const height = Math.ceil(game.embed.height / 30 + 1) * 30; const width = height * game.embed.width / game.embed.height; return ``; }