Akane710 commited on
Commit
9a30748
·
verified ·
1 Parent(s): 78df3e6

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +24 -8
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 y2mate_api import Handler # Importing the Y2Mate handler
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(video_url: str = Query(..., description="URL of the YouTube video to download")):
 
 
 
 
43
  try:
44
- # Step 1: Create a handler object for the Y2Mate API
45
- api = Handler(video_url)
46
 
47
- # Step 2: Fetch video metadata and download links
48
- video_metadata = [metadata for metadata in api.run()]
 
 
49
 
50
- # Step 3: Return the metadata or download links
51
- return JSONResponse(content={"video_metadata": video_metadata})
 
 
 
 
 
 
 
 
 
 
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