Reputation: 23
I have an issue, i am trying to make Python search for videos through the Youtube API, a part of the code doesn't work. When i try to get the tags of the video, it gives me an error.
So far i have used the example from Google "search_by_keyword". It has worked well. I have however changed it a little. What i want is Python to give me video IDs with the tags in that video. I tried debugging, by making it print the whole "snippet" part of the table, it appears that "tags" are not found at all in "snippet"
only a bit of the code have been added, the rest is working find.
for search_result in search_response.get("items", []):
if search_result["id"]["kind"] == "youtube#video":
videos.append("%s (%s)" % (search_result["snippet"]["tags"],
search_result["id"]["videoId"]))
print ("Videos:\n", "\n".join(videos), "\n")
At
videos.append("%s (%s)" % (search_result["snippet"]["tags"]
i expect it here to give me the tags of the video, but it only comes up with a traceback error "keyerror: tags".
Trying to make it print the whole "snippet" part, gives me everything but "tags" AND "categoryid"
I think the question comes down to: Where do i find the "tags" element in the data table?
Upvotes: 1
Views: 704
Reputation: 6955
As per Google's docs (https://developers.google.com/youtube/v3/docs/search#resource) the JSON object snippet obtained from Search endpoint has no tags member. Therefore, your code getting a KeyError exception exhibits correct behavior.
For reaching the tags property of a video resource (https://developers.google.com/youtube/v3/docs/videos#snippet.tags[]) you have to make a separate API call for each video you're interested in on Videos endpoint (https://developers.google.com/youtube/v3/docs/videos/list).
Addendum: you may alleviate calling the API several times on Videos endpoint for the videos you're interested in by specifying those videos -- in one API call only -- to the parameter id as a comma-separated list of video ids (https://developers.google.com/youtube/v3/docs/videos/list#id).
Upvotes: 2