diff --git a/app/download.py b/app/download.py index 6ddbdd2..010fe8e 100644 --- a/app/download.py +++ b/app/download.py @@ -1,3 +1,4 @@ +from fastapi.responses import JSONResponse import os, yt_dlp, json, requests from bs4 import BeautifulSoup @@ -10,6 +11,23 @@ class grab(): option_values = [option['value'] for option in options if option.has_attr('value')] seasons = [item.replace(url+'/season:', '') for item in option_values] return seasons + + def thumbnail(ydl,url,location): + # Extracting video information + video_info = ydl.extract_info(url, download=False) + thumbnail_url = video_info.get('thumbnail') + + # Download the thumbnail image + if thumbnail_url: + try: + thumbnail_filename = os.path.join(location, f"{video_info['id']}.jpg") + with open(thumbnail_filename, 'wb') as thumbnail_file: + thumbnail_file.write(requests.get(thumbnail_url).content) + print("Downloaded MP4 and downloaded thumbnail successfully!") + except Exception as e: + print(f"Error downloading thumbnail: {str(e)}") + else: + print("Downloaded MP4 but no thumbnail found.") class dropout(): def show(show,season,episode): @@ -38,7 +56,7 @@ class dropout(): 'temp': '/temp', 'home': directory }, - 'cookiefile': 'cookies.txt', + 'cookiefile': '/data/dropout_cookies.txt', 'reject_title': [ r'(?i).*behind.?the.?scenes.*', # Reject titles with "behind the scenes" (case-insensitive) r'(?i).*trailer.*', # Reject titles with "trailer" (case-insensitive) @@ -51,8 +69,12 @@ class dropout(): 'writesubtitles': True, # Download subtitles 'subtitleslangs': ['en'] # Specify the language for subtitles (e.g., 'en' for English) } - with yt_dlp.YoutubeDL(dl_ops) as ydl: - ydl.download([url]) + try: + with yt_dlp.YoutubeDL(dl_ops) as ydl: + ydl.download([url]) + return JSONResponse(status_code=200, content={"status": "success", "message": "Download completed."}) + except Exception as e: + return JSONResponse(status_code=500, content={"status": "error", "message": str(e)}) def series(): json_data=[] @@ -78,53 +100,41 @@ class dropout(): with open('/data/dropout.json', 'w') as json_file: json.dump(sorted_json_data, json_file, indent=4) -def ydl(url, location): - dl_ops = {'paths': {'temp': '/temp', 'home': location}, 'outtmpl': '%(uploader)s/%(title)s.%(ext)s'} - if location == "/podcasts": - dl_ops['format'] = 'bestaudio/best[ext=mp3]' - dl_ops['postprocessors'] = [{ - 'key': 'FFmpegExtractAudio', - 'preferredcodec': 'mp3', - 'preferredquality': '192', - }, { - 'key': 'FFmpegMetadata', - 'add_metadata': True, - }] - elif location == "/asmr": - dl_ops['format'] = 'bestaudio/best[ext=mp3]' - dl_ops['postprocessors'] = [{ - 'key': 'FFmpegExtractAudio', - 'preferredcodec': 'mp3', - 'preferredquality': '192', - }, { - 'key': 'FFmpegMetadata', - 'add_metadata': True, - }] - elif location == "/mnt/nsfw": - dl_ops['format'] = 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best' +class youtube(): + def ydl(url, location): + dl_ops = {'paths': {'temp': '/temp', 'home': location}, 'outtmpl': '%(uploader)s/%(title)s.%(ext)s'} + if location == "/podcasts": + dl_ops['format'] = 'bestaudio/best[ext=mp3]' + dl_ops['postprocessors'] = [{ + 'key': 'FFmpegExtractAudio', + 'preferredcodec': 'mp3', + 'preferredquality': '192', + }, { + 'key': 'FFmpegMetadata', + 'add_metadata': True, + }] + elif location == "/asmr": + dl_ops['format'] = 'bestaudio/best[ext=mp3]' + dl_ops['postprocessors'] = [{ + 'key': 'FFmpegExtractAudio', + 'preferredcodec': 'mp3', + 'preferredquality': '192', + }, { + 'key': 'FFmpegMetadata', + 'add_metadata': True, + }] + elif location == "/nsfw": + dl_ops['format'] = 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best' - else: - dl_ops = { - 'format': 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best', - 'cookiefile': 'yt-cookies.txt' - } - - - with yt_dlp.YoutubeDL(dl_ops) as ydl: - ydl.download(url) - - # Extracting video information - video_info = ydl.extract_info(url, download=False) - thumbnail_url = video_info.get('thumbnail') - - # Download the thumbnail image - if thumbnail_url: - try: - thumbnail_filename = os.path.join(location, f"{video_info['id']}.jpg") - with open(thumbnail_filename, 'wb') as thumbnail_file: - thumbnail_file.write(requests.get(thumbnail_url).content) - print("Downloaded MP4 and downloaded thumbnail successfully!") - except Exception as e: - print(f"Error downloading thumbnail: {str(e)}") else: - print("Downloaded MP4 but no thumbnail found.") \ No newline at end of file + dl_ops = { + 'format': 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best', + } + + try: + with yt_dlp.YoutubeDL(dl_ops) as ydl: + ydl.download([url]) + # grab.thumbnail(ydl,url,location) + return JSONResponse(status_code=200, content={"status": "success", "message": "Download completed."}) + except Exception as e: + return JSONResponse(status_code=500, content={"status": "error", "message": str(e)}) \ No newline at end of file diff --git a/app/main.py b/app/main.py index dfd375f..63d049a 100644 --- a/app/main.py +++ b/app/main.py @@ -9,10 +9,10 @@ import json app = FastAPI() # # Mount static files if needed (e.g. CSS, JS, images) -app.mount("/static", StaticFiles(directory="app/static"), name="static") +app.mount("/static", StaticFiles(directory="/static"), name="static") # # Jinja2 template directory -templates = Jinja2Templates(directory="app/templates") +templates = Jinja2Templates(directory="/templates") # api @@ -33,6 +33,10 @@ async def dropoutSeries(): async def dropoutDownload(show: str, season: str, episode: str): download.dropout.show(show,season,episode) +@app.get("/ydl") +async def ydl(url: str, location: str): + download.youtube.ydl(url,location) + # html @app.get("/", response_class=HTMLResponse) async def read_root(request: Request):