-
-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathfetch_and_toggle.js
171 lines (149 loc) · 4.49 KB
/
fetch_and_toggle.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
const fs = require('fs');
const path = require('path');
const fetch = (...args) => import('node-fetch').then(({default: fetch}) => fetch(...args));
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
const client_config = JSON.parse(fs.readFileSync(path.join(
__dirname,
'jsons',
'config_client.json'
)));
const account_config = JSON.parse(fs.readFileSync(path.join(
__dirname,
'jsons',
'config_user.json'
)));
// validate n refresh key
console.log('Run with', client_config.client_id, account_config.access_token);
async function entry() {
let resp = await fetch(
'https://id.twitch.tv/oauth2/validate',
{
method: 'GET',
headers: {
'Authorization': `Bearer ${account_config.access_token}`,
'Accept': 'application/json'
}
}
)
if (resp.status != 200) {
regenerate();
return;
}
goFetch();
}
async function regenerate() {
let url = new URL('https://id.twitch.tv/oauth2/token');
url.search = new URLSearchParams([
[ 'grant_type', 'refresh_token' ],
[ 'refresh_token', account_config.refresh_token ],
[ 'client_id', client_config.client_id ],
[ 'client_secret', client_config.client_secret ]
]).toString();
let resp = await fetch(
url,
{
method: 'POST',
headers: {
'Accept': 'application/json'
}
}
);
if (resp.status != 200) {
console.error('Error', resp.status, await resp.text());
return;
}
let body = await resp.json();
for (var k in body) {
account_config[k] = body[k];
}
fs.writeFileSync(path.join(
__dirname,
'jsons',
'config_user.json'
), JSON.stringify(account_config, null, 4));
goFetch();
}
async function goFetch() {
let userResp = await fetch(
'https://api.twitch.tv/helix/users',
{
method: 'GET',
headers: {
'Client-ID': client_config.client_id,
'Authorization': `Bearer ${account_config.access_token}`,
'Accept': 'application/json'
}
}
);
if (userResp.status != 200) {
console.error('Error', userResp.status, await userResp.text());
return;
}
let body = await userResp.json();
console.log(body);
let broadcaster_id = body.data[0].id;
let url = new URL('https://api.twitch.tv/helix/channel_points/custom_rewards');
url.search = new URLSearchParams([
[ 'broadcaster_id', broadcaster_id ],
[ 'only_manageable_rewards', 1 ]
]).toString();
let rewardsResp = await fetch(
url,
{
method: 'GET',
headers: {
'Client-ID': client_config.client_id,
'Authorization': `Bearer ${account_config.access_token}`,
'Accept': 'application/json'
}
}
)
if (rewardsResp.status != 200) {
console.error('Error', rewardsResp.status, await rewardsResp.text());
return;
}
let rewardsBody = await rewardsResp.json();
console.log('Found', rewardsBody.data.length, 'for', broadcaster_id);
rl.question('Enable or disable (1/0)> ', dir => {
dir = parseInt(dir);
var enable = false;
if (dir === 1) {
enable = true;
} else if (dir === 0) {
enable = false;
} else {
console.log('Enter 1/0');
goFetch();
return;
}
rl.close();
for (var x=0;x<rewardsBody.data.length;x++) {
toggleReward(broadcaster_id, rewardsBody.data[x], enable);
}
})
}
async function toggleReward(broadcaster_id, reward, is_enabled) {
let url = new URL('https://api.twitch.tv/helix/channel_points/custom_rewards');
url.search = new URLSearchParams([
[ 'broadcaster_id', broadcaster_id ],
[ 'id', reward.id ]
]).toString();
let resp = await fetch(
url,
{
method: 'PATCH',
headers: {
'Client-ID': client_config.client_id,
'Authorization': `Bearer ${account_config.access_token}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({ is_enabled })
}
);
console.log('Updated', resp.status, (resp.status != 200 ? await resp.text() : ''));
}
entry();