build on raspberrypi

This commit is contained in:
TylerCG 2025-04-20 10:46:14 -04:00
parent 6061aff2ef
commit 7f2ac18d04
2 changed files with 67 additions and 53 deletions

View File

@ -1,3 +1,4 @@
from fastapi.responses import JSONResponse
import os, yt_dlp, json, requests import os, yt_dlp, json, requests
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
@ -10,6 +11,23 @@ class grab():
option_values = [option['value'] for option in options if option.has_attr('value')] option_values = [option['value'] for option in options if option.has_attr('value')]
seasons = [item.replace(url+'/season:', '') for item in option_values] seasons = [item.replace(url+'/season:', '') for item in option_values]
return seasons 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(): class dropout():
def show(show,season,episode): def show(show,season,episode):
@ -38,7 +56,7 @@ class dropout():
'temp': '/temp', 'temp': '/temp',
'home': directory 'home': directory
}, },
'cookiefile': 'cookies.txt', 'cookiefile': '/data/dropout_cookies.txt',
'reject_title': [ 'reject_title': [
r'(?i).*behind.?the.?scenes.*', # Reject titles with "behind the scenes" (case-insensitive) r'(?i).*behind.?the.?scenes.*', # Reject titles with "behind the scenes" (case-insensitive)
r'(?i).*trailer.*', # Reject titles with "trailer" (case-insensitive) r'(?i).*trailer.*', # Reject titles with "trailer" (case-insensitive)
@ -51,8 +69,12 @@ class dropout():
'writesubtitles': True, # Download subtitles 'writesubtitles': True, # Download subtitles
'subtitleslangs': ['en'] # Specify the language for subtitles (e.g., 'en' for English) 'subtitleslangs': ['en'] # Specify the language for subtitles (e.g., 'en' for English)
} }
with yt_dlp.YoutubeDL(dl_ops) as ydl: try:
ydl.download([url]) 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(): def series():
json_data=[] json_data=[]
@ -78,53 +100,41 @@ class dropout():
with open('/data/dropout.json', 'w') as json_file: with open('/data/dropout.json', 'w') as json_file:
json.dump(sorted_json_data, json_file, indent=4) json.dump(sorted_json_data, json_file, indent=4)
def ydl(url, location): class youtube():
dl_ops = {'paths': {'temp': '/temp', 'home': location}, 'outtmpl': '%(uploader)s/%(title)s.%(ext)s'} def ydl(url, location):
if location == "/podcasts": dl_ops = {'paths': {'temp': '/temp', 'home': location}, 'outtmpl': '%(uploader)s/%(title)s.%(ext)s'}
dl_ops['format'] = 'bestaudio/best[ext=mp3]' if location == "/podcasts":
dl_ops['postprocessors'] = [{ dl_ops['format'] = 'bestaudio/best[ext=mp3]'
'key': 'FFmpegExtractAudio', dl_ops['postprocessors'] = [{
'preferredcodec': 'mp3', 'key': 'FFmpegExtractAudio',
'preferredquality': '192', 'preferredcodec': 'mp3',
}, { 'preferredquality': '192',
'key': 'FFmpegMetadata', }, {
'add_metadata': True, 'key': 'FFmpegMetadata',
}] 'add_metadata': True,
elif location == "/asmr": }]
dl_ops['format'] = 'bestaudio/best[ext=mp3]' elif location == "/asmr":
dl_ops['postprocessors'] = [{ dl_ops['format'] = 'bestaudio/best[ext=mp3]'
'key': 'FFmpegExtractAudio', dl_ops['postprocessors'] = [{
'preferredcodec': 'mp3', 'key': 'FFmpegExtractAudio',
'preferredquality': '192', 'preferredcodec': 'mp3',
}, { 'preferredquality': '192',
'key': 'FFmpegMetadata', }, {
'add_metadata': True, 'key': 'FFmpegMetadata',
}] 'add_metadata': True,
elif location == "/mnt/nsfw": }]
dl_ops['format'] = 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best' 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: else:
print("Downloaded MP4 but no thumbnail found.") 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)})

View File

@ -9,10 +9,10 @@ import json
app = FastAPI() app = FastAPI()
# # Mount static files if needed (e.g. CSS, JS, images) # # 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 # # Jinja2 template directory
templates = Jinja2Templates(directory="app/templates") templates = Jinja2Templates(directory="/templates")
# api # api
@ -33,6 +33,10 @@ async def dropoutSeries():
async def dropoutDownload(show: str, season: str, episode: str): async def dropoutDownload(show: str, season: str, episode: str):
download.dropout.show(show,season,episode) download.dropout.show(show,season,episode)
@app.get("/ydl")
async def ydl(url: str, location: str):
download.youtube.ydl(url,location)
# html # html
@app.get("/", response_class=HTMLResponse) @app.get("/", response_class=HTMLResponse)
async def read_root(request: Request): async def read_root(request: Request):