Last active
May 22, 2024 20:55
-
-
Save zencd/adc261db3f7ad36863766c9ed2a5e00c to your computer and use it in GitHub Desktop.
Copy steam games to an SG compilation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
await (async function() { | |
function getRandomInt(min, max) { | |
return Math.floor(Math.random() * (max - min + 1)) + min; | |
} | |
async function rewriteAllGames(compId, gameIds) { | |
console.log('gameIds:', gameIds); | |
var formData = new FormData(); | |
for (let i = 0; i < gameIds.length; i++) { | |
let gameId = gameIds[i]; | |
formData.append('items[]', 'game/' + gameId); | |
formData.append('order[game/'+gameId+']', '' + i); | |
} | |
var resp = await fetch('https://stopgame.ru/ajax/collections/save-items/' + compId, {method: 'POST', body: formData}); | |
if (resp.status !== 200) { | |
console.error('Не получилось перезаписать игры', resp); | |
let text = await resp.text(); | |
console.error('text: ' + text); | |
} | |
} | |
function resolveGame(searchRes) { | |
if (!searchRes) return null; | |
let results = searchRes['results']; | |
if (!results) return null; | |
for (var i = 0; i < results.length; i++) { | |
let item = results[i]; | |
if (item['type'] === 'game') { | |
return item | |
} | |
} | |
return null; | |
} | |
var m = document.location.href.match(/\/compilation\/(\d+)/); | |
if (!m) return; | |
var compId = m[1]; // like '1234' | |
var res = prompt('Вставьте текст CSV-файла', ''); | |
var rows = res.split('\n'); | |
var rowCnt = -1; | |
var gameIds = []; | |
for (var i = 0; i < rows.length; i++) { | |
rowCnt++; | |
var cells = rows[i].split(','); | |
if (cells.length < 1 || cells[0] === 'game') continue; | |
var title = cells[0].replace(/[^0-9a-zA-Zа-яА-ЯёЁ _-]+/g, '').trim(); | |
var resp = await fetch('https://stopgame.ru/ajax/search/games/?term=' + encodeURIComponent(title)); | |
if (resp.status !== 200) { | |
console.error('Не удалось найти игру: ' + title, resp); | |
continue; | |
} | |
var searchRes = await resp.json(); | |
let game = resolveGame(searchRes); | |
if (!game) { | |
console.error('Не удалось найти игру: ' + title, resp); | |
continue; | |
} | |
let gameId = game['id'] | |
let titleFound = game['title'] | |
console.log(`${rowCnt}/${rows.length} ${title} => ${titleFound} #${gameId}`); | |
gameIds.push(gameId); | |
await new Promise(r => setTimeout(r, getRandomInt(1000, 3000))); | |
} | |
console.log('Перезаписываю все игры: ' + gameIds.length); | |
rewriteAllGames(compId, gameIds); | |
console.log('Готово. Можно сохранить лог и/или обновить страницу'); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Скрипт копирует игры из своей коллекции Steam в подборку на Стопгейме. Проверено на Windows 10 + Chrome, на 280 играх.
Получаем список игр со стима:
Добавляем их на СГ: