Last active
December 18, 2022 03:49
-
-
Save danielgormly/9f6d8e3d34e7546d7fde3b65009ae5af to your computer and use it in GitHub Desktop.
Pull ratings from your Gamespot profile and save as json
This file contains 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
const { parse } = require('date-fns'); | |
const { JSDOM } = require('jsdom'); | |
const { writeFileSync } = require('fs'); | |
async function main() { | |
const outputFile = 'output.json'; | |
const outputData = []; | |
for (let i = 1; i <= 10; i++) { | |
const res = await fetch(`https://www.gamespot.com/profile/reviews/?page=${i}`, { | |
"credentials": "include", | |
"headers": { | |
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:108.0) Gecko/20100101 Firefox/108.0", | |
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8", | |
"Accept-Language": "en-US,en;q=0.5", | |
"Upgrade-Insecure-Requests": "1", | |
"Sec-Fetch-Dest": "document", | |
"Sec-Fetch-Mode": "navigate", | |
"Sec-Fetch-Site": "none", | |
"Sec-Fetch-User": "?1", | |
"Cookie": "" // Copy cookie in here from document.cookie in browser console | |
}, | |
"method": "GET", | |
"mode": "cors" | |
}); | |
const raw = await res.text(); | |
const document = new JSDOM(raw).window.document; | |
const games = document.querySelectorAll('.media-game'); | |
games.forEach((gameEl) => { | |
const newGame = {}; | |
console.log(gameEl.querySelector('.media-title').textContent); | |
newGame.title = gameEl.querySelector('.media-title').textContent; | |
const attrs = gameEl.querySelector('.userReview-list__byline').textContent; | |
if (attrs) { | |
const split = attrs.split('|').map((text) => text.trim()); | |
const day = /.+Date: (.*)/g.exec(split[1])[1]; | |
newGame.date = parse(day, 'MMM dd, yyyy', new Date()); | |
newGame.console = split[2]; | |
} | |
outputData.push(newGame); | |
}); | |
// random time | |
await new Promise((resolve) => setTimeout(resolve, Math.random * 100)); | |
} | |
writeFileSync(outputFile, JSON.stringify(outputData.sort((a, b) => a.date - b.date), null, 2), { encoding: 'utf-8' }); | |
} | |
main(); |
This file contains 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
{ | |
"dependencies": { | |
"date-fns": "^2.29.3", | |
"jsdom": "^20.0.3", | |
"node-fetch": "^3.3.0" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment