Spaces:
Paused
Paused
Update main.py
Browse files
main.py
CHANGED
|
@@ -4,7 +4,7 @@ from fastapi.responses import JSONResponse
|
|
| 4 |
import cloudinary
|
| 5 |
import cloudinary.uploader
|
| 6 |
from cloudinary.utils import cloudinary_url
|
| 7 |
-
from
|
| 8 |
|
| 9 |
app = FastAPI()
|
| 10 |
|
|
@@ -39,16 +39,32 @@ async def compress_image(image_url: str = Query(..., description="URL of the ima
|
|
| 39 |
|
| 40 |
# YouTube video download endpoint
|
| 41 |
@app.get("/download_youtube_video")
|
| 42 |
-
async def download_youtube_video(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
try:
|
| 44 |
-
#
|
| 45 |
-
|
| 46 |
|
| 47 |
-
#
|
| 48 |
-
|
|
|
|
|
|
|
| 49 |
|
| 50 |
-
#
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
except Exception as e:
|
| 54 |
# Handle any error and return a response
|
|
|
|
| 4 |
import cloudinary
|
| 5 |
import cloudinary.uploader
|
| 6 |
from cloudinary.utils import cloudinary_url
|
| 7 |
+
from ytube_api import Ytube # Importing the Ytube module
|
| 8 |
|
| 9 |
app = FastAPI()
|
| 10 |
|
|
|
|
| 39 |
|
| 40 |
# YouTube video download endpoint
|
| 41 |
@app.get("/download_youtube_video")
|
| 42 |
+
async def download_youtube_video(
|
| 43 |
+
query: str = Query(..., description="Search query for the YouTube video"),
|
| 44 |
+
format: str = Query("mp4", description="Video format to download, e.g., mp4"),
|
| 45 |
+
quality: str = Query("1080", description="Video quality, e.g., 1080")
|
| 46 |
+
):
|
| 47 |
try:
|
| 48 |
+
# Initialize the Ytube instance
|
| 49 |
+
yt = Ytube()
|
| 50 |
|
| 51 |
+
# Search for videos using the provided query
|
| 52 |
+
search_results = yt.search_videos(query)
|
| 53 |
+
if not search_results.items:
|
| 54 |
+
return JSONResponse(content={"error": "No videos found for the query"}, status_code=404)
|
| 55 |
|
| 56 |
+
# Select the first result from the search results
|
| 57 |
+
target_video = search_results.items[0]
|
| 58 |
+
|
| 59 |
+
# Get the download link for the specified format and quality
|
| 60 |
+
download_link = yt.get_download_link(target_video, format=format, quality=quality)
|
| 61 |
+
|
| 62 |
+
# Return the download link and metadata
|
| 63 |
+
return JSONResponse(content={
|
| 64 |
+
"download_url": download_link.url,
|
| 65 |
+
"filename": download_link.filename,
|
| 66 |
+
"status": download_link.status
|
| 67 |
+
})
|
| 68 |
|
| 69 |
except Exception as e:
|
| 70 |
# Handle any error and return a response
|