-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwitch.js
72 lines (57 loc) · 2.01 KB
/
twitch.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
let CLIENT_ID = process.env.CLIENT_ID
let CLIENT_SECRET = process.env.CLIENT_SECRET
export let GAME_ID = process.env.GAME_ID
export let LANGUAGE = process.env.LANGUAGE
let LAST_REQUEST = Date.now()
let TWITCH_TOKEN
async function getTwitchClientCredentialsToken(clientId, clientSecret) {
return await fetch(`https://id.twitch.tv/oauth2/token?client_id=${clientId}&client_secret=${clientSecret}&grant_type=client_credentials`, {
method: "POST"
}).then((r) => { return r.json(); });
}
async function checkTwitchTokenValidity() {
if ((Date.now() - LAST_REQUEST) / 1000 > TWITCH_TOKEN?.expires_in || !TWITCH_TOKEN) {
console.log("Twitch token expired. Getting a new one.")
TWITCH_TOKEN = await getTwitchClientCredentialsToken(CLIENT_ID, CLIENT_SECRET)
}
}
export async function getLiveStreams(gameId, language) {
let cursor = ""
let streams = []
console.log("Fetching livestreams")
do {
let queryParams = []
let queryParamsString
if (gameId) {
queryParams.push(`game_id=${gameId}`)
}
if (language) {
queryParams.push(`language=${language}`)
}
if (cursor) {
queryParams.push(`after=${cursor}`)
}
if (queryParams.length > 0) {
queryParamsString = `?${queryParams.join("&")}`
}
let apiEndpoint = `https://api.twitch.tv/helix/streams${queryParamsString}`
await checkTwitchTokenValidity()
let response = await fetch(apiEndpoint, {
headers: {
"Authorization": `Bearer ${TWITCH_TOKEN.access_token}`,
"Client-Id": CLIENT_ID
}
})
let json = await response.json()
streams.push(...json.data)
if (json.pagination.cursor) {
console.log("Getting next page of streams.")
cursor = json.pagination.cursor
}
else {
console.log("Last page.")
cursor = ""
}
} while (cursor)
return streams;
}