-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsteam.js
More file actions
463 lines (375 loc) · 15.5 KB
/
Copy pathsteam.js
File metadata and controls
463 lines (375 loc) · 15.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
import * as cheerio from 'cheerio';
// Steam discovery for Game Finder: the best-selling Early Access games are
// studios with real momentum and an active community worth tracking. Steam has
// no clean "early access success" API, so we use the storefront search filtered
// to the Early Access tag (493), ordered by top sellers — one request is the
// discovery primitive. A candidate's rank is its position in that list; the
// developer/release-date label is fetched separately via appdetails, but only
// for the games that actually make the final list (see gameFinder.js).
//
// The Early Access *tag* (tags=493) is used rather than the category2=70 genre
// filter: the json/infinite search endpoint silently ignores category2 and
// returns general top sellers, while the tag filter correctly restricts to
// Early Access titles. The search returns rows as an HTML blob (results_html),
// not a structured list, so app id + title are parsed out with a regex.
const SEARCH_URL = 'https://store.steampowered.com/search/results/';
const APPDETAILS_URL = 'https://store.steampowered.com/api/appdetails';
const CURRENT_PLAYERS_URL = 'https://api.steampowered.com/ISteamUserStats/GetNumberOfCurrentPlayers/v1/';
export const APP_URL = 'https://store.steampowered.com/app';
const SEARCH_COUNT = 100;
const HTML_ENTITIES = {
''': '\'',
'&': '&',
'>': '>',
'<': '<',
'"': '"',
};
const decodeEntities = function decodeEntities ( value ) {
return value.replace( /&(?:#39|amp|gt|lt|quot);/g, ( match ) => {
return HTML_ENTITIES[ match ] || match;
} );
};
// Extract { id, name } pairs from the search endpoint's results_html. Each row
// carries data-ds-appid and a <span class="title"> with the game name.
const parseRows = function parseRows ( html ) {
const rowPattern = /data-ds-appid="(\d+)"[\s\S]*?<span class="title">([^<]+)<\/span>/g;
const rows = [];
let match = rowPattern.exec( html );
while ( match ) {
rows.push( {
id: match[ 1 ],
name: decodeEntities( match[ 2 ].trim() ),
} );
match = rowPattern.exec( html );
}
return rows;
};
// Early Access top sellers, in chart order (index 0 = best seller). Returns
// [{ id, name }]; returns [] on any upstream failure so the scan still yields
// Twitch results. Enrichment (developer/date) is deferred to enrich() below.
export const search = async function search () {
try {
const params = new URLSearchParams( {
tags: '493',
filter: 'topsellers',
json: '1',
count: String( SEARCH_COUNT ),
infinite: '1',
} );
const response = await fetch( `${ SEARCH_URL }?${ params.toString() }` );
if ( !response.ok ) {
throw new Error( `search returned ${ response.status }` );
}
const body = await response.json();
return parseRows( body.results_html || '' );
} catch ( searchError ) {
console.error( `[steam] search failed: ${ searchError.message }` );
return [];
}
};
// Developer + release date for one app via the public appdetails endpoint.
// Returns { developer, released, releasedAt }: `released` is Steam's display
// string ("6 Dec, 2024"), `releasedAt` its parsed epoch-ms for sorting (null
// when the string is a placeholder like "Coming soon"). Any field may be null,
// and on failure all are — enrichment is a nice-to-have, never a gate.
export const enrich = async function enrich ( appId ) {
const result = {
developer: null,
released: null,
releasedAt: null,
};
try {
const params = new URLSearchParams( {
appids: appId,
filters: 'basic,developers,release_date',
} );
const response = await fetch( `${ APPDETAILS_URL }?${ params.toString() }` );
if ( response.ok ) {
const body = await response.json();
const data = body[ appId ] && body[ appId ].success && body[ appId ].data;
if ( data ) {
result.developer = ( data.developers || [] )[ 0 ] || null;
result.released = ( data.release_date && data.release_date.date ) || null;
const parsed = result.released ? Date.parse( result.released ) : NaN;
result.releasedAt = Number.isNaN( parsed ) ? null : parsed;
}
}
} catch ( detailError ) {
console.error( `[steam] appdetails ${ appId } failed: ${ detailError.message }` );
}
return result;
};
// Extract the community id from whatever the admin pastes: a full community URL
// (steamcommunity.com/games/<id> or /app/<id>), a store URL
// (store.steampowered.com/app/<n>), or a bare slug/number. The community id is
// what /games/<id>/rss/ (the announcements feed) is keyed on — the vanity slug
// for games with a custom community URL, otherwise the numeric app id.
const parseCommunityId = function parseCommunityId ( input ) {
const raw = String( input || '' ).trim();
if ( !raw ) {
return false;
}
const urlMatch = raw.match( /(?:steamcommunity\.com\/(?:games|app)|store\.steampowered\.com\/app)\/([A-Za-z0-9_]+)/i );
if ( urlMatch ) {
return urlMatch[ 1 ];
}
// A bare token (slug or number) — reject anything with path/space noise.
if ( /^[A-Za-z0-9_]+$/.test( raw ) ) {
return raw;
}
return false;
};
// Resolve a Steam source's two ids from one input. The forum scrape needs the
// numeric app id but a custom-URL game's announcements feed only works under its
// vanity slug, and app-id -> vanity isn't reliably resolvable — but the reverse
// is: the community hub page (vanity OR numeric) always links its store app page,
// so we read the numeric app id from there. Also counts announcement-feed items
// so the caller can confirm the feed id actually works. Never throws; unresolved
// fields come back null, matching the other helpers in this file.
export const resolveSteam = async function resolveSteam ( input ) {
const communityId = parseCommunityId( input );
if ( !communityId ) {
return {
announcements: null,
appId: null,
communityId: null,
};
}
const result = {
announcements: null,
appId: null,
communityId,
};
try {
const response = await fetch( `https://steamcommunity.com/games/${ communityId }`, {
headers: {
'user-agent': 'Mozilla/5.0',
},
} );
if ( response.ok ) {
const body = await response.text();
const appMatch = body.match( /store\.steampowered\.com\/app\/(\d+)/ );
if ( appMatch ) {
result.appId = appMatch[ 1 ];
}
}
} catch ( resolveError ) {
console.error( `[steam] resolve ${ communityId } failed: ${ resolveError.message }` );
}
try {
const feedResponse = await fetch( `https://steamcommunity.com/games/${ communityId }/rss/`, {
headers: {
'user-agent': 'Mozilla/5.0',
},
} );
if ( feedResponse.ok ) {
const feedBody = await feedResponse.text();
result.announcements = ( feedBody.match( /<item>/g ) || [] ).length;
}
} catch ( feedError ) {
console.error( `[steam] feed check ${ communityId } failed: ${ feedError.message }` );
}
// A purely numeric input is itself a valid app id for the discussions scrape,
// even when the community page didn't yield one (e.g. a custom-URL game's
// numeric hub is empty). The zero announcement count then signals the caller
// to use the game's vanity community URL for the feed instead.
if ( !result.appId && /^\d+$/.test( communityId ) ) {
result.appId = communityId;
}
return result;
};
const SEARCH_RESULT_LIMIT = 8;
// Search Steam's catalogue by game name for the Game Sources editor's Steam
// picker, so the admin types a name instead of hunting down a numeric app id.
// Uses the community SearchApps AJAX endpoint, which returns clean JSON
// ([{ appid, name, icon }]) — the appid is exactly the numeric forum id and the
// default announcements feed id. Returns [] on any failure, like search() above,
// so the picker degrades to the manual id fields rather than erroring.
export const searchSteamGames = async function searchSteamGames ( term ) {
const query = String( term || '' ).trim();
if ( !query ) {
return [];
}
try {
const response = await fetch(
`https://steamcommunity.com/actions/SearchApps/${ encodeURIComponent( query ) }`,
{
headers: {
'user-agent': 'Mozilla/5.0',
},
},
);
if ( !response.ok ) {
throw new Error( `SearchApps returned ${ response.status }` );
}
const body = await response.json();
if ( !Array.isArray( body ) ) {
return [];
}
return body.slice( 0, SEARCH_RESULT_LIMIT ).map( ( row ) => {
return {
appId: String( row.appid ),
name: row.name,
icon: row.icon || null,
};
} );
} catch ( searchError ) {
console.error( `[steam] game search "${ query }" failed: ${ searchError.message }` );
return [];
}
};
// SteamID64 = this base + the 32-bit account id exposed as `data-miniprofile`.
// (Same conversion the indexer's SteamDiscussions uses.)
const STEAM_ID64_BASE = 76561197960265728n;
// Page-1 discussions lists return ~15 threads; cap the per-thread crawl so a busy
// forum doesn't fan out unbounded, and run a few in parallel so the lookup is snappy.
const DEV_THREAD_LIMIT = 15;
const DEV_FETCH_CONCURRENCY = 5;
const miniProfileToSteamId64 = function miniProfileToSteamId64 ( miniProfile ) {
try {
return ( STEAM_ID64_BASE + BigInt( miniProfile ) ).toString();
} catch {
return false;
}
};
const fetchSteamHtml = function fetchSteamHtml ( url ) {
return fetch( url, {
headers: {
'user-agent': 'Mozilla/5.0',
},
} )
.then( ( response ) => {
if ( !response.ok ) {
throw new Error( `${ url } returned ${ response.status }` );
}
return response.text();
} )
.catch( ( fetchError ) => {
console.error( `[steam] fetch ${ url } failed: ${ fetchError.message }` );
return false;
} );
};
// Pull the developer-badged OP + replies out of one thread page, appending
// { steamId64, name } to `found`. Mirrors the indexer's SteamDiscussions.parseThread
// selectors (Steam's own `commentthread_author_developer` badge + `data-miniprofile`).
const parseThreadDevs = function parseThreadDevs ( threadHtml, found ) {
const $ = cheerio.load( threadHtml );
const push = ( miniProfile, author ) => {
const steamId64 = miniProfileToSteamId64( miniProfile );
if ( !steamId64 ) {
return;
}
found.push( {
name: ( author || '' ).replace( /\s+/g, ' ' ).trim(),
steamId64: steamId64,
} );
};
const $op = $( '.forum_op' ).first();
if ( $op.length && $op.find( '.forum_op_author' ).hasClass( 'commentthread_author_developer' ) ) {
push(
$op.find( '[data-miniprofile]' ).first().attr( 'data-miniprofile' ),
$op.find( '.forum_op_author' ).text()
);
}
$( '.commentthread_comment' ).each( ( index, element ) => {
const $comment = $( element );
if ( !$comment.find( '.commentthread_author_link' ).hasClass( 'commentthread_author_developer' ) ) {
return;
}
push(
$comment.find( '[data-miniprofile]' ).first().attr( 'data-miniprofile' ),
$comment.find( '.commentthread_author_link' ).text()
);
} );
};
// Discover a game's developers for the admin's "find developers on Steam" picker
// by reading Steam's own Developer badge in the discussions forum — the same
// signal the indexer uses to nudge for untracked devs (SteamDiscussions). Crawls
// page 1 of /app/<appId>/discussions/ and the threads it links, collecting badged
// authors as { steamId64, name, profile }. Deduped by steamId64. Never throws;
// returns [] on failure, matching the other helpers here. A studio that only posts
// announcements (not the forum) won't surface — those carry no SteamID.
export const findSteamDevelopers = async function findSteamDevelopers ( appId ) {
const id = String( appId || '' ).trim();
if ( !/^\d+$/.test( id ) ) {
return [];
}
const listHtml = await fetchSteamHtml( `https://steamcommunity.com/app/${ id }/discussions/` );
if ( !listHtml ) {
return [];
}
const $ = cheerio.load( listHtml );
const threadUrls = [];
const seen = new Set();
$( '.forum_topic' ).each( ( index, element ) => {
const url = $( element ).find( 'a.forum_topic_overlay' ).attr( 'href' );
if ( url && !seen.has( url ) ) {
seen.add( url );
threadUrls.push( url );
}
} );
if ( threadUrls.length > DEV_THREAD_LIMIT ) {
console.error( `[steam] dev finder ${ id }: capping ${ threadUrls.length } threads to ${ DEV_THREAD_LIMIT }` );
}
const queue = threadUrls.slice( 0, DEV_THREAD_LIMIT );
const found = [];
const worker = async () => {
while ( queue.length ) {
const url = queue.shift();
const threadHtml = await fetchSteamHtml( url );
if ( threadHtml ) {
try {
parseThreadDevs( threadHtml, found );
} catch ( parseError ) {
console.error( `[steam] dev finder parse ${ url } failed: ${ parseError.message }` );
}
}
}
};
await Promise.all(
Array.from( { length: DEV_FETCH_CONCURRENCY }, () => {
return worker();
} )
);
// Dedupe by SteamID64, keeping the first non-empty display name seen.
const byId = new Map();
for ( const dev of found ) {
const existing = byId.get( dev.steamId64 );
if ( !existing ) {
byId.set( dev.steamId64, dev );
} else if ( !existing.name && dev.name ) {
existing.name = dev.name;
}
}
return [ ...byId.values() ].map( ( dev ) => {
return {
name: dev.name,
profile: `https://steamcommunity.com/profiles/${ dev.steamId64 }`,
steamId64: dev.steamId64,
};
} );
};
// Current concurrent players for an app via Steam's public stats endpoint — the
// same "players right now" figure SteamCharts is built on, straight from the
// source, no API key required. Returns null on any failure so player stats stay
// a nice-to-have, never a gate.
export const currentPlayers = async function currentPlayers ( appId ) {
try {
const params = new URLSearchParams( {
appid: String( appId ),
} );
const response = await fetch( `${ CURRENT_PLAYERS_URL }?${ params.toString() }` );
if ( !response.ok ) {
return null;
}
const body = await response.json();
const playerCount = body.response && body.response.player_count;
if ( typeof playerCount === 'number' ) {
return playerCount;
}
return null;
} catch ( playersError ) {
console.error( `[steam] current players ${ appId } failed: ${ playersError.message }` );
return null;
}
};