move timeout to StreamProvider

This commit is contained in:
Roy Olav Purser 2021-05-28 18:24:50 +02:00
parent 1cff970ecf
commit 278de926e3
Signed by: roypur
GPG Key ID: E14D26A036F21656

View File

@ -76,6 +76,14 @@ class StreamProvider():
proxy = str(proxy) proxy = str(proxy)
if len(proxy) > 5: if len(proxy) > 5:
self.proxy = "socks5://" + proxy self.proxy = "socks5://" + proxy
async def run(self):
data = None
try:
future = asyncio.to_thread(self.stream)
data = await asyncio.wait_for(future, timeout=5)
except Exception as e:
self.logger.info(e)
return data
class StreamlinkRunner(StreamProvider): class StreamlinkRunner(StreamProvider):
def stream(self): def stream(self):
@ -93,8 +101,6 @@ class StreamlinkRunner(StreamProvider):
except Exception as e: except Exception as e:
self.logger.info(e) self.logger.info(e)
return StreamData(None, None, None, None, False) return StreamData(None, None, None, None, False)
async def run(self):
return await asyncio.to_thread(self.stream)
class YoutubeRunner(StreamProvider): class YoutubeRunner(StreamProvider):
def stream(self): def stream(self):
@ -139,8 +145,6 @@ class YoutubeRunner(StreamProvider):
except Exception as e: except Exception as e:
self.logger.info(e) self.logger.info(e)
return StreamData(best_url, thumbnail, title, description, True) return StreamData(best_url, thumbnail, title, description, True)
async def run(self):
return await asyncio.to_thread(self.stream)
class MetaRunner(StreamProvider): class MetaRunner(StreamProvider):
def stream(self): def stream(self):
@ -153,8 +157,6 @@ class MetaRunner(StreamProvider):
except Exception as e: except Exception as e:
self.logger.info(e) self.logger.info(e)
return StreamData(None, data.get("og:image"), data.get("og:title"), data.get("og:description"), False) return StreamData(None, data.get("og:image"), data.get("og:title"), data.get("og:description"), False)
async def run(self):
return await asyncio.to_thread(self.stream)
async def get_ytdl(upstream, proxy, logger): async def get_ytdl(upstream, proxy, logger):
result = None result = None
@ -195,12 +197,8 @@ async def get_any(upstream, proxy, logger):
tasks.append(asyncio.create_task(get_ytdl(upstream, proxy, logger))) tasks.append(asyncio.create_task(get_ytdl(upstream, proxy, logger)))
tasks.append(asyncio.create_task(get_meta(upstream, proxy, logger))) tasks.append(asyncio.create_task(get_meta(upstream, proxy, logger)))
result = StreamData(None, None, None, None, False) result = StreamData(None, None, None, None, False)
for task in asyncio.as_completed(tasks, timeout=5.0): for task in asyncio.as_completed(tasks):
temp_result = None temp_result = await task
try:
temp_result = await task
except Exception as e:
logger.info(e)
if isinstance(temp_result, StreamData): if isinstance(temp_result, StreamData):
result.update("upstream", temp_result.upstream(), temp_result.override) result.update("upstream", temp_result.upstream(), temp_result.override)
result.update("thumbnail", temp_result.thumbnail(), temp_result.override) result.update("thumbnail", temp_result.thumbnail(), temp_result.override)