-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathlib.js
More file actions
248 lines (226 loc) · 6.39 KB
/
Copy pathlib.js
File metadata and controls
248 lines (226 loc) · 6.39 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
const { BrowserWindow } = require("electron");
const https = require("https");
const clientId = "kimne78kx3ncx6brgo4mv6wki5h1ko";
function getAccessToken(id, isVod, redacted = {}) {
const data = JSON.stringify({
operationName: "PlaybackAccessToken",
extensions: {
persistedQuery: {
version: 1,
sha256Hash:
"0828119ded1c13477966434e15800ff57ddacf13ba1911c129dc2200705b0712",
},
},
variables: {
isLive: !isVod,
login: isVod ? "" : id,
isVod: isVod,
vodID: isVod ? id : "",
playerType: "site",
},
});
const options = {
hostname: "gql.twitch.tv",
port: 443,
path: "/gql",
method: "POST",
headers: {
"Client-id": clientId,
...redacted,
},
};
return new Promise((resolve, reject) => {
const req = https.request(options, (response) => {
var resData = {};
resData.statusCode = response.statusCode;
resData.body = [];
response.on("data", (chunk) => resData.body.push(chunk));
response.on("end", () => {
resData.body = resData.body.join("");
if (resData.statusCode !== 200) {
let win = new BrowserWindow();
win.loadURL("https://twitch.tv/login");
reject(new Error(`mabye not authorized`));
} else {
if (isVod) {
resolve(JSON.parse(resData.body).data.videoPlaybackAccessToken);
} else {
resolve(JSON.parse(resData.body).data.streamPlaybackAccessToken);
}
}
});
});
req.on("error", (error) => reject(error));
req.write(data);
req.end();
});
}
function getPlaylist(id, accessToken, vod) {
return new Promise((resolve, reject) => {
const req = https
.get(
`https://usher.ttvnw.net/${
vod ? "vod" : "api/channel/hls"
}/${id}.m3u8?client_id=${clientId}&token=${accessToken.value}&sig=${
accessToken.signature
}&allow_source=true&allow_audio_only=true`,
(response) => {
let data = {};
data.statusCode = response.statusCode;
data.body = [];
response.on("data", (chunk) => data.body.push(chunk));
response.on("end", () => {
data.body = data.body.join("");
switch (data.statusCode) {
case 200:
resolve(resolve(data.body));
break;
case 404:
reject(
new Error(
"Transcode does not exist - the stream is probably offline",
),
);
break;
default:
reject(
new Error(`Twitch returned status code ${data.statusCode}`),
);
break;
}
});
},
)
.on("error", (error) => reject(error));
req.end();
});
}
function parsePlaylist(playlist) {
const parsedPlaylist = [];
const lines = playlist.split("\n");
for (let i = 4; i < lines.length; i += 3) {
parsedPlaylist.push({
// eslint-disable-next-line quotes
quality: lines[i - 2].split('NAME="')[1].split('"')[0],
resolution:
lines[i - 1].indexOf("RESOLUTION") !== -1
? lines[i - 1].split("RESOLUTION=")[1].split(",")[0]
: null,
url: lines[i],
});
}
return parsedPlaylist;
}
function getStream(channel, raw, redacted = {}) {
return new Promise((resolve, reject) => {
getAccessToken(channel, false, redacted)
.then((accessToken) => getPlaylist(channel, accessToken, false))
.then((playlist) => resolve(raw ? playlist : parsePlaylist(playlist)))
.catch((error) => reject(error));
});
}
function getLastStreamDate(channel) {
const data = [
{
operationName: "StreamSchedule",
variables: {
login: channel,
startingWeekday: "MONDAY",
utcOffsetMinutes: 540,
startAt: "2023-08-20T15:00:00.000Z",
endAt: "2023-08-27T14:59:59.059Z",
},
extensions: {
persistedQuery: {
version: 1,
sha256Hash:
"d495cb17a67b6f7a8842e10297e57dcd553ea17fe691db435e39a618fe4699cf",
},
},
},
];
const options = {
hostname: "gql.twitch.tv",
port: 443,
path: "/gql",
method: "POST",
headers: {
"Client-id": clientId,
},
};
return new Promise((resolve, reject) => {
const req = https.request(options, (response) => {
var resData = {};
resData.statusCode = response.statusCode;
resData.body = [];
response.on("data", (chunk) => resData.body.push(chunk));
response.on("end", () => {
resData.body = resData.body.join("");
if (resData.statusCode !== 200) {
reject(new Error(`${JSON.parse(data.body).message}`));
} else {
resolve(
JSON.parse(resData.body)[0].data.user.lastBroadcast.startedAt,
);
}
});
});
req.on("error", (error) => reject(error));
req.write(JSON.stringify(data));
req.end();
});
}
function getChannelPoint(channel, redacted = {}) {
const data = [
{
operationName: "ChannelPointsContext",
variables: {
channelLogin: channel,
},
extensions: {
persistedQuery: {
version: 1,
sha256Hash:
"1530a003a7d374b0380b79db0be0534f30ff46e61cffa2bc0e2468a909fbc024",
},
},
},
];
const options = {
hostname: "gql.twitch.tv",
port: 443,
path: "/gql",
method: "POST",
headers: {
"Client-id": clientId,
...redacted,
},
};
return new Promise((resolve, reject) => {
const req = https.request(options, (response) => {
var resData = {};
resData.statusCode = response.statusCode;
resData.body = [];
response.on("data", (chunk) => resData.body.push(chunk));
response.on("end", () => {
resData.body = resData.body.join("");
if (resData.statusCode !== 200) {
reject(new Error(`${JSON.parse(data.body).message}`));
} else {
resolve(
JSON.parse(resData.body)[0].data.community.channel.self
.communityPoints.balance,
);
}
});
});
req.on("error", (error) => reject(error));
req.write(JSON.stringify(data));
req.end();
});
}
module.exports = {
getStream: getStream,
getLastStreamDate: getLastStreamDate,
getChannelPoint: getChannelPoint,
};