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¶
Inspect the results
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 👇
-
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. -
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
-
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.
-
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). -
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.
-
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.