const twitch = require("twitch-m3u8");
var tonicExpress = require("@runkit/runkit/express-endpoint/1.0.0");
var app = tonicExpress(module.exports);
app.get('/', async(req, res) => {
let quality = parseInt(req.query.q, 10);//0 = source
let channel = req.query.c;
if ( channel == "" || channel === undefined ){
console.log(channel,'channel undefined');
res.end('url/?c=channel&q=quality //0 = source');
return;
};
//console.log(channel, quality);
let streams = await twitch.getStream(channel);
if (quality >= 0){
console.log(`redirecting to ${channel} quality ${quality}`)
res.redirect(streams[quality].url);
} else {
console.log(`Creating html for ${channel}`);
let html = `<!DOCTYPE html>
<head>
<title>Twitch m3u8 - ${channel}</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Raleway">
<link rel="icon" type="image/png" sizes="32x32" href="https://static.twitchcdn.net/assets/favicon-32-e29e246c157142c94346.png">
<link rel="icon" type="image/png" sizes="16x16" href="https://static.twitchcdn.net/assets/favicon-16-52e571ffea063af7a7f4.png">
<style>
body,h1 {font-family: "Raleway", sans-serif}
body, html {height: 100%}
.bgimg {
background-color: rgb(107, 91, 149);
min-height: 100%;
//background-image: url('https://wallpaperaccess.com/full/268724.jpg');
//background-position: center;
//background-size: cover;
}
</style>
</head>
<body>
<div class="bgimg w3-display-container w3-animate-opacity w3-text-white">
<div class="w3-display-topleft w3-padding-large w3-xlarge w3-hover-text-black">
<img src="https://static.twitchcdn.net/assets/favicon-32-e29e246c157142c94346.png" alt="Icon" width="32" height="32">
<a style="text-decoration:none" href="https://runkit.com/alexbestbro/twitch" target="_blank">Twitch m3u8</a>
</div>
<div class="w3-display-middle w3-center" style="margin-top: 10px;">
<h1 class="w3-xxxlarge w3-animate-top w3-hover-text-black">
<a style="text-decoration:none" href="https://www.twitch.tv/${channel}" target="_blank">
<b>${channel}</b>
</a>
</h1>
<hr class="w3-border-white" style="margin:auto;width:100%">
<p class="w3-large w3-center w3-mobile w3-animate-bottom">`
for (let x of streams){
html += `<a class="w3-mobile w3-hover-text-black" style="text-decoration:none" href="javascript:window.open('${x.url}','_blank','toolbar=0,width=1498,height=988,left=0');">${x.quality}</a> `
}
html += `</p>
<p class="w3-large w3-center w3-mobile w3-animate-bottom">
<a class="w3-mobile w3-hover-text-black" style="text-decoration:none" href="javascript:window.open('https://www.twitch.tv/popout/${channel}/chat?popout=','_blank','toolbar=no,width=400,height=988,left=1500');">Chat_only</a> 
<a class="w3-mobile w3-hover-text-black" style="text-decoration:none" href="javascript:window.open('https://runkit.io/alexbestbro/twitch-test/1.0.0/?c=${channel}&q=0','_self');">Source+Chat</a>
</p>
</div>
</div>
</body>
</html>`
res.end(html);
}
});