Created
May 11, 2020 18:38
-
-
Save sometimesweremakemistakes/12116811f7b34d875e5bff74b3e42477 to your computer and use it in GitHub Desktop.
XLand User Page Parser
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 fetch = require('node-fetch') | |
const cheerio = require('cheerio') | |
const tableParser = require('cheerio-tableparser') | |
async function getInfoById(id = null) { | |
id = Number(id) | |
if(!id || isNaN(id) || id < 1) return {ok: false, response: "Не указан ID смешарика"} | |
let rawPage = await (await fetch(`https://playxland.ru/player/${id}`)).text() | |
let page = cheerio.load(rawPage) | |
let notFoundError = page('#container > h4').text() | |
if(notFoundError) return {ok: false, response: "Смешарик не найден"} | |
let userPic = page('#container > div.card > div.card-header > div > div > figure > img').attr('src') | |
let nickname = page('#container > div.card > div.card-header > div > div > figure > figcaption > h1 > span').text() | |
let group = page('#container > div.card > div.card-header > div > div > figure > figcaption > h2 > span').text() | |
let level = page('#container > div.card > div:nth-child(2) > table > tbody > tr:nth-child(1) > td.aligned').text() | |
let regDate = page('#container > div.card > div:nth-child(2) > table > tbody > tr:nth-child(2) > td.aligned').text() | |
// TODO: Fix bug if player hasn't club | |
let houses = page('#container > div.card > div:nth-child(4) > div') | |
.find('div > div > figure > figcaption') | |
.toArray() | |
.map((element) => page(element).attr('title')) | |
let clubs = page('#container > div.card > div:nth-child(6) > div') | |
.find('div > div > figure > figcaption') | |
.toArray() | |
.map((element) => page(element).attr('title')) | |
let phones = page('#container > div.card > div:nth-child(8) > div') | |
.find('div > div > figure > figcaption') | |
.toArray() | |
.map((element) => page(element).attr('title')) | |
return {ok: true, response: {userPic, nickname, group, level, regDate, houses, clubs, phones}} | |
} | |
async function search(query = null) { | |
if(!query) return {ok: false, response: "Не указан запрос"} | |
let rawPage = await (await fetch(`https://playxland.ru/search/filter?value=${query}`)).text() | |
let page = await cheerio.load(rawPage) | |
let notFoundError = page('#container > h4').text() | |
if(notFoundError) return {ok: false, response: "Ничего не найдено"} | |
tableParser(page) | |
let results = page('#container > table').parsetable(false,false,false) | |
results = results[0].map((value, index) => { | |
if(index === 0) return | |
return { | |
id: Number(results[1][index].match(/\/player\/([0-9]+)/i)[1]), | |
nickname: page(results[1][index]).text(), | |
group: page(results[2][index]).text(), | |
level: results[3][index] | |
} | |
}).filter((value) => typeof value !== 'undefined') | |
return {ok: true, response: results} | |
} | |
module.exports = { | |
getInfoById, | |
search | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment