From cdc28ebc132003656d13b4a19119cca560e772cf Mon Sep 17 00:00:00 2001 From: Roy Olav Purser Date: Thu, 3 Jun 2021 13:04:26 +0200 Subject: [PATCH] print video link in error --- backend/stream_providers.py | 95 ++++++++++++++++++------------------- 1 file changed, 46 insertions(+), 49 deletions(-) diff --git a/backend/stream_providers.py b/backend/stream_providers.py index a0f7158..9901c2a 100755 --- a/backend/stream_providers.py +++ b/backend/stream_providers.py @@ -23,26 +23,6 @@ class DummyLogger(): pass class MetaParser(html.parser.HTMLParser): - def __init__(self): - self.meta_data = {} - self.accepted_attrs = [] - self.accepted_attrs.append("og:title") - self.accepted_attrs.append("og:description") - self.accepted_attrs.append("og:image") - super().__init__() - def handle_starttag(self, tag, attrs): - if tag == "meta": - name = None - for attr in (attrs + attrs): - if len(attr) == 2: - if isinstance(name, str): - if attr[0] == "content": - self.meta_data[name] = attr[1] - return - elif attr[0] == "property" and attr[1] in self.accepted_attrs: - name = attr[1] - -class NextcloudParser(html.parser.HTMLParser): def __init__(self): self.meta_data = {} super().__init__() @@ -117,8 +97,19 @@ class StreamProvider(): proxy = str(proxy) if len(proxy) > 5: self.proxy = proxy + def init_stream(self): + stream = {} + stream["upstream"] = None + stream["ctype"] = None + stream["thumbnail"] = None + stream["title"] = None + stream["description"] = None + stream["override"] = False + return stream def process(self): data = self.stream() + if not isinstance(data, StreamData): + return self.init_stream() if not isinstance(data.upstream(), str) or isinstance(data.ctype(), str): return data proxies = None @@ -130,7 +121,7 @@ class StreamProvider(): try: resp = requests.head(data.upstream(), proxies=proxies, timeout=5) except Exception as e: - self.logger.info(e) + self.logger.info("%s <%s>", e, self.upstream) else: ctype = resp.headers.get("Content-Type", "binary/octet-stream") if "mpegurl" in ctype.lower(): @@ -142,7 +133,7 @@ class StreamProvider(): future = asyncio.to_thread(self.process) data = await asyncio.wait_for(future, timeout=5) except Exception as e: - self.logger.info(e) + self.logger.info("%s <%s>", e, self.upstream) return data class StreamlinkRunner(StreamProvider): @@ -161,15 +152,12 @@ class StreamlinkRunner(StreamProvider): if hasattr(stream, "url"): return StreamData(stream.url, None, None, None, None, False) except Exception as e: - self.logger.info(e) + self.logger.info("%s <%s>", e, self.upstream) return StreamData(None, None, None, None, None, False) class YoutubeRunner(StreamProvider): def stream(self): - best_url = None - thumbnail = None - title = None - description = None + best_stream = self.init_stream() try: opts = {} opts["logger"] = DummyLogger() @@ -178,9 +166,9 @@ class YoutubeRunner(StreamProvider): with youtube_dl.YoutubeDL(opts) as ydl: info = ydl.extract_info(self.upstream, download=False) vformats = info.get("formats") - thumbnail = info.get("thumbnail") - description = info.get("channel") - title = info.get("title") + best_stream["thumbnail"] = info.get("thumbnail") + best_stream["description"] = info.get("channel") + best_stream["title"] = info.get("title") best_format = {} best_format["width"] = 10 best_format["height"] = 10 @@ -203,25 +191,15 @@ class YoutubeRunner(StreamProvider): acodec != "none" and vcodec != "none"): best_format = vformat - best_url = new_url + best_stream["override"] = True + best_stream["upstream"] = new_url except Exception as e: - self.logger.info(e) - return StreamData(best_url, None, thumbnail, title, description, True) + self.logger.info("%s <%s>", e, self.upstream) + return StreamData(**best_stream) -class NextcloudRunner(StreamProvider): - def stream(self): - data = {} - try: - resp = requests.get(self.upstream) - parser = NextcloudParser() - parser.feed(resp.text) - data = parser.meta_data - except Exception as e: - self.logger.info(e) - return StreamData(data.get("downloadURL"), data.get("mimetype"), None, data.get("og:title"), data.get("og:description"), False) - -class MetaRunner(StreamProvider): - def stream(self): +class MetaProvider(StreamProvider): + def parse_web(self): + stream_data = self.init_stream() data = {} try: resp = requests.get(self.upstream) @@ -229,8 +207,27 @@ class MetaRunner(StreamProvider): parser.feed(resp.text) data = parser.meta_data except Exception as e: - self.logger.info(e) - return StreamData(None, None, data.get("og:image"), data.get("og:title"), data.get("og:description"), False) + self.logger.info("%s <%s>", e, self.upstream) + else: + stream_data["upstream"] = data.get("downloadURL") + stream_data["ctype"] = data.get("mimetype") + stream_data["title"] = data.get("og:title") + stream_data["thumbnail"] = data.get("og:image") + stream_data["description"] = data.get("og:description") + return stream_data + +class MetaRunner(MetaProvider): + def stream(self): + stream_data = self.parse_web() + stream_data["upstream"] = None + stream_data["ctype"] = None + return StreamData(**stream_data) + +class NextcloudRunner(MetaProvider): + def stream(self): + stream_data = self.parse_web() + stream_data["thumbnail"] = None + return StreamData(**stream_data) upstream_cache = expiringdict.ExpiringDict(max_len=512, max_age_seconds=1800)