-
Notifications
You must be signed in to change notification settings - Fork 2
/
scrape.js
290 lines (254 loc) · 10.6 KB
/
scrape.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
import fs from 'fs';
import jsdom from 'jsdom';
import followRedirects from 'follow-redirects';
const { http, https } = followRedirects;
import { getArgsVal } from './cmd.js';
import Skin from './models/skin.js';
import Source from './models/source.js';
import { getArgs } from './utils/args.js';
import { getHighestRarity, rarityToNumber } from './utils/rarity.js';
const scrapeUrl = (url) => {
return new Promise((resolve, reject) => {
let client = http;
if (url.toString().indexOf("https") === 0) {
client = https;
}
client.get(url, (resp) => {
let data = '';
// A chunk of data has been recieved.
resp.on('data', (chunk) => {
data += chunk;
});
// The whole response has been received. Print out the result.
resp.on('end', () => {
resolve(data);
});
}).on("error", (err) => {
reject(err);
});
});
};
async function getGunsSources() {
let res = [];
let scraped = await scrapeUrl("https://csgostash.com/");
let dom = new jsdom.JSDOM(scraped);
let all = [...dom.window.document.getElementsByClassName("navbar-nav")[0].children]
Array.prototype.slice.call(all).map((p, i) => {
if(i != 6 && i != 7) return; // only leave the relevant drop downs menues and skip knives
[...p.children[1].children].map(e => {
if(e.children.length > 0){
let temp = { url:'', source:'' }
temp.url = (e.children[0].href);
temp.source = e.children[0].textContent.trim();
if(temp.source == "All Skin Cases" ||
temp.source == "Souvenir Packages" ||
temp.source == "Gift Packages") return;
res.push(temp);
}
});
});
return res;
}
async function getGunsData(){
let gunSources = await getGunsSources();
let gunsData = {};
for(let i = 0; i < gunSources.length; i++){
console.log(`now scraping: ${gunSources[i].source}`.green);
gunsData[`${gunSources[i].source}`] = await gunScrape(gunSources[i].url);
}
return gunsData;
}
async function gunScrape(url){
let res = [];
let scraped = await scrapeUrl(url);
let dom = new jsdom.JSDOM(scraped);
let boxes = [...dom.window.document.getElementsByClassName('result-box')];
let allRarities = [...dom.window.document.getElementsByClassName('quality')];
const highestRarity = rarityToNumber(getHighestRarity(allRarities));
await Array.prototype.slice.call(boxes).map(async (p, i) => {
if(p.textContent.includes("Rare")) return; // skip knifes and gloves
if(p.children.length <= 3) return; // remove adds
if(p.children[0].textContent.toLowerCase().indexOf("default") != -1) return; // remove default skins
console.log(` -${p.children[0].textContent}`.gray);
// get basic data first
let gunData = {
name: p.children[0].textContent,
rarity: p.children[1].textContent.trim().split(' ')[0],
imgSrc: p.children[3].children[0].src,
isValidInput: true,
};
// can't be used in trade ups
if(gunData.rarity == "Contraband") return;
// scrape the more complex data
let otherData = await advancedGunScrape(p.children[4].children[0].children[0].href);
gunData = {...gunData, ...otherData}
// skip the gun if it's souvenier only
if(
gunData.STAT_TRAK.FN.stashVal +
gunData.STAT_TRAK.MW.stashVal +
gunData.STAT_TRAK.FT.stashVal +
gunData.STAT_TRAK.WW.stashVal +
gunData.STAT_TRAK.BS.stashVal +
gunData.FN.stashVal +
gunData.MW.stashVal +
gunData.FT.stashVal +
gunData.WW.stashVal +
gunData.BS.stashVal == -10
) return;
// is not a valid input (only output)
if(rarityToNumber(gunData.rarity) == highestRarity){
gunData.isValidInput = false;
}
// add the gun data to the resulting array of objects
res.push(gunData);
});
return res;
}
async function advancedGunScrape(url){
let gunData = {
STAT_TRAK:{
FN: {stashVal:-1, steamLink:''},
MW: {stashVal:-1, steamLink:''},
FT: {stashVal:-1, steamLink:''},
WW: {stashVal:-1, steamLink:''},
BS: {stashVal:-1, steamLink:''},
},
FN: {stashVal:-1, steamLink:''},
MW: {stashVal:-1, steamLink:''},
FT: {stashVal:-1, steamLink:''},
WW: {stashVal:-1, steamLink:''},
BS: {stashVal:-1, steamLink:''},
MIN_WEAR:-1,
MAX_WEAR:-1,
CSGO_STASH_ID: Number(url.split('/')[4]),
CSGO_STASH_NAME: url.split('/')[5]
};
let scraped = await scrapeUrl(url);
let dom = new jsdom.JSDOM(scraped);
// get min and max wear values
try {
let [minW,maxW] = [...dom.window.document.getElementsByClassName("wear-bar-markers")[0].children];
gunData.MIN_WEAR = Number(minW.textContent.trim());
gunData.MAX_WEAR = Number(maxW.textContent.trim());
} catch { /* floats not available */ }
// get stashed values
let tabView = dom.window.document.getElementById("prices");
let items = [...tabView.getElementsByClassName("btn-group-justified")];
items.splice(0,1); // remove the first irrelevant one
for(let item of items){
let arr = item.textContent.trim().split('\n');
if(arr.length == 3){
let current = arr[0] == "StatTrak" ? JSON.parse(JSON.stringify(gunData.STAT_TRAK)) : null;
if(current == null) continue; // skip souvenier
if(arr[2].indexOf('No') != -1) continue; // skip the "no recent/not possible" ones
let stashVal = Number(arr[2].slice(0,-1).replace(',','.').replace('--','0').replace(' ',''));
stashVal *= 1.16; // convert to usd
if(arr[1].trim().toLowerCase() == 'factory new') {
current.FN.stashVal = stashVal;
current.FN.steamLink = item.firstElementChild.href;
}
else if(arr[1].trim().toLowerCase() == 'minimal wear') {
current.MW.stashVal = stashVal;
current.MW.steamLink = item.firstElementChild.href;
}
else if(arr[1].trim().toLowerCase() == 'field-tested') {
current.FT.stashVal = stashVal;
current.FT.steamLink = item.firstElementChild.href;
}
else if(arr[1].trim().toLowerCase() == 'well-worn') {
current.WW.stashVal = stashVal;
current.WW.steamLink = item.firstElementChild.href;
}
else if(arr[1].trim().toLowerCase() == 'battle-scarred') {
current.BS.stashVal = stashVal;
current.BS.steamLink = item.firstElementChild.href;
}
else if(arr[1].trim().toLowerCase() == 'vanilla') {
// current.VANILLA.stashVal = stashVal;
// current.VANILLA.steamLink = item.firstElementChild.href;
}
else throw new Error(arr[1]);
if(arr[0] == 'StatTrak') {
gunData.STAT_TRAK.FN = {...current.FN};
gunData.STAT_TRAK.MW = {...current.MW};
gunData.STAT_TRAK.FT = {...current.FT};
gunData.STAT_TRAK.WW = {...current.WW};
gunData.STAT_TRAK.BS = {...current.BS};
}
}
else {
if(arr[1].indexOf('No') != -1) continue; // skip the "no recent/not possible" ones
let stashVal = Number(arr[1].slice(0,-1).replace(',','.').replace('--','0').replace(' ',''));
stashVal *= 1.16; // convert to usd
if(stashVal == null) throw new Error(arr[1]);
if(arr[0].trim().toLowerCase() == 'factory new') {
gunData.FN.stashVal = stashVal;
gunData.FN.steamLink = item.firstElementChild.href;
}
else if(arr[0].trim().toLowerCase() == 'minimal wear') {
gunData.MW.stashVal = stashVal;
gunData.MW.steamLink = item.firstElementChild.href;
}
else if(arr[0].trim().toLowerCase() == 'field-tested') {
gunData.FT.stashVal = stashVal;
gunData.FT.steamLink = item.firstElementChild.href;
}
else if(arr[0].trim().toLowerCase() == 'well-worn') {
gunData.WW.stashVal = stashVal;
gunData.WW.steamLink = item.firstElementChild.href;
}
else if(arr[0].trim().toLowerCase() == 'battle-scarred') {
gunData.BS.stashVal = stashVal;
gunData.BS.steamLink = item.firstElementChild.href;
}
else if(arr[0].trim().toLowerCase() == 'vanilla') {
// gunData.VANILLA.stashVal = stashVal;
// gunData.VANILLA.steamLink = item.firstElementChild.href;
}
else throw new Error(arr[0]);
}
}
return gunData;
}
async function updateMongose(scrapedData){
for(let source in scrapedData){
let sourceIsValids = {
'Consumer': false,
'Industrial': false,
'Mil-Spec': false,
'Restricted': false,
'Classified': false,
'Covert': false,
}
// add guns to database
for(let gun of scrapedData[source]){
gun.source = source;
try { await new Skin({...gun}).save(); } catch { continue }
if(!sourceIsValids[gun.rarity] && gun.isValidInput){
sourceIsValids[gun.rarity] = true;
}
}
// add source to database
try { await new Source({ name: source, ...sourceIsValids }).save(); } catch {}
}
}
async function updateDatabase(){
let scrapedData;
// parse the json and get the scraped data
if(getArgs().includes('--json')){
let path = getArgsVal('--json', 'string');
if(!fs.existsSync(path)) throw new Error("Path for json file is not correct.");
scrapedData = JSON.parse(fs.readFileSync(path));
}
// scrape data from web
else {
scrapedData = await getGunsData();
}
console.log("[#] scraping data finished!".green);
if(getArgs().includes('--sdb')){
fs.writeFileSync(`data_${Date.now()}.json`, JSON.stringify(scrapedData));
console.log("[#] scraped data saved to a file!".green);
}
await updateMongose(scrapedData);
}
export { advancedGunScrape, scrapeUrl, updateDatabase };