Update app.py
Browse files
app.py
CHANGED
@@ -152,7 +152,7 @@ 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("/
|
156 |
async def download_high_quality_video(request: Request):
|
157 |
user_ip = get_user_ip(request)
|
158 |
user_info = request_counts[user_ip]
|
@@ -221,82 +221,7 @@ async def download_high_quality_video(request: Request):
|
|
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 |
|
|
|
152 |
"""Helper function to get user's IP address."""
|
153 |
return request.client.host
|
154 |
|
155 |
+
@app.post("/maxs")
|
156 |
async def download_high_quality_video(request: Request):
|
157 |
user_ip = get_user_ip(request)
|
158 |
user_info = request_counts[user_ip]
|
|
|
221 |
|
222 |
|
223 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
224 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
225 |
|
226 |
|
227 |
|