debug print with lines

This commit is contained in:
Roy Olav Purser 2021-05-13 09:16:47 +02:00
parent afe5195ef0
commit 87ee073660
Signed by: roypur
GPG Key ID: E14D26A036F21656

View File

@ -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,14 +237,15 @@ 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]
try: if isinstance(proxy_server, str):
resp = requests.post(proxy_server, json=links) try:
if isinstance(resp.text, str): resp = requests.post(proxy_server, json=links)
new_links = json.loads(resp.text) if isinstance(resp.text, str):
if isinstance(new_links, list) and len(new_links) == 1: new_links = json.loads(resp.text)
upstream = new_links.pop() if isinstance(new_links, list) and len(new_links) == 1:
except Exception as e: upstream = new_links.pop()
print(e) except Exception as 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)