This repository has been archived by the owner on Jun 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
128 lines (120 loc) · 3.49 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
"use strict";
const cheerio = require("cheerio");
const request = require("request");
const signale = require("signale");
const _ = require("lodash");
const cfg = require("./config");
// !! CHANGE THIS WHEN UPDATING API
const dataHeaders = [
"death_date",
"class",
"level",
"base_fame",
"total_fame",
"experience",
"equipment",
"death_stats",
"killed_by"
];
// wow this is shitty lol
function parseData(el, type, $) {
switch (type) {
case cfg.DATA.EQUIPMENT:
return $(el).attr("title");
break;
default:
return $(el).text();
break;
}
}
// todo add input sanitization
async function scrapeGraveyard(playerName, maxResults = 10) {
let url = cfg.API_ENDPOINT + playerName;
signale.debug("Url is: " + url);
if (maxResults > 100) {
signale.warn(
"Currently, max number of results is 100...Clamping Value"
);
maxResults = 100;
}
signale.info(`Fetching ${maxResults} results`);
return new Promise((resolve, reject) => {
request(
{
url: url,
headers: {
"user-agent": cfg.USER_AGENT // need this to avoid being blacklisted by nginx
}
},
(err, res, body) => {
if (!err && res.statusCode == 200) {
signale.success("Successfully loaded graveyard page");
var $ = cheerio.load(body);
// safety check to see if graveyard is hidden
if($("body > div.container > div > div > h3").text().contains("hidden")) {
reject(new Error("Graveyard is hidden"))
}
let allEls = []; // stores cheerio stuff
let graves = [];
let raw_data = [[]];
// dates
// todo optimize strings
let deathDates = $("#e > tbody > tr > td:nth-child(1)");
let classes = $("#e > tbody > tr > td:nth-child(3)");
let levels = $("#e > tbody > tr > td:nth-child(4)");
let baseFame = $("#e > tbody > tr > td:nth-child(5)");
let totalFame = $("#e > tbody > tr > td:nth-child(6)");
let exp = $("#e > tbody > tr > td:nth-child(7)");
let equipmentSets = $("#e > tbody > tr > td:nth-child(8)")
.children("span")
.children("a")
.children("span");
let deathStats = $(
"#e > tbody > tr > td:nth-child(9) > span"
);
let killedBy = $("#e > tbody > tr > td:nth-child(10)");
allEls.push(
{ data: deathDates, type: cfg.DATA.DEATH_DATES },
{ data: classes, type: cfg.DATA.CLASS },
{ data: levels, type: cfg.DATA.LEVEL },
{ data: baseFame, type: cfg.DATA.BASE_FAME },
{ data: totalFame, type: cfg.DATA.TOTAL_FAME },
{ data: exp, type: cfg.DATA.EXP },
{ data: equipmentSets, type: cfg.DATA.EQUIPMENT },
{ data: deathStats, type: cfg.DATA.DEATH_STATS },
{ data: killedBy, type: cfg.DATA.KILLED_BY }
);
// reduce the number of results from the first 100 to the first $maxResults
// and process stuff
for (var i in allEls) {
allEls[i].data = _.slice(allEls[i].data, 0, maxResults);
raw_data[i] = []; // https://stackoverflow.com/a/17534346/3807967
$(allEls[i].data).each((j, el) => {
raw_data[i][j] = parseData(el, allEls[i].type, $);
});
}
signale.info("Processing graves");
// I LOVE U LODASH
graves = _.reverse(
_.map(_.zip(...raw_data), grave_arr => {
return _.zipObject(dataHeaders, grave_arr);
})
);
signale.success("Graves have been processed");
resolve(graves);
} else {
signale.error(
reject(
new Error(
"Could not get graveyard due to error" + err
)
)
);
}
}
);
});
}
module.exports = {
scrapeGraveyard
};