add sources

This commit is contained in:
Roy Olav Purser 2021-05-01 20:01:24 +02:00
parent 7374cfed9d
commit 9b46ae4683
No known key found for this signature in database
GPG Key ID: 0BA77797F072BC52
5 changed files with 59 additions and 9 deletions

1
.gitignore vendored
View File

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

View File

@ -2,9 +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 ["python3", "/app/sources.py"]
FROM scratch
COPY --from=base / /
COPY --from=sources /app/sources.m3u8 /app/sources.m3u8
USER 1444:1444
ENTRYPOINT ["/app/stream.py"]

29
sources.py Executable file
View File

@ -0,0 +1,29 @@
#!/usr/bin/env python3
import xml.dom.minidom
import requests
playlist = None
try:
resp = requests.get("https://git.purser.it/roypur/icecast-relay/raw/branch/master/icecast.xml")
dom = xml.dom.minidom.parseString(resp.text)
except Exception as e:
print(e)
else:
playlist = "#EXTM3U\n"
mounts = dom.getElementsByTagName("mount")
for mount in mounts:
mount_names = mount.getElementsByTagName("mount-name")
stream_names = mount.getElementsByTagName("stream-name")
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"
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)

View File

@ -35,9 +35,7 @@ class MainHandler(tornado.web.RequestHandler):
if resp is not None:
src = resp.url
except Exception as e:
if write:
self.write(e)
return
print(e)
else:
try:
streams = proxy.streams(src)
@ -47,20 +45,27 @@ class MainHandler(tornado.web.RequestHandler):
endpoint = stream.url
break
except Exception as e:
if write:
self.write(str(e))
return
print(str(e))
endpoint = None
if endpoint is None:
if write:
self.write("stream not found")
self.set_status(404, reason="stream not found")
else:
self.redirect(endpoint, status=303)
def get(self):
self.handle_any(True)
def head(self):
self.handle_any(False)
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())
try:
app_web = tornado.web.Application([(tornado.routing.AnyMatches(), MainHandler)])
handlers = []
handlers.append((tornado.routing.PathMatches("/sources.m3u8"), FileHandler))
handlers.append((tornado.routing.AnyMatches(), MainHandler))
app_web = tornado.web.Application(handlers)
app_web.listen(8080)
tornado.ioloop.IOLoop.current().start()
except KeyboardInterrupt:

8
tv.m3u8 Normal file
View File

@ -0,0 +1,8 @@
#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