debug print with lines
This commit is contained in:
parent
afe5195ef0
commit
87ee073660
35
stream.py
35
stream.py
@ -8,6 +8,10 @@ import tornado.web
|
|||||||
import tornado.routing
|
import tornado.routing
|
||||||
import requests
|
import requests
|
||||||
import base64
|
import base64
|
||||||
|
import logging
|
||||||
|
|
||||||
|
logging.basicConfig(format='[%(filename)s:%(lineno)d] %(message)s', level=logging.INFO)
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
providers = {}
|
providers = {}
|
||||||
providers["nrk"] = "https://tv.nrk.no"
|
providers["nrk"] = "https://tv.nrk.no"
|
||||||
@ -77,7 +81,7 @@ try:
|
|||||||
with open("/app/castjs-version.txt", "r") as f:
|
with open("/app/castjs-version.txt", "r") as f:
|
||||||
castjs_version = f.read().strip()
|
castjs_version = f.read().strip()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(e)
|
logger.info(e)
|
||||||
|
|
||||||
def get_proxy_url(proxy, current, path):
|
def get_proxy_url(proxy, current, path):
|
||||||
data = {}
|
data = {}
|
||||||
@ -153,7 +157,7 @@ class MainHandler(tornado.web.RequestHandler):
|
|||||||
else:
|
else:
|
||||||
self.handle_stream(provider, write)
|
self.handle_stream(provider, write)
|
||||||
else:
|
else:
|
||||||
print("provider missing")
|
logger.info(f'provider missing {self.request.uri}')
|
||||||
self.set_status(404)
|
self.set_status(404)
|
||||||
if write:
|
if write:
|
||||||
self.write("Stream not found. (provider missing)")
|
self.write("Stream not found. (provider missing)")
|
||||||
@ -189,14 +193,13 @@ class MainHandler(tornado.web.RequestHandler):
|
|||||||
current_list = proxy_list.copy()
|
current_list = proxy_list.copy()
|
||||||
current = proxy_list.pop()
|
current = proxy_list.pop()
|
||||||
proxy_list = [current] + proxy_list
|
proxy_list = [current] + proxy_list
|
||||||
print(proxy_list)
|
|
||||||
try:
|
try:
|
||||||
resp = requests.head(src, allow_redirects=True, proxies=current.req, timeout=2)
|
resp = requests.head(src, allow_redirects=True, proxies=current.req, timeout=2)
|
||||||
if resp is not None:
|
if resp is not None:
|
||||||
print(src)
|
logger.info(src)
|
||||||
src = resp.url
|
src = resp.url
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(e)
|
logger.info(e)
|
||||||
else:
|
else:
|
||||||
proxies[provider] = current_list
|
proxies[provider] = current_list
|
||||||
proxy = current
|
proxy = current
|
||||||
@ -206,17 +209,23 @@ class MainHandler(tornado.web.RequestHandler):
|
|||||||
streams = proxy.stream.streams(src)
|
streams = proxy.stream.streams(src)
|
||||||
for key in reversed(streams):
|
for key in reversed(streams):
|
||||||
stream = streams.get(key)
|
stream = streams.get(key)
|
||||||
print(stream)
|
logger.info(stream)
|
||||||
if hasattr(stream, "url"):
|
if hasattr(stream, "url"):
|
||||||
upstream = stream.url
|
upstream = stream.url
|
||||||
break
|
break
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(e)
|
logger.info(e)
|
||||||
if upstream is None:
|
else:
|
||||||
print(f'invalid provider ({provider})')
|
logger.info(f'invalid provider ({provider})')
|
||||||
self.set_status(404)
|
self.set_status(404)
|
||||||
if write:
|
if write:
|
||||||
self.write("Stream not found. (invalid provider)")
|
self.write("Stream not found. (invalid provider)")
|
||||||
|
return
|
||||||
|
if upstream is None:
|
||||||
|
logger.info(f'invalid upstream ({provider})')
|
||||||
|
self.set_status(404)
|
||||||
|
if write:
|
||||||
|
self.write("Stream not found. (invalid upstream)")
|
||||||
else:
|
else:
|
||||||
ctype = upstream_type(upstream, proxy)
|
ctype = upstream_type(upstream, proxy)
|
||||||
data = None
|
data = None
|
||||||
@ -228,6 +237,7 @@ class MainHandler(tornado.web.RequestHandler):
|
|||||||
ldata["proxy"] = proxy.proxy
|
ldata["proxy"] = proxy.proxy
|
||||||
ldata["proxied"] = isinstance(proxy.proxy, str)
|
ldata["proxied"] = isinstance(proxy.proxy, str)
|
||||||
links = [ldata]
|
links = [ldata]
|
||||||
|
if isinstance(proxy_server, str):
|
||||||
try:
|
try:
|
||||||
resp = requests.post(proxy_server, json=links)
|
resp = requests.post(proxy_server, json=links)
|
||||||
if isinstance(resp.text, str):
|
if isinstance(resp.text, str):
|
||||||
@ -235,7 +245,7 @@ class MainHandler(tornado.web.RequestHandler):
|
|||||||
if isinstance(new_links, list) and len(new_links) == 1:
|
if isinstance(new_links, list) and len(new_links) == 1:
|
||||||
upstream = new_links.pop()
|
upstream = new_links.pop()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(e)
|
logger.info(e)
|
||||||
if data is None:
|
if data is None:
|
||||||
self.redirect(upstream, status=303)
|
self.redirect(upstream, status=303)
|
||||||
else:
|
else:
|
||||||
@ -251,9 +261,14 @@ class FileHandler(tornado.web.RequestHandler):
|
|||||||
self.set_header("Content-Type", "text/plain; charset=utf-8")
|
self.set_header("Content-Type", "text/plain; charset=utf-8")
|
||||||
self.write(playlist)
|
self.write(playlist)
|
||||||
|
|
||||||
|
class IconHandler(tornado.web.RequestHandler):
|
||||||
|
def get(self):
|
||||||
|
self.set_header("Content-Type", "text/plain; charset=utf-8")
|
||||||
|
self.set_status(204)
|
||||||
try:
|
try:
|
||||||
handlers = []
|
handlers = []
|
||||||
handlers.append((tornado.routing.PathMatches("/sources.m3u8"), FileHandler))
|
handlers.append((tornado.routing.PathMatches("/sources.m3u8"), FileHandler))
|
||||||
|
handlers.append((tornado.routing.PathMatches("/favicon.ico"), IconHandler))
|
||||||
handlers.append((tornado.routing.AnyMatches(), MainHandler))
|
handlers.append((tornado.routing.AnyMatches(), MainHandler))
|
||||||
app_web = tornado.web.Application(handlers)
|
app_web = tornado.web.Application(handlers)
|
||||||
app_web.listen(8080)
|
app_web.listen(8080)
|
||||||
|
Loading…
Reference in New Issue
Block a user