Created
July 12, 2020 11:49
-
-
Save m4p/1c0c32215bc9f1feb31012773db8076d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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, displayName) { | |
return 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: displayName, | |
url: 'http://twitch.tv/' + channelName | |
}); | |
} | |
}) | |
}).catch((err) => { | |
console.error(err); | |
}); | |
} | |
(async () => { | |
await getSchedule("sergeyager", "Serge") | |
await getSchedule("bengineering", "Ben") | |
await getSchedule("james_lrr", "James") | |
var icsFileContent = builder.toString(); | |
fs.writeFile("twitch.ics", icsFileContent, function (err) { | |
if (err) return console.log(err); | |
}); | |
})(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment