Update app.py
Browse files
app.py
CHANGED
|
@@ -7,7 +7,7 @@ import yt_dlp
|
|
| 7 |
import ffmpeg
|
| 8 |
import urllib.parse
|
| 9 |
import os
|
| 10 |
-
from datetime import datetime
|
| 11 |
import schedule
|
| 12 |
import requests
|
| 13 |
import uvicorn
|
|
@@ -26,6 +26,7 @@ from PIL import Image
|
|
| 26 |
from io import BytesIO
|
| 27 |
from pathlib import Path
|
| 28 |
from fastapi.staticfiles import StaticFiles
|
|
|
|
| 29 |
|
| 30 |
tmp_dir = tempfile.gettempdir()
|
| 31 |
BASE_URL = "https://chrunos-multi.hf.space"
|
|
@@ -143,43 +144,159 @@ async def get_video_url(youtube_url: str):
|
|
| 143 |
# Define a global temporary download directory
|
| 144 |
global_download_dir = tempfile.mkdtemp()
|
| 145 |
|
| 146 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
async def download_high_quality_video(request: Request):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
data = await request.json()
|
| 149 |
video_url = data.get('url')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
cookiefile = "firefox-cookies.txt"
|
| 151 |
env_to_cookies_from_env("firefox-cookies.txt")
|
| 152 |
-
|
| 153 |
timestamp = datetime.now().strftime('%Y%m%d%H%M%S')
|
| 154 |
output_template = str(Path(global_download_dir) / f'%(title)s_{timestamp}.%(ext)s')
|
| 155 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 156 |
|
| 157 |
ydl_opts = {
|
| 158 |
-
'format':
|
| 159 |
'outtmpl': output_template,
|
| 160 |
'quiet': True,
|
| 161 |
'no_warnings': True,
|
| 162 |
'noprogress': True,
|
| 163 |
-
'merge_output_format': 'mp4'
|
|
|
|
| 164 |
}
|
| 165 |
-
ydl_opts["cookiefile"] = "firefox-cookies.txt" #create_temp_cookie_file()
|
| 166 |
|
| 167 |
await run_in_threadpool(lambda: yt_dlp.YoutubeDL(ydl_opts).download([video_url]))
|
| 168 |
-
|
| 169 |
-
# Find the downloaded file (with the title as filename)
|
| 170 |
downloaded_files = list(Path(global_download_dir).glob(f"*_{timestamp}.mp4"))
|
| 171 |
-
|
| 172 |
if not downloaded_files:
|
| 173 |
return {"error": "Download failed"}
|
| 174 |
-
|
| 175 |
-
# Assume there is only one matching file
|
| 176 |
downloaded_file = downloaded_files[0]
|
| 177 |
encoded_filename = urllib.parse.quote(downloaded_file.name)
|
| 178 |
download_url = f"{BASE_URL}/file/{encoded_filename}"
|
| 179 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 180 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 181 |
return {"url": download_url}
|
| 182 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 183 |
|
| 184 |
|
| 185 |
|
|
|
|
| 7 |
import ffmpeg
|
| 8 |
import urllib.parse
|
| 9 |
import os
|
| 10 |
+
from datetime import datetime, timedelta
|
| 11 |
import schedule
|
| 12 |
import requests
|
| 13 |
import uvicorn
|
|
|
|
| 26 |
from io import BytesIO
|
| 27 |
from pathlib import Path
|
| 28 |
from fastapi.staticfiles import StaticFiles
|
| 29 |
+
from collections import defaultdict
|
| 30 |
|
| 31 |
tmp_dir = tempfile.gettempdir()
|
| 32 |
BASE_URL = "https://chrunos-multi.hf.space"
|
|
|
|
| 144 |
# Define a global temporary download directory
|
| 145 |
global_download_dir = tempfile.mkdtemp()
|
| 146 |
|
| 147 |
+
# Rate limiting dictionary
|
| 148 |
+
request_counts = defaultdict(lambda: {"count": 0, "reset_time": datetime.now() + timedelta(days=1)})
|
| 149 |
+
MAX_REQUESTS_PER_DAY = 10 # Set your desired limit
|
| 150 |
+
|
| 151 |
+
def get_user_ip(request: Request) -> str:
|
| 152 |
+
"""Helper function to get user's IP address."""
|
| 153 |
+
return request.client.host
|
| 154 |
+
|
| 155 |
+
@app.post("/max")
|
| 156 |
async def download_high_quality_video(request: Request):
|
| 157 |
+
user_ip = get_user_ip(request)
|
| 158 |
+
user_info = request_counts[user_ip]
|
| 159 |
+
|
| 160 |
+
# Check if reset time has passed
|
| 161 |
+
if datetime.now() > user_info["reset_time"]:
|
| 162 |
+
user_info["count"] = 0
|
| 163 |
+
user_info["reset_time"] = datetime.now() + timedelta(days=1)
|
| 164 |
+
|
| 165 |
+
# Check if user has exceeded the request limit
|
| 166 |
+
if user_info["count"] >= MAX_REQUESTS_PER_DAY:
|
| 167 |
+
error_message = "You have exceeded the maximum number of requests per day. Please try again tomorrow."
|
| 168 |
+
return {"error": error_message}
|
| 169 |
+
|
| 170 |
data = await request.json()
|
| 171 |
video_url = data.get('url')
|
| 172 |
+
quality = data.get('quality', '1080') # Default to 1080p if not specified
|
| 173 |
+
|
| 174 |
+
# Check if the requested quality is above 1080p
|
| 175 |
+
if int(quality) > 1080:
|
| 176 |
+
error_message = "Quality above 1080p is for Premium Members Only. Please check the URL for more information."
|
| 177 |
+
help_url = "https://chrunos.com/premium-shortcuts/" # Replace with your actual URL
|
| 178 |
+
return {"error": error_message, "url": help_url}
|
| 179 |
+
|
| 180 |
cookiefile = "firefox-cookies.txt"
|
| 181 |
env_to_cookies_from_env("firefox-cookies.txt")
|
| 182 |
+
|
| 183 |
timestamp = datetime.now().strftime('%Y%m%d%H%M%S')
|
| 184 |
output_template = str(Path(global_download_dir) / f'%(title)s_{timestamp}.%(ext)s')
|
| 185 |
|
| 186 |
+
# Convert quality string to height
|
| 187 |
+
height_map = {
|
| 188 |
+
'480': 480,
|
| 189 |
+
'720': 720,
|
| 190 |
+
'1080': 1080
|
| 191 |
+
}
|
| 192 |
+
max_height = height_map.get(quality, 1080) # Use the quality variable correctly
|
| 193 |
+
|
| 194 |
+
# Determine format string based on quality
|
| 195 |
+
format_str = f'bestvideo[height<={max_height}][vcodec^=avc]+bestaudio/best'
|
| 196 |
|
| 197 |
ydl_opts = {
|
| 198 |
+
'format': format_str,
|
| 199 |
'outtmpl': output_template,
|
| 200 |
'quiet': True,
|
| 201 |
'no_warnings': True,
|
| 202 |
'noprogress': True,
|
| 203 |
+
'merge_output_format': 'mp4',
|
| 204 |
+
'cookiefile': cookiefile
|
| 205 |
}
|
|
|
|
| 206 |
|
| 207 |
await run_in_threadpool(lambda: yt_dlp.YoutubeDL(ydl_opts).download([video_url]))
|
| 208 |
+
|
|
|
|
| 209 |
downloaded_files = list(Path(global_download_dir).glob(f"*_{timestamp}.mp4"))
|
|
|
|
| 210 |
if not downloaded_files:
|
| 211 |
return {"error": "Download failed"}
|
| 212 |
+
|
|
|
|
| 213 |
downloaded_file = downloaded_files[0]
|
| 214 |
encoded_filename = urllib.parse.quote(downloaded_file.name)
|
| 215 |
download_url = f"{BASE_URL}/file/{encoded_filename}"
|
| 216 |
|
| 217 |
+
# Increment the user's request count
|
| 218 |
+
user_info["count"] += 1
|
| 219 |
+
|
| 220 |
+
return {"url": download_url}
|
| 221 |
+
|
| 222 |
+
|
| 223 |
+
|
| 224 |
+
@app.post("/maxs")
|
| 225 |
+
async def download_high_quality_video(request: Request):
|
| 226 |
+
user_ip = get_user_ip(request)
|
| 227 |
+
user_info = request_counts[user_ip]
|
| 228 |
|
| 229 |
+
# Check if reset time has passed
|
| 230 |
+
if datetime.now() > user_info["reset_time"]:
|
| 231 |
+
user_info["count"] = 0
|
| 232 |
+
user_info["reset_time"] = datetime.now() + timedelta(days=1)
|
| 233 |
+
|
| 234 |
+
# Check if user has exceeded the request limit
|
| 235 |
+
if user_info["count"] >= MAX_REQUESTS_PER_DAY:
|
| 236 |
+
error_message = "You have exceeded the maximum number of requests per day. Please try again tomorrow."
|
| 237 |
+
return {"error": error_message}
|
| 238 |
+
|
| 239 |
+
data = await request.json()
|
| 240 |
+
video_url = data.get('url')
|
| 241 |
+
quality = data.get('quality', '1080') # Default to 1080p if not specified
|
| 242 |
+
|
| 243 |
+
# Check if the requested quality is above 1080p
|
| 244 |
+
if int(quality) > 1080:
|
| 245 |
+
error_message = "Quality above 1080p is not supported. Please check the URL for more information."
|
| 246 |
+
help_url = "https://example.com/more-info" # Replace with your actual URL
|
| 247 |
+
return {"error": error_message, "url": help_url}
|
| 248 |
+
|
| 249 |
+
cookiefile = "firefox-cookies.txt"
|
| 250 |
+
env_to_cookies_from_env("firefox-cookies.txt")
|
| 251 |
+
|
| 252 |
+
timestamp = datetime.now().strftime('%Y%m%d%H%M%S')
|
| 253 |
+
output_template = str(Path(global_download_dir) / f'%(title)s_{timestamp}.%(ext)s')
|
| 254 |
+
|
| 255 |
+
# Convert quality string to height
|
| 256 |
+
height_map = {
|
| 257 |
+
'480': 480,
|
| 258 |
+
'720': 720,
|
| 259 |
+
'1080': 1080
|
| 260 |
+
}
|
| 261 |
+
max_height = height_map.get(quality, 1080) # Use the quality variable correctly
|
| 262 |
+
|
| 263 |
+
# Determine format string based on quality
|
| 264 |
+
format_str = f'bestvideo[height<={max_height}][vcodec^=avc]+bestaudio/best'
|
| 265 |
+
|
| 266 |
+
ydl_opts = {
|
| 267 |
+
'format': format_str,
|
| 268 |
+
'outtmpl': output_template,
|
| 269 |
+
'quiet': True,
|
| 270 |
+
'no_warnings': True,
|
| 271 |
+
'noprogress': True,
|
| 272 |
+
'merge_output_format': 'mp4',
|
| 273 |
+
'cookiefile': cookiefile
|
| 274 |
+
}
|
| 275 |
+
|
| 276 |
+
await run_in_threadpool(lambda: yt_dlp.YoutubeDL(ydl_opts).download([video_url]))
|
| 277 |
+
|
| 278 |
+
downloaded_files = list(Path(global_download_dir).glob(f"*_{timestamp}.mp4"))
|
| 279 |
+
if not downloaded_files:
|
| 280 |
+
return {"error": "Download failed"}
|
| 281 |
+
|
| 282 |
+
downloaded_file = downloaded_files[0]
|
| 283 |
+
encoded_filename = urllib.parse.quote(downloaded_file.name)
|
| 284 |
+
download_url = f"{BASE_URL}/file/{encoded_filename}"
|
| 285 |
+
|
| 286 |
+
# Increment the user's request count
|
| 287 |
+
user_info["count"] += 1
|
| 288 |
+
|
| 289 |
return {"url": download_url}
|
| 290 |
|
| 291 |
+
# Mount the static files directory
|
| 292 |
+
app.mount("/file", StaticFiles(directory=global_download_dir), name="downloads")
|
| 293 |
+
|
| 294 |
+
@app.middleware("http")
|
| 295 |
+
async def set_mime_type_middleware(request: Request, call_next):
|
| 296 |
+
response = await call_next(request)
|
| 297 |
+
if request.url.path.endswith(".mp4"):
|
| 298 |
+
response.headers["Content-Type"] = "video/mp4"
|
| 299 |
+
return response
|
| 300 |
|
| 301 |
|
| 302 |
|