From 8f2744ced7c922573e67489b6ff82b85696b7442 Mon Sep 17 00:00:00 2001 From: Roy Olav Purser Date: Thu, 6 May 2021 15:23:43 +0200 Subject: [PATCH] auto source --- .gitignore | 2 +- Dockerfile | 12 ++++++------ sources.py | 21 +++++++++++++-------- stream.py | 29 ++++++++++++++++++++++++----- tv.json | 6 ++++++ tv.m3u8 | 8 -------- 6 files changed, 50 insertions(+), 28 deletions(-) create mode 100644 tv.json delete mode 100644 tv.m3u8 diff --git a/.gitignore b/.gitignore index 2f91929..5c896ea 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ *.pyc -sources.m3u8 +sources.json diff --git a/Dockerfile b/Dockerfile index 4ac72e5..93ae53e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,16 +2,16 @@ FROM alpine:edge as base RUN ["apk", "add", "--no-cache", "--repository", "https://dl-cdn.alpinelinux.org/alpine/edge/testing", "streamlink", "py3-tornado"] RUN ["mkdir", "/app"] COPY ["stream.py", "/app/stream.py"] -RUN ["chmod", "755", "/app/stream.py"] - -FROM base as sources COPY ["sources.py", "/app/sources.py"] -COPY ["tv.m3u8", "/app/tv.m3u8"] +RUN ["chmod", "-R", "755", "/app"] +COPY ["tv.json", "/app/tv.json"] RUN ["python3", "/app/sources.py"] +RUN ["rm", "/app/tv.json"] +RUN ["rm", "/app/sources.py"] FROM scratch COPY --from=base / / -COPY --from=sources /app/sources.m3u8 /app/sources.m3u8 - USER 1444:1444 +ENV ICECAST_SERVER=https://icecast.purser.it:7000 +ENV STREAM_SERVER=https://stream.purser.it ENTRYPOINT ["/app/stream.py"] diff --git a/sources.py b/sources.py index 5c24772..805cb0e 100755 --- a/sources.py +++ b/sources.py @@ -1,6 +1,7 @@ #!/usr/bin/env python3 import xml.dom.minidom import requests +import json playlist = None @@ -10,7 +11,7 @@ try: except Exception as e: print(e) else: - playlist = "#EXTM3U\n" + playlist = {} mounts = dom.getElementsByTagName("mount") for mount in mounts: mount_names = mount.getElementsByTagName("mount-name") @@ -18,12 +19,16 @@ else: if len(mount_names) == 1 and len(stream_names) == 1: mount_name = mount_names[0].firstChild.nodeValue stream_name = stream_names[0].firstChild.nodeValue - playlist += f'#EXTINF:0 radio="true", {stream_name}\n' - playlist += "https://icecast.purser.it:7000" + mount_name + "\n" + value = {} + value["radio"] = True + value["name"] = stream_name + playlist[mount_name] = value if playlist is not None: - with open("/app/tv.m3u8", "r") as f: - tv = f.read() - with open("/app/sources.m3u8", "w+") as f: - f.write(playlist) - f.write(tv) + with open("/app/tv.json", "r") as f: + tv = json.loads(f.read()) + for name in tv: + playlist[name] = tv[name] + + with open("/app/sources.json", "w+") as f: + f.write(json.dumps(playlist)) diff --git a/stream.py b/stream.py index e63b451..3bd885e 100755 --- a/stream.py +++ b/stream.py @@ -3,6 +3,7 @@ import streamlink import tornado.web import tornado.routing import requests +import json import os providers = {} @@ -23,8 +24,26 @@ for key in proxies: if proxy is not None: proxies[key].set_option("https-proxy", proxy) +playlist = None +icecast_server = os.environ.get("ICECAST_SERVER") +stream_server = os.environ.get("STREAM_SERVER") +if icecast_server is not None and stream_server is not None: + with open("/app/sources.json", "r") as f: + data = json.loads(f.read()) + playlist = "#EXTM3U\n" + for key in data: + current = data[key] + name = current["name"] + radio = current["radio"] + if radio: + playlist += f'#EXTINF:0 radio="true", {name}\n' + playlist += icecast_server + key + "\n" + else: + playlist += f'#EXTINF:0 radio="false", {name}\n' + playlist += stream_server + key + "\n" + class MainHandler(tornado.web.RequestHandler): - def handle_any(self, write): + def handle_any(self): provider = self.get_query_argument("provider", None) endpoint = None if provider is not None and provider in providers.keys(): @@ -52,15 +71,15 @@ class MainHandler(tornado.web.RequestHandler): else: self.redirect(endpoint, status=303) def get(self): - self.handle_any(True) + self.handle_any() def head(self): - self.handle_any(False) + self.handle_any() class FileHandler(tornado.web.RequestHandler): def get(self): self.set_header("Content-Type", "text/plain; charset=utf-8") - with open("/app/sources.m3u8", "r") as f: - self.write(f.read()) + self.write(playlist) + try: handlers = [] handlers.append((tornado.routing.PathMatches("/sources.m3u8"), FileHandler)) diff --git a/tv.json b/tv.json new file mode 100644 index 0000000..c501948 --- /dev/null +++ b/tv.json @@ -0,0 +1,6 @@ +{ + "/direkte/nrk1?provider=nrk": {"radio": false, "name": "NRK 1"}, + "/direkte/nrk2?provider=nrk": {"radio": false, "name": "NRK 2"}, + "/direkte/nrk3?provider=nrk": {"radio": false, "name": "NRK 3"}, + "/direkte/nrksuper?provider=nrk": {"radio": false, "name": "NRK Super"} +} diff --git a/tv.m3u8 b/tv.m3u8 deleted file mode 100644 index b5cb64d..0000000 --- a/tv.m3u8 +++ /dev/null @@ -1,8 +0,0 @@ -#EXTINF:0 radio="false", NRK 1 -https://stream.purser.it/direkte/nrk1?provider=nrk -#EXTINF:0 radio="false", NRK 2 -https://stream.purser.it/direkte/nrk2?provider=nrk -#EXTINF:0 radio="false", NRK 3 -https://stream.purser.it/direkte/nrk3?provider=nrk -#EXTINF:0 radio="false", NRK Super -https://stream.purser.it/direkte/nrksuper?provider=nrk