Added possible fallback for playlist incase filename got changed.

This commit is contained in:
ArabCoders 2024-11-03 17:56:12 +03:00
parent 0cd36ea4be
commit 7a5a06219c
3 changed files with 42 additions and 2 deletions

View file

@ -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<id>[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

View file

@ -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))

View file

@ -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)