youtubeAPIspython
Ben Gorman

Ben Gorman

Life's a garden. Dig it.

Challenge

Use the YouTube Data API to fetch up to 200 videos about coffee ☕ such that

  • each video was published in 2022
  • each video's location is New York City :fontawesome-solid-city:

Solution

from googleapiclient.discovery import build
 
# Instantiate a googleapiclient.discovery.Resource object for youtube
youtube = build(
  serviceName='youtube', 
  version='v3', 
  developerKey='YOURAPIKEY'
)
 
# List to store response objects
responses = []
 
# Get the first response
request = youtube.search().list(
  part="snippet",
  type="video",
  q="coffee",
  publishedAfter="2022-01-01T00:00:00Z",
  location="40.7128,-74.0060",
  locationRadius="10mi",
  maxResults=50
)
response = request.execute()
responses.append(response)
 
# Iterate until the "current" response doesn't have nextPageToken
# or until we have 4 responses
while "nextPageToken" in response and len(responses) < 4:
  request = youtube.search().list(
    part="snippet",
    type="video",
    q="coffee",
    publishedAfter="2022-01-01T00:00:00Z",
    location="40.7128,-74.0060",
    locationRadius="10mi",
    maxResults=50,
    pageToken=response['nextPageToken']
  )
  response = request.execute()
  responses.append(response)
 
# Close the connection
youtube.close()
 
# Flatten the nested items lists
items = [item for response in responses for item in response['items']]
print(len(items)) # 200

Inspect the results

# Print the first 10 videos' titles
for i in range(10):
  print(items[i]['snippet']['title'])
 
# Black coffee | Brooklyn mirage closing season day 3 | 10/16/2022
# The Importance of Making Coffee
# Al Pacino Starring in Chinese Coffee (full movie)
# 5 Best Coffee Shops in New York City ☕️
# New York Cozy Coffee Shop - Smooth Jazz Instrumental Music for Stress Relief
# Best Electric Coffee Percolators Consumer Reports
# Black coffee closing set part 2 |brooklyn mirage ny |closing season day 2 | 10/15/2022
# Rainy Day Coffee Shop Ambience With Smooth Jazz, Rain Sounds for Study, Work, Relaxing
# Hanging out with SEY Coffee!
# The BEST Coffee Shops in New York City ☕🗽

Explanation

In this solution we use the search.list resource.

search.list is expensive 💰

Every call to search.list costs 100 quota. This means you could exhaust your 10,000 quota per day limit just by calling search.list 100 times 😬

search.list lets us find videos (or channels) that match one or more search terms (in this case "coffee"). We can also pre-filter the results by published date and location.

Our first call to search.list() looks like this 👇

request = youtube.search().list(
  part="snippet", # (1)!
  type="video", # (2)!
  q="coffee", # (3)!
  publishedAfter="2022-01-01T00:00:00Z", # (4)!
  location="40.7128,-74.0060", # (5)!
  locationRadius="10mi", # (6)!
  maxResults=50
)
  1. part (required)

    string
    The part parameter specifies a comma-separated list of one or more search resource properties that the API response will include. Set the parameter value to snippet.

  2. type

    string
    The type parameter restricts a search query to only retrieve a particular type of resource. The value is a comma-separated list of resource types. The default value is video, channel, playlist.

    Acceptable values are:

    • channel
    • playlist
    • video
  3. q

    string
    The q parameter specifies the query term to search for.

    Your request can also use the Boolean NOT (-) and OR (|) operators to exclude videos or to find videos that are associated with one of several search terms. For example, to search for videos matching either "boating" or "sailing", set the q parameter value to boating|sailing. Similarly, to search for videos matching either "boating" or "sailing" but not "fishing", set the q parameter value to boating|sailing -fishing. Note that the pipe character must be URL-escaped when it is sent in your API request. The URL-escaped value for the pipe character is %7C.

  4. publishedAfter

    datetime
    The publishedAfter parameter indicates that the API response should only contain resources created at or after the specified time. The value is an RFC 3339 formatted date-time value (1970-01-01T00:00:00Z).

  5. location

    string The location parameter, in conjunction with the locationRadius parameter, defines a circular geographic area and also restricts a search to videos that specify, in their metadata, a geographic location that falls within that area. The parameter value is a string that specifies latitude/longitude coordinates e.g. (37.42307,-122.08427).

    • The location parameter value identifies the point at the center of the area.
    • The locationRadius parameter specifies the maximum distance that the location associated with a video can be from that point for the video to still be included in the search results.

    The API returns an error if your request specifies a value for the location parameter but does not also specify a value for the locationRadius parameter.

    Note: If you specify a value for this parameter, you must also set the type parameter's value to video.

  6. locationRadius

    string
    The locationRadius parameter, in conjunction with the location parameter, defines a circular geographic area.

    The parameter value must be a floating point number followed by a measurement unit. Valid measurement units are m, km, ft, and mi. For example, valid parameter values include 1500m, 5km, 10000ft, and 0.75mi. The API does not support locationRadius parameter values larger than 1000 kilometers.

    Note: See the definition of the location parameter for more information.

New York City location

If you search "New York City lat and lon" on Google, you'll get back

40.7128° N, 74.0060° W

The location value we send to the API is actually "40.7128,-74.0060". Note the negative longitude coordinate.

Pagination

The other slightly tricky part of this query is handling pagination, which we explain in detail in our solution to 3Blue1Brown.