auto source

This commit is contained in:
Roy Olav Purser 2021-05-06 15:23:43 +02:00
parent 9b46ae4683
commit 8f2744ced7
Signed by: roypur
GPG Key ID: E14D26A036F21656
6 changed files with 50 additions and 28 deletions

2
.gitignore vendored
View File

@ -1,2 +1,2 @@
*.pyc *.pyc
sources.m3u8 sources.json

View File

@ -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 ["apk", "add", "--no-cache", "--repository", "https://dl-cdn.alpinelinux.org/alpine/edge/testing", "streamlink", "py3-tornado"]
RUN ["mkdir", "/app"] RUN ["mkdir", "/app"]
COPY ["stream.py", "/app/stream.py"] COPY ["stream.py", "/app/stream.py"]
RUN ["chmod", "755", "/app/stream.py"]
FROM base as sources
COPY ["sources.py", "/app/sources.py"] 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 ["python3", "/app/sources.py"]
RUN ["rm", "/app/tv.json"]
RUN ["rm", "/app/sources.py"]
FROM scratch FROM scratch
COPY --from=base / / COPY --from=base / /
COPY --from=sources /app/sources.m3u8 /app/sources.m3u8
USER 1444:1444 USER 1444:1444
ENV ICECAST_SERVER=https://icecast.purser.it:7000
ENV STREAM_SERVER=https://stream.purser.it
ENTRYPOINT ["/app/stream.py"] ENTRYPOINT ["/app/stream.py"]

View File

@ -1,6 +1,7 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import xml.dom.minidom import xml.dom.minidom
import requests import requests
import json
playlist = None playlist = None
@ -10,7 +11,7 @@ try:
except Exception as e: except Exception as e:
print(e) print(e)
else: else:
playlist = "#EXTM3U\n" playlist = {}
mounts = dom.getElementsByTagName("mount") mounts = dom.getElementsByTagName("mount")
for mount in mounts: for mount in mounts:
mount_names = mount.getElementsByTagName("mount-name") mount_names = mount.getElementsByTagName("mount-name")
@ -18,12 +19,16 @@ else:
if len(mount_names) == 1 and len(stream_names) == 1: if len(mount_names) == 1 and len(stream_names) == 1:
mount_name = mount_names[0].firstChild.nodeValue mount_name = mount_names[0].firstChild.nodeValue
stream_name = stream_names[0].firstChild.nodeValue stream_name = stream_names[0].firstChild.nodeValue
playlist += f'#EXTINF:0 radio="true", {stream_name}\n' value = {}
playlist += "https://icecast.purser.it:7000" + mount_name + "\n" value["radio"] = True
value["name"] = stream_name
playlist[mount_name] = value
if playlist is not None: if playlist is not None:
with open("/app/tv.m3u8", "r") as f: with open("/app/tv.json", "r") as f:
tv = f.read() tv = json.loads(f.read())
with open("/app/sources.m3u8", "w+") as f: for name in tv:
f.write(playlist) playlist[name] = tv[name]
f.write(tv)
with open("/app/sources.json", "w+") as f:
f.write(json.dumps(playlist))

View File

@ -3,6 +3,7 @@ import streamlink
import tornado.web import tornado.web
import tornado.routing import tornado.routing
import requests import requests
import json
import os import os
providers = {} providers = {}
@ -23,8 +24,26 @@ for key in proxies:
if proxy is not None: if proxy is not None:
proxies[key].set_option("https-proxy", proxy) 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): class MainHandler(tornado.web.RequestHandler):
def handle_any(self, write): def handle_any(self):
provider = self.get_query_argument("provider", None) provider = self.get_query_argument("provider", None)
endpoint = None endpoint = None
if provider is not None and provider in providers.keys(): if provider is not None and provider in providers.keys():
@ -52,15 +71,15 @@ class MainHandler(tornado.web.RequestHandler):
else: else:
self.redirect(endpoint, status=303) self.redirect(endpoint, status=303)
def get(self): def get(self):
self.handle_any(True) self.handle_any()
def head(self): def head(self):
self.handle_any(False) self.handle_any()
class FileHandler(tornado.web.RequestHandler): class FileHandler(tornado.web.RequestHandler):
def get(self): def get(self):
self.set_header("Content-Type", "text/plain; charset=utf-8") self.set_header("Content-Type", "text/plain; charset=utf-8")
with open("/app/sources.m3u8", "r") as f: self.write(playlist)
self.write(f.read())
try: try:
handlers = [] handlers = []
handlers.append((tornado.routing.PathMatches("/sources.m3u8"), FileHandler)) handlers.append((tornado.routing.PathMatches("/sources.m3u8"), FileHandler))

6
tv.json Normal file
View File

@ -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"}
}

View File

@ -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