-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrequest.js
More file actions
173 lines (154 loc) · 5.19 KB
/
request.js
File metadata and controls
173 lines (154 loc) · 5.19 KB
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
const { readFile, writeFile } = require('node:fs/promises')
const dayjs = require('dayjs')
const timezone = require('dayjs/plugin/timezone')
const utc = require('dayjs/plugin/utc')
const customParseFormat = require('dayjs/plugin/customParseFormat')
const duration = require('dayjs/plugin/duration')
const relativeTime = require('dayjs/plugin/relativeTime')
dayjs.extend(relativeTime)
dayjs.extend(duration)
dayjs.extend(utc)
dayjs.extend(timezone)
dayjs.extend(customParseFormat)
require('dayjs/locale/en')
const puppeteer = require('puppeteer-extra')
// add stealth plugin and use defaults (all evasion techniques)
const StealthPlugin = require('puppeteer-extra-plugin-stealth')
puppeteer.use(StealthPlugin())
const url = "https://steamdb.info/sales/history/";
const launchOpts = {
headless: true,
args: ['--no-sandbox', '--disable-setuid-sandbox', '--disable-infobars']
};
if (process.platform === "darwin") {
launchOpts.executablePath = process.env['PUPPETEER_EXECUTABLE_PATH']
}
async function getData() {
const browser = await puppeteer.launch(launchOpts)
try {
const page = await browser.newPage()
await page.goto(url)
const upcoming = await page.evaluate(() => {
const panels = document.querySelector("#main .row").querySelectorAll(".panel-sale")
const ls = []
panels.forEach(e => {
let img = e.querySelector('img')
if (img) {
img = img.src
} else {
// Some event may not have image
img = "https://t3.ftcdn.net/jpg/00/99/07/56/240_F_99075616_91C3OrVfac71CjwTJgz68r62dh2A0R80.jpg" // Hard code leaked image for now
}
const p = e.querySelector('.panel-body')
const a = p.querySelector("a")
const title = a.text
const link = a.href
const duration = p.querySelector("div").textContent.split(" — ")
const start = duration[0]
const end = duration[1]
const isMajorSale = e.classList.contains("major-sale")
ls.push({
img, title, link, start, end, isMajorSale
})
})
return ls
});
const current = await page.evaluate(() => {
const panel = document.querySelector(".next-sale .container")
const h2 = panel.querySelector("h2").textContent
const unixtime = panel.querySelector('#js-sale-countdown').attributes['data-target'].nodeValue.slice(0, -3)
if (h2 === "When is the next Steam sale?") {
return {
live: false,
title: panel.querySelector('.sale-name').textContent,
unixtime
}
}
return {
live: true,
title: h2,
unixtime
}
});
} finally {
browser.close()
}
return {
upcoming,
current,
operation_time: dayjs()
}
}
/*
[
{
title: 'Steam VR Fest 2022',
link: 'https://store.steampowered.com/news/group/4145017/view/3218394291609115017',
start: '18 July 2022',
end: '25 July 2022'
},
{
title: 'Steam Survival Fest 2022',
link: 'https://steamcommunity.com/groups/steamworks/announcements/detail/3222900587040657570',
start: '1 August 2022',
end: '8 August 2022'
},
{
title: 'Next Fest: October 2022',
link: 'https://steamcommunity.com/groups/store_promos/announcements/detail/3363641088446628992',
start: '3 October 2022',
end: '10 October 2022'
}
]
*/
const cacheFileName = "cache.json"
const refreshHour = 23
async function fetchAndWrite() {
const d = await getData()
writeFile(cacheFileName, JSON.stringify({
...d,
operation_time: d.operation_time.toJSON()
}))
return d
}
let cache = null
async function main() {
if (!cache) {
console.log("no cache present, getting data")
cache = await readFile(cacheFileName).then(res => {
const d = JSON.parse(res)
d.operation_time = dayjs(d.operation_time)
const diffhour = dayjs().diff(d.operation_time, 'hour')
console.log(`Diff hour: ${diffhour}`)
if (diffhour > refreshHour) {
console.log("local cache expired, refetching...")
return fetchAndWrite()
}
return d
}).catch(e => {
console.log("Unable to retrieve cache", e)
return fetchAndWrite()
})
} else {
if (dayjs().diff(cache.operation_time, 'hour') > refreshHour) {
console.log("cache expired, updating...")
cache = await fetchAndWrite()
}
}
return {
current: {
...cache.current,
time: dayjs.unix(cache.current.unixtime)
},
upcoming: cache.upcoming.map(e => {
const start = dayjs(e.start, "D MMMM YYYY", "en")
const end = dayjs(e.end, "D MMMM YYYY", "en")
return {
...e,
start,
end,
}
})
}
}
module.exports = main