From e449f109bc2d80ada48c5b569833f8c0ff201e73 Mon Sep 17 00:00:00 2001 From: juju2143 Date: Mon, 28 Aug 2023 22:58:40 -0400 Subject: [PATCH] Add Twitch api for iptv --- .gitignore | 3 ++- netlify.toml | 5 +++++ netlify/functions/twitch.js | 37 +++++++++++++++++++++++++++++++++++++ package-lock.json | 17 +++++++++++++++++ package.json | 5 +++++ 5 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 netlify/functions/twitch.js create mode 100644 package-lock.json create mode 100644 package.json diff --git a/.gitignore b/.gitignore index f141c94..08a2e28 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ /assets/jsconfig.json hugo_stats.json .hugo_build.lock -.netlify \ No newline at end of file +.netlify +node_modules \ No newline at end of file diff --git a/netlify.toml b/netlify.toml index a46f46d..abf2a36 100644 --- a/netlify.toml +++ b/netlify.toml @@ -11,3 +11,8 @@ command = "hugo server" port = 3000 publish = "public" + +[[redirects]] + from="/api/*" + to="/.netlify/functions/:splat" + status = 200 \ No newline at end of file diff --git a/netlify/functions/twitch.js b/netlify/functions/twitch.js new file mode 100644 index 0000000..f936e6e --- /dev/null +++ b/netlify/functions/twitch.js @@ -0,0 +1,37 @@ +const twitch = require('twitch-m3u8') + +exports.handler = async function (event, context) { + const user = event.queryStringParameters?.user; + if(user) + { + try + { + var playlist = await twitch.getStream(user, true); + return { headers: {"content-type":"application/vnd.apple.mpegurl; charset=utf-8"}, statusCode: 200, body: playlist }; + } + catch(err) + { + return { headers: {"content-type":"text/plain"}, statusCode: 404, body: "user "+user+" not found" }; + } + } + else + { + const vod = event.queryStringParameters?.vod; + if(vod) + { + try + { + var playlist = await twitch.getVod(vod, true); + return { headers: {"content-type":"application/vnd.apple.mpegurl; charset=utf-8"}, statusCode: 200, body: playlist }; + } + catch(err) + { + return { headers: {"content-type":"text/plain"}, statusCode: 404, body: "vod "+vod+" not found" }; + } + } + else + { + return { headers: {"content-type":"text/plain"}, statusCode: 404, body: "Usage:\ntwitch?user=\ntwitch?vod=" }; + } + } +}; \ No newline at end of file diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..4d41cd2 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,17 @@ +{ + "name": "functions", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "dependencies": { + "twitch-m3u8": "^1.1.5" + } + }, + "node_modules/twitch-m3u8": { + "version": "1.1.5", + "resolved": "https://registry.npmjs.org/twitch-m3u8/-/twitch-m3u8-1.1.5.tgz", + "integrity": "sha512-o037ePS9fwfuOmBe/ftji/e3cqzWq50AUvuIHJZ5E0pV73UMjn3BAm00aZUYVrt3Ns6k08HvhVjAIya/WYcGRg==" + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..b5a7f38 --- /dev/null +++ b/package.json @@ -0,0 +1,5 @@ +{ + "dependencies": { + "twitch-m3u8": "^1.1.5" + } +}