Spaces:
Paused
Paused
Update main.py
Browse files
main.py
CHANGED
|
@@ -31,7 +31,7 @@ async def compress_image(image_url: str = Query(..., description="URL of the ima
|
|
| 31 |
except Exception as e:
|
| 32 |
return JSONResponse(content={"error": str(e)}, status_code=500)
|
| 33 |
|
| 34 |
-
# YouTube video
|
| 35 |
@app.get("/youtube_video_options")
|
| 36 |
async def youtube_video_options(
|
| 37 |
query: str = Query(..., description="Search query for the YouTube video")
|
|
@@ -39,28 +39,42 @@ async def youtube_video_options(
|
|
| 39 |
try:
|
| 40 |
yt = Ytube()
|
| 41 |
search_results = yt.search_videos(query)
|
| 42 |
-
|
| 43 |
if not search_results.items:
|
| 44 |
return JSONResponse(content={"error": "No videos found for the query"}, status_code=404)
|
| 45 |
|
| 46 |
-
#
|
|
|
|
|
|
|
|
|
|
| 47 |
available_downloads = []
|
| 48 |
-
|
| 49 |
-
for video in search_results.items:
|
| 50 |
-
available_downloads.append({
|
| 51 |
-
"title": video.title,
|
| 52 |
-
"video_id": video.id,
|
| 53 |
-
"size": video.size,
|
| 54 |
-
"duration": video.duration,
|
| 55 |
-
"channel": video.channelTitle,
|
| 56 |
-
"source": video.source,
|
| 57 |
-
"quality": "unknown", # Assuming search does not provide quality; update if available.
|
| 58 |
-
"format": "mp4" # Assuming default format mp4
|
| 59 |
-
})
|
| 60 |
|
| 61 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
return JSONResponse(content={
|
| 63 |
-
"
|
| 64 |
"available_downloads": available_downloads
|
| 65 |
})
|
| 66 |
|
|
@@ -70,7 +84,7 @@ async def youtube_video_options(
|
|
| 70 |
# Root endpoint
|
| 71 |
@app.get("/")
|
| 72 |
def root():
|
| 73 |
-
return "
|
| 74 |
|
| 75 |
if __name__ == "__main__":
|
| 76 |
import uvicorn
|
|
|
|
| 31 |
except Exception as e:
|
| 32 |
return JSONResponse(content={"error": str(e)}, status_code=500)
|
| 33 |
|
| 34 |
+
# YouTube video download options endpoint
|
| 35 |
@app.get("/youtube_video_options")
|
| 36 |
async def youtube_video_options(
|
| 37 |
query: str = Query(..., description="Search query for the YouTube video")
|
|
|
|
| 39 |
try:
|
| 40 |
yt = Ytube()
|
| 41 |
search_results = yt.search_videos(query)
|
|
|
|
| 42 |
if not search_results.items:
|
| 43 |
return JSONResponse(content={"error": "No videos found for the query"}, status_code=404)
|
| 44 |
|
| 45 |
+
# Get the top video result from search results
|
| 46 |
+
target_video = search_results.items[0]
|
| 47 |
+
|
| 48 |
+
# Initialize an empty list for available download options
|
| 49 |
available_downloads = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
+
# Check common qualities and formats (replace with dynamically available ones if possible)
|
| 52 |
+
qualities = ["144", "240", "360", "480", "720", "1080"]
|
| 53 |
+
formats = ["mp4"]
|
| 54 |
+
|
| 55 |
+
for quality in qualities:
|
| 56 |
+
for fmt in formats:
|
| 57 |
+
try:
|
| 58 |
+
# Try to get the download link for each quality and format
|
| 59 |
+
download_link = yt.get_download_link(target_video, format=fmt, quality=quality)
|
| 60 |
+
|
| 61 |
+
# Only add if a valid link is returned
|
| 62 |
+
if download_link.url:
|
| 63 |
+
available_downloads.append({
|
| 64 |
+
"quality": quality,
|
| 65 |
+
"format": fmt,
|
| 66 |
+
"download_url": download_link.url
|
| 67 |
+
})
|
| 68 |
+
except Exception as e:
|
| 69 |
+
print(f"Quality {quality} and format {fmt} not available: {e}")
|
| 70 |
+
continue
|
| 71 |
+
|
| 72 |
+
if not available_downloads:
|
| 73 |
+
return JSONResponse(content={"error": "No available download options found."}, status_code=404)
|
| 74 |
+
|
| 75 |
+
# Return the video metadata and available download options
|
| 76 |
return JSONResponse(content={
|
| 77 |
+
"video_title": target_video.title,
|
| 78 |
"available_downloads": available_downloads
|
| 79 |
})
|
| 80 |
|
|
|
|
| 84 |
# Root endpoint
|
| 85 |
@app.get("/")
|
| 86 |
def root():
|
| 87 |
+
return "Welcome to the Image Compression and YouTube Downloader API"
|
| 88 |
|
| 89 |
if __name__ == "__main__":
|
| 90 |
import uvicorn
|