Compare commits

..

No commits in common. "de9ed5196897766d57e75818a381f9025849b479" and "9700444420e5558cc0e90a34bdd65467fa721075" have entirely different histories.

66
code.js
View File

@ -1,39 +1,36 @@
/**
* Скрипт для парсинга значков Steam
* @version 1.0.0
* @version 0.1.0
* @license Unlicense
* @author Alexander Popov <iiiypuk@fastmail.fm>
*/
/**
* Получает данные из элемента `div.badge_row`
* @param {Array} value - div.badge_row
* @param {DOM} value - div.badge_row.
* @returns {Object}
*/
function getData(value) {
/*
* Sctruct of Badge
* id: int - steam app id
* url: str - game url in store
* card_url: str - card page url
* name: str - steam app name
* dropLeft: int - cards left drops
* Badge Sctruct:
* id: int
* uri: str
* name: str
* dropLeft: int
*/
// check left cards
// для карточек, у которых нет параметра 'карточек выпадет'
// для металлических карточек нет параметра 'карточек выпадет'
let cardsDropLeft = 0;
if (value.getElementsByClassName('progress_info_bold').length > 0) {
cardsDropsNumber = value.getElementsByClassName('progress_info_bold')[0].innerText.match(/\d/g);
if (cardsDropsNumber !== null) { cardsDropLeft = [0]; }
if (value.getElementsByClassName('progress_info_bold')[0]) {
cardsDropLeft = value.getElementsByClassName('progress_info_bold')[0].match(/\d/g);
}
let badgeItem = {
id: parseInt(value.childNodes[1].href.split('/')[6]),
url: `https://store.steampowered.com/app/${value.childNodes[1].href.split('/')[6]}/`,
card_url: value.childNodes[1].href,
name: value.getElementsByClassName('badge_title')[0].childNodes[0].textContent.replace(/[\r\n\t]/g, '').trim(),
dropLeft: parseInt(cardsDropLeft),
id: value.childNodes[1].href.split('/')[6],
uri: value.childNodes[1].href,
name: value.childNodes[3].childNodes[1].childNodes[3].childNodes[0].nodeValue.replace(/[\r\n\t]/g, ''),
dropLeft: cardsDropLeft,
};
return badgeItem;
@ -41,14 +38,10 @@ function getData(value) {
/**
* Преобразовывает массив данных в JSON и возвращает к консоль
* @param {Array} badgesArray - массив данных о значках
* @param {Array} badgesArray - массив данных о значках.
*/
function exportJson(badgesArray) {
const byteSize = str => new Blob([str]).size;
json = JSON.stringify(badgesArray);
console.log(json);
console.log(`Size: ${byteSize(json)} bytes.`)
console.log(JSON.stringify(badgesArray));
}
/**
@ -56,34 +49,13 @@ function exportJson(badgesArray) {
*/
function getBadges() {
let badges = [];
let currentPage = 1;
// get total pages
const [baseURI, totalPages] = (function(element) {
return [
element[0].baseURI,
element[element.length - 1].text
];
})(document.getElementsByClassName('pagelink'));
while (currentPage <= totalPages) {
fetch(`${baseURI}?p=${currentPage}`)
.then((response) => { return response.text(); })
.then((html) => {
let parser = new DOMParser();
let doc = parser.parseFromString(html, 'text/html');
const collection = doc.getElementsByClassName('badge_row');
const collection = document.getElementsByClassName('badge_row');
for (const value of Object.entries(collection)) {
badges.push(getData(value[1]));
}
})
.catch((err) => console.warn('Something went wrong.', err));
currentPage++;
badges.push(getData(value));
}
setTimeout(() => { exportJson(badges); }, 10000); // FIX IT
exportJson(badges);
}
getBadges();