/
twitch.js
80 lines (74 loc) · 2.02 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
73
74
75
76
77
78
79
80
require("dotenv").config();
const axios = require("axios");
const cache = require("node-cache");
const get_app_access_token = async () => {
const key = "twapi:twitch_app_access_token";
const myCache = new cache();
const token = myCache.get(key);
if (token) {
return token;
}
const url = "https://id.twitch.tv/oauth2/token";
const data = {
client_id: process.env.TWITCH_CLIENT_ID,
client_secret: process.env.TWITCH_CLIENT_SECRET,
grant_type: "client_credentials",
};
try {
const res = await axios.post(url, data);
console.log(res);
myCache.set(key, res.data.access_token, 3600);
return res.data.access_token;
} catch (error) {
console.log(error);
}
};
const get_stream = async (channel) => {
const token = await get_app_access_token();
const url = `https://api.twitch.tv/helix/streams?user_login=${channel}`;
const headers = {
"Client-ID": process.env.TWITCH_CLIENT_ID,
Authorization: `Bearer ${token}`,
};
try {
const res = await axios.get(url, { headers: headers });
console.log(res.data.data[0]);
return res.data.data[0];
} catch (error) {
console.log(error);
}
};
const get_user = async (channel) => {
const token = await get_app_access_token();
const url = `https://api.twitch.tv/helix/users?login=${channel}`;
const headers = {
"Client-ID": process.env.TWITCH_CLIENT_ID,
Authorization: `Bearer ${token}`,
};
try {
const res = await axios.get(url, { headers: headers });
return res.data.data[0];
} catch (error) {
console.log(error);
}
};
const get_user_by_id = async (id) => {
const token = await get_app_access_token();
const url = `https://api.twitch.tv/helix/users?id=${id}`;
const headers = {
"Client-ID": process.env.TWITCH_CLIENT_ID,
Authorization: `Bearer ${token}`,
};
try {
const res = await axios.get(url, { headers: headers });
return res.data.data[0];
} catch (error) {
console.log(error);
}
};
module.exports = {
get_app_access_token,
get_stream,
get_user,
get_user_by_id,
};