Update app.py
Browse files
app.py
CHANGED
|
@@ -220,3 +220,70 @@ async def download_hls_video(request: Request):
|
|
| 220 |
download_url = f"{base_url}file/{encoded_filename}"
|
| 221 |
gc.collect()
|
| 222 |
return {"url": download_url}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 220 |
download_url = f"{base_url}file/{encoded_filename}"
|
| 221 |
gc.collect()
|
| 222 |
return {"url": download_url}
|
| 223 |
+
|
| 224 |
+
|
| 225 |
+
@app.post("/maxs")
|
| 226 |
+
async def download_high_quality_video(request: Request):
|
| 227 |
+
user_ip = get_user_ip(request)
|
| 228 |
+
|
| 229 |
+
if rate_limiter.is_rate_limited(user_ip):
|
| 230 |
+
current_count = rate_limiter.get_current_count(user_ip)
|
| 231 |
+
raise HTTPException(
|
| 232 |
+
status_code=429,
|
| 233 |
+
detail={
|
| 234 |
+
"error": "You have exceeded the maximum number of requests per day. Please try again tomorrow.",
|
| 235 |
+
"url": "https://t.me/chrunoss"
|
| 236 |
+
}
|
| 237 |
+
)
|
| 238 |
+
|
| 239 |
+
data = await request.json()
|
| 240 |
+
|
| 241 |
+
video_url = data.get('url')
|
| 242 |
+
if "t.me" in video_url or "chrunos.com" in video_url:
|
| 243 |
+
return {"error": f"{video_url} is not supported", "url": "https://t.me/chrunoss"}
|
| 244 |
+
|
| 245 |
+
quality = data.get('quality', '720') # Default to 720 if not specified
|
| 246 |
+
logger.info(f'input: {video_url}, {quality}')
|
| 247 |
+
|
| 248 |
+
# Check if the requested quality is above 1080p
|
| 249 |
+
if int(quality) > 720:
|
| 250 |
+
error_message = "Quality above 720p is for Premium Members Only. Please check the URL for more information."
|
| 251 |
+
help_url = "https://chrunos.com/premium-shortcuts/" # Replace with your actual URL
|
| 252 |
+
return {"error": error_message, "url": help_url}
|
| 253 |
+
|
| 254 |
+
timestamp = datetime.now().strftime('%Y%m%d%H%M%S')
|
| 255 |
+
output_template = str(Path(global_download_dir) / f'%(title).70s_{timestamp}.%(ext)s')
|
| 256 |
+
|
| 257 |
+
# Convert quality string to height
|
| 258 |
+
height_map = {
|
| 259 |
+
'480': 480,
|
| 260 |
+
'720': 720,
|
| 261 |
+
'1080': 1080
|
| 262 |
+
}
|
| 263 |
+
max_height = height_map.get(quality, 1080) # Use the quality variable correctly
|
| 264 |
+
|
| 265 |
+
# Determine format string based on quality
|
| 266 |
+
format_str = f'bestvideo[height<={max_height}][vcodec^=avc]+bestaudio/best'
|
| 267 |
+
|
| 268 |
+
ydl_opts = {
|
| 269 |
+
'format': format_str,
|
| 270 |
+
'outtmpl': output_template,
|
| 271 |
+
'quiet': True,
|
| 272 |
+
'no_warnings': True,
|
| 273 |
+
'noprogress': True,
|
| 274 |
+
'merge_output_format': 'mp4'
|
| 275 |
+
}
|
| 276 |
+
|
| 277 |
+
await run_in_threadpool(lambda: yt_dlp.YoutubeDL(ydl_opts).download([video_url]))
|
| 278 |
+
|
| 279 |
+
downloaded_files = list(Path(global_download_dir).glob(f"*_{timestamp}.mp4"))
|
| 280 |
+
if not downloaded_files:
|
| 281 |
+
return {"error": "Download failed. Report error on Telegram", "url": "https://t.me/chrunoss"}
|
| 282 |
+
|
| 283 |
+
downloaded_file = downloaded_files[0]
|
| 284 |
+
encoded_filename = urllib.parse.quote(downloaded_file.name)
|
| 285 |
+
download_url = f"{BASE_URL}/file/{encoded_filename}"
|
| 286 |
+
|
| 287 |
+
gc.collect()
|
| 288 |
+
|
| 289 |
+
return {"url": download_url}
|