stream-api/backend/stream_providers.py

211 lines
7.7 KiB
Python
Raw Normal View History

2021-05-20 11:09:00 +00:00
import youtube_dl
import streamlink
2021-05-28 13:57:19 +00:00
import requests
2021-05-20 11:09:00 +00:00
import asyncio
2021-05-28 13:57:19 +00:00
import html.parser
import re
ytimg_pattern = re.compile(r'(https:\/\/[a-z0-9.]+ytimg\.com\/.+\/)[a-z0-9]+(\.[a-z0-9]+)')
2021-05-20 11:09:00 +00:00
class DummyLogger():
def debug(self, msg):
pass
def warning(self, msg):
pass
def error(self, msg):
pass
2021-05-28 13:57:19 +00:00
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 StreamData():
def __init__(self, upstream, thumbnail, title, description, override):
self.values = {}
self.values["upstream"] = upstream
self.values["thumbnail"] = thumbnail
self.values["title"] = title
self.values["description"] = description
self.override = override
def update(self, key, value, override):
missing = not isinstance(self.values.get(key), str)
override = override and isinstance(value, str)
if missing or override:
self.values[key] = value
def upstream(self):
return self.values.get("upstream")
def thumbnail(self):
return self.values.get("thumbnail")
def title(self):
return self.values.get("title")
def description(self):
return self.values.get("description")
def meta(self):
data = []
if isinstance(self.values.get("thumbnail"), str):
data.append(("og:image", self.values.get("thumbnail")))
if isinstance(self.values.get("title"), str):
data.append(("og:title", self.values.get("title")))
if isinstance(self.values.get("description"), str):
data.append(("og:description", self.values.get("description")))
return data
2021-05-20 11:09:00 +00:00
class StreamProvider():
2021-05-28 13:57:19 +00:00
def __init__(self, upstream, proxy, logger):
2021-05-20 11:09:00 +00:00
self.upstream = upstream
self.proxy = None
2021-05-28 13:57:19 +00:00
self.logger = logger
2021-05-20 11:09:00 +00:00
proxy = str(proxy)
if len(proxy) > 5:
self.proxy = "socks5://" + proxy
class StreamlinkRunner(StreamProvider):
def stream(self):
2021-05-28 13:57:19 +00:00
try:
session = streamlink.Streamlink()
if self.proxy is not None:
session.set_option("https-proxy", self.proxy)
session.set_option("http-proxy", self.proxy)
streams = session.streams(self.upstream)
if streams is not None:
for key in reversed(streams):
stream = streams.get(key)
if hasattr(stream, "url"):
return StreamData(stream.url, None, None, None, False)
except Exception as e:
self.logger.info(e)
return StreamData(None, None, None, None, False)
2021-05-20 11:09:00 +00:00
async def run(self):
return await asyncio.to_thread(self.stream)
class YoutubeRunner(StreamProvider):
def stream(self):
best_url = None
2021-05-28 13:57:19 +00:00
thumbnail = None
title = None
description = None
try:
opts = {}
opts["logger"] = DummyLogger()
if isinstance(self.proxy, str):
opts["proxy"] = self.proxy
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_format = {}
best_format["width"] = 10
best_format["height"] = 10
if isinstance(vformats, list):
for vformat in vformats:
acodec = vformat.get("acodec")
vcodec = vformat.get("vcodec")
current_width = vformat.get("height")
current_height = vformat.get("width")
best_width = best_format.get("width")
best_height = best_format.get("height")
new_url = vformat.get("url")
if (isinstance(best_width, int) and
isinstance(best_height, int) and
isinstance(current_width, int) and
isinstance(current_height, int) and
isinstance(new_url, str) and
current_width > best_width and
current_height > best_height and
acodec != "none" and
vcodec != "none"):
best_format = vformat
best_url = new_url
except Exception as e:
self.logger.info(e)
return StreamData(best_url, thumbnail, title, description, True)
async def run(self):
return await asyncio.to_thread(self.stream)
class MetaRunner(StreamProvider):
def stream(self):
data = {}
try:
resp = requests.get(self.upstream)
parser = MetaParser()
parser.feed(resp.text)
data = parser.meta_data
except Exception as e:
self.logger.info(e)
return StreamData(None, data.get("og:image"), data.get("og:title"), data.get("og:description"), False)
2021-05-20 11:09:00 +00:00
async def run(self):
return await asyncio.to_thread(self.stream)
2021-05-20 11:46:52 +00:00
async def get_ytdl(upstream, proxy, logger):
2021-05-26 17:15:12 +00:00
result = None
try:
2021-05-28 13:57:19 +00:00
runner = YoutubeRunner(upstream, proxy, logger)
2021-05-26 17:15:12 +00:00
result_temp = await runner.run()
except Exception as e:
logger.info(e)
else:
result = result_temp
2021-05-25 07:39:34 +00:00
return result
2021-05-20 11:46:52 +00:00
async def get_streamlink(upstream, proxy, logger):
2021-05-26 17:15:12 +00:00
result = None
try:
2021-05-28 13:57:19 +00:00
runner = StreamlinkRunner(upstream, proxy, logger)
result_temp = await runner.run()
except Exception as e:
logger.info(e)
else:
result = result_temp
return result
async def get_meta(upstream, proxy, logger):
result = None
try:
runner = MetaRunner(upstream, proxy, logger)
2021-05-26 17:15:12 +00:00
result_temp = await runner.run()
except Exception as e:
logger.info(e)
else:
result = result_temp
2021-05-25 07:39:34 +00:00
return result
2021-05-20 11:46:52 +00:00
async def get_any(upstream, proxy, logger):
tasks = []
tasks.append(asyncio.create_task(get_streamlink(upstream, proxy, logger)))
tasks.append(asyncio.create_task(get_ytdl(upstream, proxy, logger)))
2021-05-28 13:57:19 +00:00
tasks.append(asyncio.create_task(get_meta(upstream, proxy, logger)))
result = StreamData(None, None, None, None, False)
2021-05-23 19:48:10 +00:00
for task in asyncio.as_completed(tasks, timeout=5.0):
2021-05-27 18:39:29 +00:00
temp_result = None
try:
temp_result = await task
except Exception as e:
logger.info(e)
2021-05-28 13:57:19 +00:00
if isinstance(temp_result, StreamData):
result.update("upstream", temp_result.upstream(), temp_result.override)
result.update("thumbnail", temp_result.thumbnail(), temp_result.override)
result.update("title", temp_result.title(), temp_result.override)
result.update("description", temp_result.description(), temp_result.override)
2021-05-20 11:46:52 +00:00
for task in tasks:
if not task.done():
task.cancel()
return result