Update app.py
Browse files
app.py
CHANGED
@@ -7,6 +7,7 @@ from fastapi.concurrency import run_in_threadpool
|
|
7 |
import yt_dlp
|
8 |
import ffmpeg
|
9 |
import urllib.parse
|
|
|
10 |
import os
|
11 |
from datetime import datetime, timedelta
|
12 |
import schedule
|
@@ -549,6 +550,34 @@ def get_youtube_download_url_from_apis(video_url: str) -> str:
|
|
549 |
|
550 |
logger.info("Both APIs failed to retrieve a download URL.")
|
551 |
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
552 |
|
553 |
@app.post("/multi")
|
554 |
async def multi_download(request: Request):
|
@@ -593,9 +622,19 @@ async def multi_download(request: Request):
|
|
593 |
error_message = "Quality above 720p is for Premium Members Only. Please check the URL for more information."
|
594 |
help_url = "https://chrunos.com/premium-shortcuts/" # Replace with your actual URL
|
595 |
return {"error": error_message, "url": help_url}
|
596 |
-
|
597 |
-
|
598 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
599 |
|
600 |
'''if is_youtube_url:
|
601 |
dl_url = get_youtube_download_url_from_apis(video_url)
|
@@ -616,15 +655,7 @@ async def multi_download(request: Request):
|
|
616 |
|
617 |
else:
|
618 |
'''
|
619 |
-
|
620 |
-
if dl_url:
|
621 |
-
remaining_requests_value = rate_limiter.max_requests - rate_limiter.get_current_count(user_ip)
|
622 |
-
dl_url["requests_remaining"] = remaining_requests_value
|
623 |
-
return dl_url
|
624 |
-
else:
|
625 |
-
return {
|
626 |
-
"error": "Failed to Fetch the video."
|
627 |
-
}
|
628 |
|
629 |
|
630 |
|
|
|
7 |
import yt_dlp
|
8 |
import ffmpeg
|
9 |
import urllib.parse
|
10 |
+
from urllib.parse import urlparse
|
11 |
import os
|
12 |
from datetime import datetime, timedelta
|
13 |
import schedule
|
|
|
550 |
|
551 |
logger.info("Both APIs failed to retrieve a download URL.")
|
552 |
return None
|
553 |
+
|
554 |
+
reads_api = os.getenv("reads_api")
|
555 |
+
def call_extract_endpoint(post_url: str) -> Dict[str, Any]:
|
556 |
+
|
557 |
+
try:
|
558 |
+
response = requests.get(f"{reads_api}/extract", params={"url": post_url})
|
559 |
+
response.raise_for_status()
|
560 |
+
return response.json()
|
561 |
+
except requests.RequestException as e:
|
562 |
+
raise requests.RequestException(f"Failed to call extract endpoint: {str(e)}")
|
563 |
+
except ValueError as e:
|
564 |
+
raise ValueError(f"Invalid response format: {str(e)}")
|
565 |
+
|
566 |
+
|
567 |
+
def is_threads_url(url: str) -> bool:
|
568 |
+
"""Validate if URL is a valid Threads URL"""
|
569 |
+
try:
|
570 |
+
parsed = urlparse(url)
|
571 |
+
logger.info(parsed)
|
572 |
+
# Check if netloc matches known Threads domains
|
573 |
+
if parsed.netloc not in ['threads.net', 'www.threads.net', 'threads.com', 'www.threads.com']:
|
574 |
+
return False
|
575 |
+
# Check if path contains /post/ or /t/ after a username
|
576 |
+
if '/post/' in parsed.path or '/t/' in parsed.path:
|
577 |
+
return True
|
578 |
+
return False
|
579 |
+
except Exception:
|
580 |
+
return False
|
581 |
|
582 |
@app.post("/multi")
|
583 |
async def multi_download(request: Request):
|
|
|
622 |
error_message = "Quality above 720p is for Premium Members Only. Please check the URL for more information."
|
623 |
help_url = "https://chrunos.com/premium-shortcuts/" # Replace with your actual URL
|
624 |
return {"error": error_message, "url": help_url}
|
625 |
+
if is_threads_url(video_url):
|
626 |
+
return call_extract_endpoint(video_url)
|
627 |
+
else:
|
628 |
+
dl_url = await get_track_download_url(video_url, quality)
|
629 |
+
if dl_url:
|
630 |
+
remaining_requests_value = rate_limiter.max_requests - rate_limiter.get_current_count(user_ip)
|
631 |
+
dl_url["requests_remaining"] = remaining_requests_value
|
632 |
+
return dl_url
|
633 |
+
else:
|
634 |
+
return {
|
635 |
+
"error": "Failed to Fetch the video."
|
636 |
+
}
|
637 |
+
#is_youtube_url = re.search(r'(youtube\.com|youtu\.be)', video_url) is not None
|
638 |
|
639 |
'''if is_youtube_url:
|
640 |
dl_url = get_youtube_download_url_from_apis(video_url)
|
|
|
655 |
|
656 |
else:
|
657 |
'''
|
658 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
659 |
|
660 |
|
661 |
|