This commit is contained in:
2026-11-12 09:32:23 +01:00
commit 53f1896d05
27 changed files with 6336 additions and 0 deletions

91
lib/itch.js Normal file
View File

@@ -0,0 +1,91 @@
// itch.io: which projects are published. Reads the public profile page (settings
// "itchProfile", e.g. https://sn09.itch.io) - no API key, no login - and matches the
// game titles/links to project names. A project can override it in .dashboard.json:
// "itch": "" automatic (match by name)
// "itch": "<url>" this page (manual, also for games not listed on the profile)
// "itch": "-" not on itch.io (switch the automatic match off)
const settings = require('./settings')
const TTL_MS = 60 * 60 * 1000
const state = { profile: '', games: [], at: 0, error: null }
let pending = null
const norm = (s) => String(s).toLowerCase().replace(/[^a-z0-9]/g, '')
function profileUrl() {
return (settings.get().itchProfile ?? '').replace(/\/+$/, '')
}
// <a ... href="https://x.itch.io/slug" class="title game_link">Title</a>
function parse(html, profile) {
const games = []
const seen = new Set()
const re = /<a\b[^>]*\bclass="[^"]*\btitle game_link\b[^"]*"[^>]*>([^<]*)<\/a>/g
for (const m of html.matchAll(re)) {
const href = m[0].match(/\bhref="([^"]+)"/)?.[1]
if (!href || seen.has(href)) continue
seen.add(href)
const title = m[1].replace(/&amp;/g, '&').replace(/&#39;/g, "'").replace(/&quot;/g, '"').trim()
games.push({ title, url: href, slug: href.replace(profile + '/', '').replace(/\/.*$/, '') })
}
return games
}
async function refresh() {
const profile = profileUrl()
if (!profile) { Object.assign(state, { profile: '', games: [], at: Date.now(), error: null }); return state }
if (pending) return pending
pending = (async () => {
try {
const res = await fetch(profile + '/', { headers: { 'User-Agent': 'ProjectDashboard' }, signal: AbortSignal.timeout(10000) })
if (!res.ok) throw new Error(`itch.io antwortet ${res.status}`)
const games = parse(await res.text(), profile)
Object.assign(state, { profile, games, at: Date.now(), error: null })
} catch (e) {
// keep the last good list; retry after a few minutes instead of an hour
Object.assign(state, { profile, at: Date.now() - TTL_MS + 5 * 60 * 1000, error: String(e.message ?? e) })
} finally { pending = null }
return state
})()
return pending
}
// For the project list: fresh enough data, but never hold the page up for long.
async function ensure(maxWaitMs = 3000) {
if (state.profile !== profileUrl() || Date.now() - state.at > TTL_MS) {
const p = refresh()
if (!state.games.length) await Promise.race([p, new Promise((r) => setTimeout(r, maxWaitMs))])
}
return state
}
// Same name, or the itch title is the project name plus a short suffix
// ("DataOrganizer" -> "Data Organizer Ultra"). Not the other way round, so
// "CatMaus 2" does not take over "CatMaus".
function autoMatch(name) {
const pn = norm(name)
if (!pn) return null
return state.games.find((g) => norm(g.title) === pn || norm(g.slug) === pn)
?? state.games.find((g) => pn.length >= 5 && norm(g.title).startsWith(pn) && norm(g.title).length - pn.length <= 6)
?? null
}
// -> { url, title, source: 'manual' | 'auto' } or null (not published / switched off)
function forProject(name, manual) {
const m = (manual ?? '').trim()
if (m === '-') return null
if (/^https?:\/\//i.test(m)) {
const g = state.games.find((x) => x.url.replace(/\/+$/, '') === m.replace(/\/+$/, ''))
return { url: m, title: g?.title ?? '', source: 'manual' }
}
const g = autoMatch(name)
return g ? { url: g.url, title: g.title, source: 'auto' } : null
}
// Games on the profile that no project claims (to spot a missing manual link).
function unmatched(claimedUrls) {
const set = new Set(claimedUrls.map((u) => u.replace(/\/+$/, '')))
return state.games.filter((g) => !set.has(g.url.replace(/\/+$/, '')))
}
module.exports = { ensure, refresh, forProject, unmatched, state, parse }