File size: 13,071 Bytes
05fb587 575abdb 05fb587 575abdb ad4a15d 05fb587 59df0e9 05fb587 10e71aa 05fb587 575abdb c0b4b77 575abdb 0c42700 05fb587 575abdb c0b4b77 575abdb 05fb587 575abdb 05fb587 575abdb 05fb587 575abdb 05fb587 575abdb 05fb587 575abdb 05fb587 575abdb 05fb587 575abdb 05fb587 575abdb 05fb587 2bae5b1 05fb587 2bae5b1 05fb587 2bae5b1 05fb587 2bae5b1 05fb587 3b118ef 05fb587 ad4a15d 05fb587 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 |
import sys
import time
from fastapi import FastAPI, BackgroundTasks, Request, HTTPException
from fastapi.responses import FileResponse
from fastapi.concurrency import run_in_threadpool
import yt_dlp
import ffmpeg
import urllib.parse
import os
from datetime import datetime, timedelta
import schedule
import requests
import uvicorn
import subprocess
import json
from dotenv import load_dotenv
import mimetypes
import tempfile
from mutagen.mp4 import MP4, MP4Cover, MP4FreeForm
from mutagen.mp3 import MP3
from mutagen.id3 import ID3, USLT, SYLT, Encoding, APIC, TIT2, TPE1, TALB, TPE2, TDRC, TCON, TRCK, COMM
from mutagen.oggvorbis import OggVorbis
from mutagen.oggopus import OggOpus
from mutagen.flac import FLAC, Picture
from PIL import Image
from io import BytesIO
from pathlib import Path
from fastapi.staticfiles import StaticFiles
from collections import defaultdict
import logging
tmp_dir = tempfile.gettempdir()
BASE_URL = "https://chrunos-multi.hf.space"
MP4_TAGS_MAP = {
"album": "\xa9alb",
"album_artist": "aART",
"artist": "\xa9ART",
"composer": "\xa9wrt",
"copyright": "cprt",
"lyrics": "\xa9lyr",
"comment": "desc",
"media_type": "stik",
"producer": "\xa9prd",
"rating": "rtng",
"release_date": "\xa9day",
"title": "\xa9nam",
"url": "\xa9url",
}
def env_to_cookies(env_content: str, output_file: str) -> None:
"""Convert environment variable content back to cookie file"""
try:
# Extract content from env format
if '="' not in env_content:
raise ValueError("Invalid env content format")
content = env_content.split('="', 1)[1].strip('"')
# Replace escaped newlines with actual newlines
cookie_content = content.replace('\\n', '\n')
# Write to cookie file
with open(output_file, 'w') as f:
f.write(cookie_content)
except Exception as e:
raise ValueError(f"Error converting to cookie file: {str(e)}")
def save_to_env_file(env_content: str, env_file: str = '.env') -> None:
"""Save environment variable content to .env file"""
try:
with open(env_file, 'w') as f:
f.write(env_content)
#print(f"Successfully saved to {env_file}")
except Exception as e:
raise ValueError(f"Error saving to env file: {str(e)}")
def env_to_cookies_from_env(output_file: str) -> None:
"""Convert environment variable from .env file to cookie file"""
try:
load_dotenv() # Load from .env file
env_content = os.getenv('FIREFOX_COOKIES')
#print(f"Printing env content: \n{env_content}")
if not env_content:
raise ValueError("FIREFOX_COOKIES not found in .env file")
env_to_cookies(f'FIREFOX_COOKIES="{env_content}"', output_file)
except Exception as e:
raise ValueError(f"Error converting to cookie file: {str(e)}")
def get_cookies():
"""Get cookies from environment variable"""
load_dotenv()
cookie_content = os.getenv('FIREFOX_COOKIES')
#print(cookie_content)
if not cookie_content:
raise ValueError("FIREFOX_COOKIES environment variable not set")
return cookie_content
def create_temp_cookie_file():
"""Create temporary cookie file from environment variable"""
temp_cookie = tempfile.NamedTemporaryFile(mode='w+', delete=False, suffix='.txt')
try:
cookie_content = get_cookies()
# Replace escaped newlines with actual newlines
cookie_content = cookie_content.replace('\\n', '\n')
temp_cookie.write()
temp_cookie.flush()
return Path(temp_cookie.name)
finally:
temp_cookie.close()
load_dotenv()
app = FastAPI()
ydl_opts = {
'format': 'best',
'quiet': True,
#'outtmpl': f'{VIDEO_DIR}/%(id)s.%(ext)s',
'max_filesize': 50 * 1024 * 1024
}
@app.get('/')
def main():
return "API Is Running. If you want to use this API, contact Cody from chrunos.com"
@app.get("/get_video_url")
async def get_video_url(youtube_url: str):
try:
cookiefile = "firefox-cookies.txt"
env_to_cookies_from_env("firefox-cookies.txt")
# Add cookies
ydl_opts["cookiefile"] = "firefox-cookies.txt" #create_temp_cookie_file()
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
info = ydl.extract_info(youtube_url, download=False)
return info
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
# Define a global temporary download directory
global_download_dir = tempfile.mkdtemp()
# Rate limiting dictionary
request_counts = defaultdict(lambda: {"count": 0, "reset_time": datetime.now() + timedelta(days=1)})
MAX_REQUESTS_PER_DAY = 50 # Set your desired limit
def get_user_ip(request: Request) -> str:
"""Helper function to get user's IP address."""
return request.client.host
@app.post("/maxs")
async def download_high_quality_video(request: Request):
user_ip = get_user_ip(request)
user_info = request_counts[user_ip]
# Check if reset time has passed
if datetime.now() > user_info["reset_time"]:
user_info["count"] = 0
user_info["reset_time"] = datetime.now() + timedelta(days=1)
# Check if user has exceeded the request limit
if user_info["count"] >= MAX_REQUESTS_PER_DAY:
error_message = "You have exceeded the maximum number of requests per day. Please try again tomorrow."
return {"error": error_message, "url": "https://t.me/chrunoss"}
data = await request.json()
video_url = data.get('url')
quality = data.get('quality', '1080') # Default to 1080p if not specified
# Check if the requested quality is above 1080p
if int(quality) > 1080:
error_message = "Quality above 1080p is for Premium Members Only. Please check the URL for more information."
help_url = "https://chrunos.com/premium-shortcuts/" # Replace with your actual URL
return {"error": error_message, "url": help_url}
cookiefile = "firefox-cookies.txt"
env_to_cookies_from_env("firefox-cookies.txt")
timestamp = datetime.now().strftime('%Y%m%d%H%M%S')
output_template = str(Path(global_download_dir) / f'%(title)s_{timestamp}.%(ext)s')
# Convert quality string to height
height_map = {
'480': 480,
'720': 720,
'1080': 1080
}
max_height = height_map.get(quality, 1080) # Use the quality variable correctly
# Determine format string based on quality
format_str = f'bestvideo[height<={max_height}][vcodec^=avc]+bestaudio/best'
ydl_opts = {
'format': format_str,
'outtmpl': output_template,
'quiet': True,
'no_warnings': True,
'noprogress': True,
'merge_output_format': 'mp4',
'cookiefile': cookiefile
}
await run_in_threadpool(lambda: yt_dlp.YoutubeDL(ydl_opts).download([video_url]))
downloaded_files = list(Path(global_download_dir).glob(f"*_{timestamp}.mp4"))
if not downloaded_files:
return {"error": "Download failed"}
downloaded_file = downloaded_files[0]
encoded_filename = urllib.parse.quote(downloaded_file.name)
download_url = f"{BASE_URL}/file/{encoded_filename}"
# Increment the user's request count
user_info["count"] += 1
return {"url": download_url}
@app.post("/max")
async def download_high_quality_video(request: Request):
data = await request.json()
video_url = data.get('url')
quality = data.get('quality', '1080') # Default to 1080p if not specified
# Check if the requested quality is above 1080p
if int(quality) > 1080:
error_message = "Quality above 1080p is for premium users. Please check the URL for more information."
help_url = "https://chrunos.com/premium-shortcuts/" # Replace with your actual URL
return {"error": error_message, "url": help_url}
cookiefile = "firefox-cookies.txt"
env_to_cookies_from_env("firefox-cookies.txt")
timestamp = datetime.now().strftime('%Y%m%d%H%M%S')
output_template = str(Path(global_download_dir) / f'%(title)s_{timestamp}.%(ext)s')
# Convert quality string to height
height_map = {
'480': 480,
'720': 720,
'1080': 1080
}
max_height = height_map.get(quality, 1080) # Use the quality variable correctly
# Determine format string based on quality
format_str = f'bestvideo[height<={max_height}][vcodec^=avc]+bestaudio/best'
ydl_opts = {
'format': format_str,
'outtmpl': output_template,
'quiet': True,
'no_warnings': True,
'noprogress': True,
'merge_output_format': 'mp4',
'cookiefile': cookiefile
}
await run_in_threadpool(lambda: yt_dlp.YoutubeDL(ydl_opts).download([video_url]))
downloaded_files = list(Path(global_download_dir).glob(f"*_{timestamp}.mp4"))
if not downloaded_files:
return {"error": "Download failed"}
downloaded_file = downloaded_files[0]
encoded_filename = urllib.parse.quote(downloaded_file.name)
download_url = f"{BASE_URL}/file/{encoded_filename}"
return {"url": download_url}
@app.post("/audio")
async def download_audio(request: Request):
data = await request.json()
video_url = data.get('url')
cookiefile = "firefox-cookies.txt"
env_to_cookies_from_env("firefox-cookies.txt")
timestamp = datetime.now().strftime('%Y%m%d%H%M%S')
output_template = str(Path(global_download_dir) / f'%(title)s_{timestamp}.%(ext)s')
ydl_opts = {
'format': 'bestaudio/best',
'outtmpl': output_template,
'quiet': True,
'no_warnings': True,
'noprogress': True,
'cookiefile': cookiefile,
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192'
}]
}
await run_in_threadpool(lambda: yt_dlp.YoutubeDL(ydl_opts).download([video_url]))
downloaded_files = list(Path(global_download_dir).glob(f"*_{timestamp}.*"))
if not downloaded_files:
return {"error": "Download failed"}
downloaded_file = downloaded_files[0]
encoded_filename = urllib.parse.quote(downloaded_file.name)
download_url = f"{BASE_URL}/file/{encoded_filename}"
return {"url": download_url}
# Configure logging
logging.basicConfig(level=logging.INFO)
@app.post("/search")
async def search_and_download_song(request: Request):
data = await request.json()
song_name = data.get('songname')
artist_name = data.get('artist')
if artist_name:
search_query = f"ytsearch:{song_name} {artist_name}"
else:
search_query = f"ytsearch:{song_name}"
logging.info(f"Search query: {search_query}")
cookiefile = "firefox-cookies.txt"
env_to_cookies_from_env("firefox-cookies.txt")
timestamp = datetime.now().strftime('%Y%m%d%H%M%S')
output_template = str(Path(global_download_dir) / f'%(title)s_{timestamp}.%(ext)s')
ydl_opts = {
'format': 'bestaudio/best',
'outtmpl': output_template,
'quiet': True,
'no_warnings': True,
'noprogress': True,
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192'
}],
'cookiefile': cookiefile
}
try:
logging.info("Starting yt-dlp search and download...")
await run_in_threadpool(lambda: yt_dlp.YoutubeDL(ydl_opts).download([search_query]))
logging.info("yt-dlp search and download completed")
except yt_dlp.utils.DownloadError as e:
error_message = str(e)
logging.error(f"yt-dlp error: {error_message}")
return JSONResponse(content={"error": error_message}, status_code=500)
except Exception as e:
error_message = str(e)
logging.error(f"General error: {error_message}")
return JSONResponse(content={"error": error_message}, status_code=500)
downloaded_files = list(Path(global_download_dir).glob(f"*_{timestamp}.mp3"))
if not downloaded_files:
logging.error("Download failed: No MP3 files found")
return JSONResponse(content={"error": "Download failed"}, status_code=500)
downloaded_file = downloaded_files[0]
encoded_filename = urllib.parse.quote(downloaded_file.name)
download_url = f"{BASE_URL}/file/{encoded_filename}"
logging.info(f"Download URL: {download_url}")
# Log just before returning the response
logging.info("Preparing to send response back to the client")
return JSONResponse(content={"url": download_url}, status_code=200)
# Mount the static files directory
app.mount("/file", StaticFiles(directory=global_download_dir), name="downloads")
@app.middleware("http")
async def set_mime_type_middleware(request: Request, call_next):
response = await call_next(request)
if request.url.path.endswith(".mp4"):
response.headers["Content-Type"] = "video/mp4"
return response
|