Reputation: 1230
How can I get a single video by its ID
using the Youtube API v3
.
So far I've only come across search mechanisms and other, but not specifically one to get a single video, the code that I have currently is:
def youtube_search(q, max_results=1,order="relevance", token=None, location=None, location_radius=None):
youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION, developerKey=DEVELOPER_KEY)
search_response = youtube.search().list(
q=q,
type="video",
pageToken=token,
order = order,
part="id,snippet",
maxResults=max_results,
location=location,
locationRadius=location_radius
).execute()
videos = []
for search_result in search_response.get("items", []):
if search_result["id"]["kind"] == "youtube#video":
videos.append(search_result)
try:
nexttok = search_response["nextPageToken"]
return(nexttok, videos)
except Exception as e:
nexttok = "last_page"
return(nexttok, videos)
But I've realized that this method is not efficient but rather wastes time and resources. How can I go about it?
Upvotes: 1
Views: 10716
Reputation: 71
To access a specific video in Python can be done by video()
method instead of search()
, and with a parameter id
in list()
method. id
is an argument that can be found in an URL of your Youtube video in format https://www.youtube.com/watch?v={video_id}[&{otherParameters}={values}]
(for an example https://www.youtube.com/watch?v=qEB0FIBoKAg&t=15s
). In your URL, the value of v
(after "=" symbol) is the ID of the video you are currently watching. The "&" symbol means to add other arguments (here, t
is "start time of the video" and starts at 15 seconds).
How I do it in a code is:
...
# Create API service
serv = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION, developerKey=DEVELOPER_KEY)
# Make request. part must be "snippet", id is your video, and maxResults is optional
req = serv.videos().list(part = "snippet", id = video_id, maxResults = 1)
# get the JSON file (can be accessed as a dictionary)
response = req.execute()
# Access the (standard) thumbnail (URL)
sd_thumbnail = response["items"][0]["snippet"]["thumbnails"]["standard"]["url"]
# If you want other resolutions than "standard" you can change it into "default", "medium" or "high"
print(sd_thumbnail)
This program will print:
https://i.ytimg.com/vi/qEB0FIBoKAg/sddefault.jpg
It's format is https://i.ytimg.com/vi/{video_id}/{file}.jpg
Instead of file
here can be "default", "mqdefault", "hqdefault" and "sddefautl".
Youtube has a good documentation about their APIs. More about your question can be answered in https://developers.google.com/youtube/v3/docs/videos/list
Upvotes: 1
Reputation: 3004
Configure your api in Google play console and playground. Get api key and access token then call this API to get video by Id.
You can test from here as well link
GET https://www.googleapis.com/youtube/v3/videos?part=snippet&id=vuKRKt7aKpE&key=[YOUR_API_KEY]
Authorization: Bearer [YOUR_ACCESS_TOKEN]
Accept: application/json
Upvotes: 0
Reputation: 4185
YouTube API search call is for searches. You want to use the videos call and pass in the video ID(s) you want to query.
Example:
https://www.googleapis.com/youtube/v3/videos?part=snippet&id=xE_rMj35BIM&key=YOUR_KEY
Upvotes: 9