Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException
|
| 2 |
+
from ytmusicapi import YTMusic
|
| 3 |
+
from typing import Optional
|
| 4 |
+
from pydantic import BaseModel
|
| 5 |
+
import uvicorn
|
| 6 |
+
|
| 7 |
+
app = FastAPI(title="YouTube Music Search API")
|
| 8 |
+
|
| 9 |
+
# Initialize the YTMusic API
|
| 10 |
+
ytmusic = YTMusic()
|
| 11 |
+
|
| 12 |
+
class SearchResponse(BaseModel):
|
| 13 |
+
title: str
|
| 14 |
+
duration: str
|
| 15 |
+
video_id: str
|
| 16 |
+
artists: list[str]
|
| 17 |
+
album: dict
|
| 18 |
+
youtube_music_url: str
|
| 19 |
+
youtube_video_url: str
|
| 20 |
+
song_thumbnail_url: str
|
| 21 |
+
album_thumbnail_url: Optional[str]
|
| 22 |
+
|
| 23 |
+
@app.get("/search/", response_model=list[SearchResponse])
|
| 24 |
+
async def search_songs(query: str, filter_type: str = 'songs', limit: int = 5):
|
| 25 |
+
"""
|
| 26 |
+
Search for songs on YouTube Music and return enriched metadata
|
| 27 |
+
|
| 28 |
+
Args:
|
| 29 |
+
query: Search query string
|
| 30 |
+
filter_type: Type of search filter (default: songs)
|
| 31 |
+
limit: Maximum number of results to return (default: 5)
|
| 32 |
+
"""
|
| 33 |
+
try:
|
| 34 |
+
search_results = ytmusic.search(query, filter=filter_type, limit=limit)
|
| 35 |
+
|
| 36 |
+
if not search_results:
|
| 37 |
+
raise HTTPException(status_code=404, detail="No results found")
|
| 38 |
+
|
| 39 |
+
enriched_results = []
|
| 40 |
+
|
| 41 |
+
for song in search_results:
|
| 42 |
+
video_id = song.get('videoId', 'N/A')
|
| 43 |
+
|
| 44 |
+
# Handle multiple artists
|
| 45 |
+
artists = song.get('artists', [])
|
| 46 |
+
artist_names = [artist['name'] for artist in artists] if artists else ['N/A']
|
| 47 |
+
|
| 48 |
+
# Get album details
|
| 49 |
+
album = song.get('album', {})
|
| 50 |
+
album_id = album.get('id', None)
|
| 51 |
+
|
| 52 |
+
# Construct album data
|
| 53 |
+
album_data = {
|
| 54 |
+
'name': album.get('name', 'N/A'),
|
| 55 |
+
'id': album_id,
|
| 56 |
+
'url': f"https://music.youtube.com/browse/{album_id}" if album_id else "N/A"
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
# Get album thumbnail if available
|
| 60 |
+
album_thumbnail_url = None
|
| 61 |
+
if album_id:
|
| 62 |
+
try:
|
| 63 |
+
album_details = ytmusic.get_album(album_id)
|
| 64 |
+
album_thumbnail_url = album_details.get('thumbnails', [{}])[-1].get('url', None)
|
| 65 |
+
except Exception:
|
| 66 |
+
pass
|
| 67 |
+
|
| 68 |
+
# Construct URLs
|
| 69 |
+
youtube_music_url = f"https://music.youtube.com/watch?v={video_id}" if video_id != 'N/A' else 'N/A'
|
| 70 |
+
youtube_video_url = f"https://www.youtube.com/watch?v={video_id}" if video_id != 'N/A' else 'N/A'
|
| 71 |
+
|
| 72 |
+
# Get song thumbnail URL (highest resolution)
|
| 73 |
+
song_thumbnail_url = song.get('thumbnails', [{}])[-1].get('url', 'N/A')
|
| 74 |
+
|
| 75 |
+
result = SearchResponse(
|
| 76 |
+
title=song.get('title', 'N/A'),
|
| 77 |
+
duration=song.get('duration', 'N/A'),
|
| 78 |
+
video_id=video_id,
|
| 79 |
+
artists=artist_names,
|
| 80 |
+
album=album_data,
|
| 81 |
+
youtube_music_url=youtube_music_url,
|
| 82 |
+
youtube_video_url=youtube_video_url,
|
| 83 |
+
song_thumbnail_url=song_thumbnail_url,
|
| 84 |
+
album_thumbnail_url=album_thumbnail_url
|
| 85 |
+
)
|
| 86 |
+
|
| 87 |
+
enriched_results.append(result)
|
| 88 |
+
|
| 89 |
+
return enriched_results
|
| 90 |
+
|
| 91 |
+
except Exception as e:
|
| 92 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 93 |
+
|
| 94 |
+
@app.get("/")
|
| 95 |
+
async def root():
|
| 96 |
+
"""Root endpoint that provides API information"""
|
| 97 |
+
return {
|
| 98 |
+
"name": "YouTube Music Search API",
|
| 99 |
+
"version": "1.0",
|
| 100 |
+
"description": "API for searching songs on YouTube Music with enriched metadata",
|
| 101 |
+
"endpoints": {
|
| 102 |
+
"search": "/search/?query=<search_term>&filter_type=songs&limit=5"
|
| 103 |
+
}
|
| 104 |
+
}
|
| 105 |
+
|
| 106 |
+
if __name__ == "__main__":
|
| 107 |
+
import uvicorn
|
| 108 |
+
uvicorn.run("app:app", host="0.0.0.0", port=8000, reload=True)
|