-
Notifications
You must be signed in to change notification settings - Fork 0
/
twitch-schedule.js
89 lines (67 loc) · 2.74 KB
/
twitch-schedule.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
// Scrapes a twitch streamers schedule and generates an ics
const icalToolkit = require('ical-toolkit');
const axios = require('axios');
const fs = require('fs');
var builder = icalToolkit.createIcsFileBuilder();
builder.spacers = true; //Add space in ICS file, better human reading. Default: true
builder.NEWLINE_CHAR = '\n'; //Newline char to use.
builder.throwError = false; //If true throws errors, else returns error when you do .toString() to generate the file contents.
builder.ignoreTZIDMismatch = true; //If TZID is invalid, ignore or not to ignore!
builder.calname = 'Twitch';
builder.method = 'REQUEST';
async function getSchedule(channelName, callback) {
return callback(axios.post('https://gql.twitch.tv/gql', '[{"operationName":"StreamSchedule","variables":{"login":"' + channelName + '","startingWeekday":"MONDAY","utcOffsetMinutes":120},"extensions":{"persistedQuery":{"version":1,"sha256Hash":"d77c052e84dfeb3ca1c3633b41cda0fa988577a56c519f43d3142bc99de83ac5"}}}]', { headers: { "Client-Id": "kimne78kx3ncx6brgo4mv6wki5h1ko" } })
.then((res) => {
let schedule = res.data[0]["data"]["user"]["channel"]["schedule"]
let segments = schedule["segments"]
let nextStart = new Date(schedule["nextSegment"]["startAt"])
let interruption = schedule["interruption"]
var interruptionStart = new Date()
var interruptionEnd = new Date()
if (interruption) {
interruptionStart = new Date(interruption["startAt"])
interruptionEnd = new Date(interruption["endAt"])
}
segments.map(segment => {
let startDate = new Date(segment["startAt"])
let endDate = new Date(segment["endAt"])
var title = segment["title"]
if (title == "") {
if (segment["categories"].length > 0) {
title = segment["categories"][0]["name"]
} else {
title = "Stream"
}
}
if (segment["isCancelled"]) {
title = title + "(Cancelled)"
}
if (!interruption || !(startDate >= interruptionStart && endDate <= interruptionEnd)) {
builder.events.push({
start: startDate,
end: endDate,
summary: title,
location: 'http://twitch.tv/' + channelName,
url: 'http://twitch.tv/' + channelName
});
}
})
}).catch((err) => {
console.error(err);
}));
}
const http = require('http');
http.createServer(async (req, res) => {
builder.events = []
const getSchedule1 = (channelName) => new Promise(resolve => getSchedule(
channelName,
function(res) { resolve(res); }));
result = await getSchedule1("warframe")
//res.toString()
result = await getSchedule1("warframeinternational")
//res.toString()
var icsFileContent = builder.toString();
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.write(icsFileContent);
res.end();
}).listen(process.env.PORT || 8080);