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