-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetSoldItemsBuff.js
71 lines (65 loc) · 2.06 KB
/
getSoldItemsBuff.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
let startDate = "2024-01-01";
let endDate = "2024-02-01";
let start = Math.round(
new Date(`${startDate}T00:00:00-03:00`).getTime() / 1000
);
let end = Math.round(new Date(`${endDate}T00:00:00-03:00`).getTime() / 1000);
const timeout = (ms) => {
return new Promise((res) => setTimeout(res, ms));
};
class SoldItem {
constructor(name, cny, date) {
this.name = name;
this.cny = cny;
this.date = date;
}
}
async function getSoldItemsBuff(start, end, page = 1, soldItems = []) {
try {
let res = await fetch(
`https://buff.163.com/market/sell_order/history?game=csgo&start_time=${start}&end_time=${end}&state=success&page_num=${page}`,
{
headers: {
accept:
"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
},
method: "GET",
// @ts-ignore
credentials: "include",
}
);
let text = await res.text();
let parser = new DOMParser();
let doc = parser.parseFromString(text, "text/html");
let items = [...doc.querySelectorAll(".list_tb_csgo>tr")];
console.log(items.length);
items.forEach((item) => {
try {
let property = [...item.querySelectorAll("td")];
let name = property[2].querySelector("span").innerText;
let cny =
Number(property[3].querySelector("strong").innerText.slice(2)) *
0.975;
cny = Number(cny.toFixed(2));
let dateNum =
Number(property[5].querySelector("span").getAttribute("data-ts")) *
1000;
let date = new Date(dateNum);
let soldItem = new SoldItem(name, cny, date);
soldItems.push(soldItem);
} catch (err) {
return soldItems;
}
});
if (items.length == 10) {
await timeout(2000);
return await getSoldItemsBuff(start, end, page + 1, soldItems);
}
return soldItems;
} catch (err) {
throw err;
}
}
getSoldItemsBuff(start, end).then((soldItems) => {
console.log(JSON.stringify(soldItems, undefined, 2));
});