From 7a5a06219cb4f80917c3b30e7dbbd690823044df Mon Sep 17 00:00:00 2001 From: ArabCoders Date: Sun, 3 Nov 2024 17:56:12 +0300 Subject: [PATCH] Added possible fallback for playlist incase filename got changed. --- app/Utils.py | 31 +++++++++++++++++++++++++++++++ app/main.py | 2 ++ app/player/Playlist.py | 11 +++++++++-- 3 files changed, 42 insertions(+), 2 deletions(-) diff --git a/app/Utils.py b/app/Utils.py index 1452a333..4448896e 100644 --- a/app/Utils.py +++ b/app/Utils.py @@ -4,6 +4,8 @@ from datetime import datetime, timezone import json import logging import os +import pathlib +import re from typing import Any import yt_dlp from socketio import AsyncServer @@ -388,3 +390,32 @@ def load_file(file: str, check_type=None) -> tuple[dict | list, bool, str]: return ({}, False, f"Failed to assert that the contents '{type(opts)}' are of type '{check_type}'.",) except Exception as e: return ({}, False, f'{e}',) + + +def checkId(basePath: str, file: pathlib.Path) -> bool | str: + """ + Check if we are able to get an id from the file name. + if so check if any video file with the same id exists. + + :param basePath: Base path to strip. + :param file: File to check. + + :return: False if no id found, otherwise the id. + """ + + match = re.search(r'(?<=\[)(?:youtube-)?(?P[a-zA-Z0-9\-_]{11})(?=\])', file.stem, re.IGNORECASE) + if not match: + return False + + id = match.groupdict().get('id') + + for f in file.parent.iterdir(): + if id not in f.stem: + continue + + if f.suffix not in ('.mp4', '.mkv', '.webm', '.m4v', '.m4a', '.mp3', '.aac', '.ogg',): + continue + + return f.absolute() + + return False diff --git a/app/main.py b/app/main.py index dd0c6966..c9869448 100644 --- a/app/main.py +++ b/app/main.py @@ -465,6 +465,8 @@ class Main: download_path=self.config.download_path, file=file ) + if isinstance(text, Response): + return text except Exception as e: return web.HTTPNotFound(reason=str(e)) diff --git a/app/player/Playlist.py b/app/player/Playlist.py index 254fa292..48c96216 100644 --- a/app/player/Playlist.py +++ b/app/player/Playlist.py @@ -6,6 +6,7 @@ from Utils import calcDownloadPath import pathlib from .ffprobe import FFProbe from .Subtitle import Subtitle +from aiohttp.web import Response class Playlist: @@ -14,11 +15,17 @@ class Playlist: def __init__(self, url: str): self.url = url - async def make(self, download_path: str, file: str): + async def make(self, download_path: str, file: str) -> str | Response: rFile = pathlib.Path(calcDownloadPath(basePath=download_path, folder=file, createPath=False)) if not rFile.exists(): - raise Exception(f"File '{rFile}' does not exist.") + possibleFile = self.checkId(download_path, rFile) + if not possibleFile: + raise Exception(f"File '{rFile}' does not exist.") + + return Response(status=302, headers={ + 'Location': f"{self.url}player/playlist/{quote(possibleFile.replace(download_path, '').strip('/'))}.m3u8" + }) try: ffprobe = FFProbe(rFile)