Compare commits
8 Commits
9700444420
...
de9ed51968
Author | SHA1 | Date | |
---|---|---|---|
de9ed51968 | |||
d0065503c4 | |||
f511b6aea8 | |||
a498743e45 | |||
bc6e580c3d | |||
8f4b8b0ebc | |||
c4249a0700 | |||
94b8b9788c |
66
code.js
66
code.js
@ -1,36 +1,39 @@
|
||||
/**
|
||||
* Скрипт для парсинга значков Steam
|
||||
* @version 0.1.0
|
||||
* @version 1.0.0
|
||||
* @license Unlicense
|
||||
* @author Alexander Popov <iiiypuk@fastmail.fm>
|
||||
*/
|
||||
|
||||
/**
|
||||
* Получает данные из элемента `div.badge_row`
|
||||
* @param {DOM} value - div.badge_row.
|
||||
* @param {Array} value - div.badge_row
|
||||
* @returns {Object}
|
||||
*/
|
||||
function getData(value) {
|
||||
/*
|
||||
* Badge Sctruct:
|
||||
* id: int
|
||||
* uri: str
|
||||
* name: str
|
||||
* dropLeft: int
|
||||
* 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
|
||||
*/
|
||||
|
||||
// check left cards
|
||||
// для металлических карточек нет параметра 'карточек выпадет'
|
||||
// для карточек, у которых нет параметра 'карточек выпадет'
|
||||
let cardsDropLeft = 0;
|
||||
if (value.getElementsByClassName('progress_info_bold')[0]) {
|
||||
cardsDropLeft = value.getElementsByClassName('progress_info_bold')[0].match(/\d/g);
|
||||
if (value.getElementsByClassName('progress_info_bold').length > 0) {
|
||||
cardsDropsNumber = value.getElementsByClassName('progress_info_bold')[0].innerText.match(/\d/g);
|
||||
if (cardsDropsNumber !== null) { cardsDropLeft = [0]; }
|
||||
}
|
||||
|
||||
let badgeItem = {
|
||||
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,
|
||||
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),
|
||||
};
|
||||
|
||||
return badgeItem;
|
||||
@ -38,10 +41,14 @@ function getData(value) {
|
||||
|
||||
/**
|
||||
* Преобразовывает массив данных в JSON и возвращает к консоль
|
||||
* @param {Array} badgesArray - массив данных о значках.
|
||||
* @param {Array} badgesArray - массив данных о значках
|
||||
*/
|
||||
function exportJson(badgesArray) {
|
||||
console.log(JSON.stringify(badgesArray));
|
||||
const byteSize = str => new Blob([str]).size;
|
||||
|
||||
json = JSON.stringify(badgesArray);
|
||||
console.log(json);
|
||||
console.log(`Size: ${byteSize(json)} bytes.`)
|
||||
}
|
||||
|
||||
/**
|
||||
@ -49,13 +56,34 @@ function exportJson(badgesArray) {
|
||||
*/
|
||||
function getBadges() {
|
||||
let badges = [];
|
||||
const collection = document.getElementsByClassName('badge_row');
|
||||
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');
|
||||
|
||||
for (const value of Object.entries(collection)) {
|
||||
badges.push(getData(value));
|
||||
badges.push(getData(value[1]));
|
||||
}
|
||||
})
|
||||
.catch((err) => console.warn('Something went wrong.', err));
|
||||
|
||||
currentPage++;
|
||||
}
|
||||
|
||||
exportJson(badges);
|
||||
setTimeout(() => { exportJson(badges); }, 10000); // FIX IT
|
||||
}
|
||||
|
||||
getBadges();
|
||||
|
Loading…
Reference in New Issue
Block a user