Akane710 commited on
Commit
6d1f8fa
·
verified ·
1 Parent(s): f3179f5

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +17 -32
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 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,43 +39,28 @@ async def youtube_video_options(
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
- "size": download_link.size,
67
- "download_url": download_link.url
68
- })
69
- except Exception as e:
70
- print(f"Quality {quality} and format {fmt} not available: {e}")
71
- continue
72
-
73
- if not available_downloads:
74
- return JSONResponse(content={"error": "No available download options found."}, status_code=404)
75
-
76
- # Return the video metadata and available download options
77
  return JSONResponse(content={
78
- "video_title": target_video.title,
79
  "available_downloads": available_downloads
80
  })
81
 
 
31
  except Exception as e:
32
  return JSONResponse(content={"error": str(e)}, status_code=500)
33
 
34
+ # YouTube video search and 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
+
43
  if not search_results.items:
44
  return JSONResponse(content={"error": "No videos found for the query"}, status_code=404)
45
 
46
+ # Collect metadata for each search result
 
 
 
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
+ # Return the search results with metadata
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
  return JSONResponse(content={
63
+ "query": query,
64
  "available_downloads": available_downloads
65
  })
66