use search api
Browse fileshttps://claude.ai/share/3de1aded-e0e3-4f52-9cc4-6d7dcd1ae927
- yt_utils.py +58 -27
yt_utils.py
CHANGED
|
@@ -4,48 +4,79 @@ import os
|
|
| 4 |
YT_API_KEY = os.getenv("YT_API_KEY")
|
| 5 |
|
| 6 |
def get_latest_video_id(channel_id, device_name=None, playlist_id=None):
|
| 7 |
-
|
| 8 |
if device_name is None and playlist_id is None:
|
| 9 |
-
|
| 10 |
|
| 11 |
if device_name is not None and playlist_id is not None:
|
| 12 |
-
|
| 13 |
|
| 14 |
if playlist_id is None:
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
|
| 35 |
-
# Step 2: get the most recent video
|
| 36 |
-
|
| 37 |
-
url = 'https://www.googleapis.com/youtube/v3/playlistItems'
|
| 38 |
params = {
|
| 39 |
'part': 'snippet',
|
| 40 |
-
'
|
| 41 |
'maxResults': 1,
|
|
|
|
|
|
|
| 42 |
'key': YT_API_KEY
|
| 43 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
res = requests.get(url, params=params)
|
| 45 |
res.raise_for_status()
|
| 46 |
items = res.json().get("items", [])
|
| 47 |
-
|
| 48 |
if not items:
|
| 49 |
return None
|
| 50 |
-
|
| 51 |
-
return items[0]["
|
|
|
|
| 4 |
YT_API_KEY = os.getenv("YT_API_KEY")
|
| 5 |
|
| 6 |
def get_latest_video_id(channel_id, device_name=None, playlist_id=None):
|
|
|
|
| 7 |
if device_name is None and playlist_id is None:
|
| 8 |
+
raise Exception("Must specify either device_name or playlist_id")
|
| 9 |
|
| 10 |
if device_name is not None and playlist_id is not None:
|
| 11 |
+
print("Both device_name and playlist_id entered.. device_name will be ignored.")
|
| 12 |
|
| 13 |
if playlist_id is None:
|
| 14 |
+
# Step 1: get all playlists from the channel (note that 'playlists' in url)
|
| 15 |
+
url = 'https://www.googleapis.com/youtube/v3/playlists'
|
| 16 |
+
params = {
|
| 17 |
+
'part': 'snippet',
|
| 18 |
+
'channelId': channel_id, # Fixed: was CHANNEL_ID (undefined variable)
|
| 19 |
+
'maxResults': 1000,
|
| 20 |
+
'key': YT_API_KEY
|
| 21 |
+
}
|
| 22 |
+
res = requests.get(url, params=params)
|
| 23 |
+
res.raise_for_status()
|
| 24 |
+
playlists = res.json().get("items", [])
|
| 25 |
|
| 26 |
+
for p in playlists:
|
| 27 |
+
if device_name.lower() in p["snippet"]["title"].lower():
|
| 28 |
+
playlist_id = p["id"]
|
| 29 |
+
break
|
| 30 |
|
| 31 |
+
if not playlist_id:
|
| 32 |
+
raise Exception(f"No playlist found matching device name '{device_name}'")
|
| 33 |
|
| 34 |
+
# Step 2: Use search API instead of playlistItems to get the most recent video by publication date
|
| 35 |
+
url = 'https://www.googleapis.com/youtube/v3/search'
|
|
|
|
| 36 |
params = {
|
| 37 |
'part': 'snippet',
|
| 38 |
+
'channelId': channel_id,
|
| 39 |
'maxResults': 1,
|
| 40 |
+
'order': 'date', # This sorts by publication date (newest first)
|
| 41 |
+
'type': 'video', # Only return videos
|
| 42 |
'key': YT_API_KEY
|
| 43 |
}
|
| 44 |
+
|
| 45 |
+
# If we have a playlist_id, we need to get videos from that specific playlist
|
| 46 |
+
# Note: To properly filter by both channel and playlist, we need an additional step
|
| 47 |
+
if playlist_id:
|
| 48 |
+
# First get videos from the playlist
|
| 49 |
+
playlist_url = 'https://www.googleapis.com/youtube/v3/playlistItems'
|
| 50 |
+
playlist_params = {
|
| 51 |
+
'part': 'snippet,contentDetails',
|
| 52 |
+
'playlistId': playlist_id,
|
| 53 |
+
'maxResults': 10, # Get several videos to ensure we find the latest
|
| 54 |
+
'key': YT_API_KEY
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
playlist_res = requests.get(playlist_url, params=playlist_params)
|
| 58 |
+
playlist_res.raise_for_status()
|
| 59 |
+
playlist_items = playlist_res.json().get("items", [])
|
| 60 |
+
|
| 61 |
+
if not playlist_items:
|
| 62 |
+
return None
|
| 63 |
+
|
| 64 |
+
# Sort by publish date (newest first)
|
| 65 |
+
sorted_items = sorted(
|
| 66 |
+
playlist_items,
|
| 67 |
+
key=lambda x: x["snippet"].get("publishedAt", ""),
|
| 68 |
+
reverse=True
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
# Return the newest video ID
|
| 72 |
+
return sorted_items[0]["snippet"]["resourceId"]["videoId"]
|
| 73 |
+
|
| 74 |
+
# If we're not filtering by playlist, just use the search API
|
| 75 |
res = requests.get(url, params=params)
|
| 76 |
res.raise_for_status()
|
| 77 |
items = res.json().get("items", [])
|
| 78 |
+
|
| 79 |
if not items:
|
| 80 |
return None
|
| 81 |
+
|
| 82 |
+
return items[0]["id"]["videoId"] # Note the different path to videoId for search results
|