Challenge¶
Surely you've heard of MrBeast - the greatest youtuber ever! Use the YouTube Data API to fetch info about his channel. Specifically..
- What date did the MrBeast channel start?
- How many current subscribers does it have?
Solution¶
MrBeast started on 2012-02-20 and currently (2022-12-11) has 117M subscribers.
Code
Explanation
Since we want to fetch data about a YouTube channel it makes sense for us to inspect the channels.list
resource.
We'll need to tell the YouTube API which channel we'd like to fetch information about. One option is to use the channel's username MrBeast. Another option is to use the channel's id UCX6OQ3DkcsbYNE6H8uQQuVA.
The link we provided on the problem page references MrBeast's channel id, so we'll use that here.
The docs for channels.list
indicate that we need to provide one required parameter to the endpoint - part
- a comma separated string of resource properties. In our case, we'll use part="snippet,statistics"
.
How do I know which parts to use?
The best thing you can do is try each of them and see what data they return.
Now to write the Python code.. Our Hello World code snippet is a good "jumping off" point.
Steps
-
Import the
build()
function from thegoogleapiclient.discovery
module. -
Instantiate a
googleapiclient.discovery.Resource
object for the youtube service. You'll need to input your API key here. -
Define the request.
-
Execute the request.
-
The response is returned as a dictionary with nested data. We just have to traverse it to pick out the data elements we want.
Try It widget
YouTube's Data API has a handy "Try It" widget that you can use to generate code for running some of these queries.
OAuth
Note that the widget code uses OAuth with a client secret file. This is overkill for us. The API endpoints we're accessing only require an API key - not an OAuth token.