diff --git a/backend/stream_providers.py b/backend/stream_providers.py index 47f2ce5..11d0a57 100755 --- a/backend/stream_providers.py +++ b/backend/stream_providers.py @@ -3,6 +3,7 @@ import streamlink import requests import asyncio import html.parser +import urllib.parse import expiringdict import json @@ -102,6 +103,21 @@ class StreamProvider(): proxy = str(proxy) if len(proxy) > 5: self.proxy = proxy + def extract_mime(self, upstream): + try: + url = urllib.parse.urlparse(upstream) + query = urllib.parse.parse_qs(url.query.lower()) + except Exception as e: + self.logger.info(e) + else: + mime_types = query.get("mime") + if isinstance(mime_types, list): + for mime in mime_types: + if isinstance(mime, str) and not mime.startswith("text"): + if "mpegurl" in mime: + return "application/vnd.apple.mpegurl" + return mime + return None def init_stream(self): stream = {} stream["upstream"] = None @@ -113,8 +129,6 @@ class StreamProvider(): 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 @@ -160,7 +174,7 @@ class StreamlinkRunner(StreamProvider): for key in reversed(streams): stream = streams.get(key) if hasattr(stream, "url"): - return StreamData(stream.url, None, None, None, None, False) + return StreamData(stream.url, self.extract_mime(stream.url), None, None, None, False) except Exception as e: self.logger.info("%s <%s>", e, self.upstream) return StreamData(None, None, None, None, None, False) @@ -203,6 +217,7 @@ class YoutubeRunner(StreamProvider): best_format = vformat best_stream["override"] = True best_stream["upstream"] = new_url + best_stream["ctype"] = self.extract_mime(new_url) except Exception as e: self.logger.info("%s <%s>", e, self.upstream) return StreamData(**best_stream)