python-3.xyoutube-apigoogle-client

Youtube v3 Api Get video by ID


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?


Solution

  • 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