Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,6 +8,7 @@ import os
|
|
| 8 |
import pytz
|
| 9 |
import yaml
|
| 10 |
import json
|
|
|
|
| 11 |
import typing
|
| 12 |
from typing import List, Dict, Optional
|
| 13 |
from tools.final_answer import FinalAnswerTool
|
|
@@ -27,22 +28,22 @@ def search_youtube_guitar_tutorials(song_name: str) -> List[Dict[str, Any]]:
|
|
| 27 |
A list of dictionaries containing information about the tutorials found,
|
| 28 |
including title, channel name, view count, and URL.
|
| 29 |
"""
|
| 30 |
-
|
| 31 |
api_key = os.getenv("YOUTUBE_API_KEY")
|
| 32 |
if not api_key:
|
| 33 |
raise ValueError("YouTube API key not found. Please set the YOUTUBE_API_KEY environment variable.")
|
| 34 |
-
|
| 35 |
base_url = "https://www.googleapis.com/youtube/v3/search"
|
| 36 |
search_params = {
|
| 37 |
'part': 'snippet',
|
| 38 |
'q': f"{song_name} guitar tutorial",
|
| 39 |
'type': 'video',
|
|
|
|
|
|
|
| 40 |
'maxResults': 5,
|
| 41 |
'key': api_key
|
| 42 |
}
|
| 43 |
search_url = f"{base_url}?{urlencode(search_params)}"
|
| 44 |
-
|
| 45 |
-
|
| 46 |
try:
|
| 47 |
search_response = requests.get(search_url)
|
| 48 |
search_response.raise_for_status()
|
|
@@ -52,7 +53,7 @@ def search_youtube_guitar_tutorials(song_name: str) -> List[Dict[str, Any]]:
|
|
| 52 |
video_ids = [item['id']['videoId'] for item in search_data.get('items', [])]
|
| 53 |
if not video_ids:
|
| 54 |
return []
|
| 55 |
-
|
| 56 |
# Get video details (including view counts)
|
| 57 |
video_url = "https://www.googleapis.com/youtube/v3/videos"
|
| 58 |
video_params = {
|
|
@@ -64,7 +65,7 @@ def search_youtube_guitar_tutorials(song_name: str) -> List[Dict[str, Any]]:
|
|
| 64 |
video_response = requests.get(video_details_url)
|
| 65 |
video_response.raise_for_status()
|
| 66 |
video_data = video_response.json()
|
| 67 |
-
|
| 68 |
results = []
|
| 69 |
for item in video_data.get('items', [])[:3]: # Limit to top 3
|
| 70 |
results.append({
|
|
@@ -75,12 +76,11 @@ def search_youtube_guitar_tutorials(song_name: str) -> List[Dict[str, Any]]:
|
|
| 75 |
})
|
| 76 |
|
| 77 |
return results
|
| 78 |
-
|
| 79 |
except requests.exceptions.RequestException as e:
|
| 80 |
print(f"Error searching YouTube: {e}")
|
| 81 |
return []
|
| 82 |
|
| 83 |
-
|
| 84 |
# Initialize tools
|
| 85 |
final_answer = FinalAnswerTool()
|
| 86 |
|
|
|
|
| 8 |
import pytz
|
| 9 |
import yaml
|
| 10 |
import json
|
| 11 |
+
from urllib.parse import urlencode
|
| 12 |
import typing
|
| 13 |
from typing import List, Dict, Optional
|
| 14 |
from tools.final_answer import FinalAnswerTool
|
|
|
|
| 28 |
A list of dictionaries containing information about the tutorials found,
|
| 29 |
including title, channel name, view count, and URL.
|
| 30 |
"""
|
|
|
|
| 31 |
api_key = os.getenv("YOUTUBE_API_KEY")
|
| 32 |
if not api_key:
|
| 33 |
raise ValueError("YouTube API key not found. Please set the YOUTUBE_API_KEY environment variable.")
|
| 34 |
+
|
| 35 |
base_url = "https://www.googleapis.com/youtube/v3/search"
|
| 36 |
search_params = {
|
| 37 |
'part': 'snippet',
|
| 38 |
'q': f"{song_name} guitar tutorial",
|
| 39 |
'type': 'video',
|
| 40 |
+
'videoDefinition': 'high',
|
| 41 |
+
'order': 'viewCount',
|
| 42 |
'maxResults': 5,
|
| 43 |
'key': api_key
|
| 44 |
}
|
| 45 |
search_url = f"{base_url}?{urlencode(search_params)}"
|
| 46 |
+
|
|
|
|
| 47 |
try:
|
| 48 |
search_response = requests.get(search_url)
|
| 49 |
search_response.raise_for_status()
|
|
|
|
| 53 |
video_ids = [item['id']['videoId'] for item in search_data.get('items', [])]
|
| 54 |
if not video_ids:
|
| 55 |
return []
|
| 56 |
+
|
| 57 |
# Get video details (including view counts)
|
| 58 |
video_url = "https://www.googleapis.com/youtube/v3/videos"
|
| 59 |
video_params = {
|
|
|
|
| 65 |
video_response = requests.get(video_details_url)
|
| 66 |
video_response.raise_for_status()
|
| 67 |
video_data = video_response.json()
|
| 68 |
+
|
| 69 |
results = []
|
| 70 |
for item in video_data.get('items', [])[:3]: # Limit to top 3
|
| 71 |
results.append({
|
|
|
|
| 76 |
})
|
| 77 |
|
| 78 |
return results
|
| 79 |
+
|
| 80 |
except requests.exceptions.RequestException as e:
|
| 81 |
print(f"Error searching YouTube: {e}")
|
| 82 |
return []
|
| 83 |
|
|
|
|
| 84 |
# Initialize tools
|
| 85 |
final_answer = FinalAnswerTool()
|
| 86 |
|