Last active
August 16, 2021 09:30
-
-
Save shamansir/452d771b2653aa32bdb09f22e53eadc0 to your computer and use it in GitHub Desktop.
Load games list as JSON From Backloggery page
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
let games = Array.from(document.getElementsByClassName('gamebox')); | |
function loadTitle(gamebox) { | |
if (!gamebox) return null; | |
return gamebox.querySelector('h2 b') ? gamebox.querySelector('h2 b').textContent.trim() : null; | |
} | |
function isNowPlaying(gamebox) { | |
return gamebox.classList.contains('nowplaying'); | |
} | |
function getPlatform(gamebox) { | |
return gamebox.querySelector('.gamerow b') ? gamebox.querySelector('.gamerow b').textContent.trim() : null; | |
} | |
function getStatus(gamebox) { | |
if (!gamebox.querySelectorAll('h2 a')) return null; | |
if (!gamebox.querySelectorAll('h2 a')[1]) return null; | |
if (!gamebox.querySelectorAll('h2 a')[1].querySelector('img')) return null; | |
return gamebox.querySelectorAll('h2 a')[1].querySelector('img').getAttribute('alt'); | |
} | |
function getInfo(gamebox) { | |
if (!gamebox.querySelectorAll('.gamerow')) return null; | |
if (!gamebox.querySelectorAll('.gamerow')[1]) return null; | |
return gamebox.querySelectorAll('.gamerow')[1].textContent.trim(); | |
} | |
function getAchievements(gamebox) { | |
if (!gamebox.querySelector('.info span')) return null; | |
if (!gamebox.querySelector('.info span').childNodes[1]) return null; | |
return adaptAchievements(gamebox.querySelector('.info span').childNodes[1].textContent.trim()); | |
} | |
function getRegion(gamebox) { | |
if (!gamebox.querySelectorAll('h2 img.lift')) return null; | |
if (!gamebox.querySelectorAll('h2 img.lift')[1]) return null; | |
return gamebox.querySelectorAll('h2 img.lift')[1].getAttribute('alt'); | |
} | |
function getRating(gamebox) { | |
if (!gamebox.querySelectorAll('.gamerow img.lift')) return null; | |
if (!gamebox.querySelectorAll('.gamerow img.lift')[0]) return null; | |
return adaptRating(gamebox.querySelectorAll('.gamerow img.lift')[0].getAttribute('alt')); | |
} | |
function getDetails(gamebox) { | |
if (!gamebox.querySelector('div[id]')) return; | |
if (!gamebox.querySelector('div[id]')[1]) return; | |
return gamebox.querySelectorAll('div[id]')[1].textContent.trim(); | |
} | |
function adaptRating(rating) { | |
if (!rating) return rating; | |
return { as_str : rating, | |
value : (rating.match(/★/g)||[]).length | |
}; | |
} | |
function adaptAchievements(achievementsStr) { | |
if (!achievementsStr) return achievementsStr; | |
let matches = achievementsStr.match(/(\d+)\s+\/\s+(\d+)\s+\((\d+)\%\)/); | |
if (!matches) { | |
return { as_str : achievementsStr } | |
} else { | |
return { | |
got : parseInt(matches[1]), | |
total : parseInt(matches[2]), | |
percent: parseInt(matches[3]), | |
as_str : achievementsStr | |
} | |
} | |
} | |
function loadGamesInfo(games) { | |
return games | |
.filter(function(gamebox) { return !gamebox.classList.contains('systemend'); }) | |
.map(function(gamebox) { | |
return { | |
title : loadTitle(gamebox), | |
nowPlaying: isNowPlaying(gamebox), | |
platform: getPlatform(gamebox), | |
status: getStatus(gamebox), | |
info: getInfo(gamebox), | |
achievements: getAchievements(gamebox), | |
rating: getRating(gamebox), | |
region: getRegion(gamebox), | |
details : getDetails(gamebox) | |
} | |
}); | |
} | |
const gamesInfo = loadGamesInfo(games); | |
console.log(JSON.stringify(gamesInfo)); | |
console.info(gamesInfo.length); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment