youtubeAPIspython
Ben Gorman

Ben Gorman

Life's a garden. Dig it.

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

from googleapiclient.discovery import build
 
# Instantiate a googleapiclient.discovery.Resource object for youtube
youtube = build(
  serviceName='youtube', 
  version='v3', 
  developerKey='YOURAPIKEY'
)
 
# Define the request
request = youtube.channels().list(
  part="snippet,statistics",
  id="UCX6OQ3DkcsbYNE6H8uQQuVA"
)
 
# Execute the request and save the response
response = request.execute()
 
# Close the connection
youtube.close()
 
# Fetch the first (and only) item
item = response['items'][0]
 
### Print stuff
print(f"Channel inception: {item['snippet']['publishedAt']}")
# Channel inception: 2012-02-20T00:43:50Z
 
print(f"Current subscribers: {item['statistics']['subscriberCount']}")
# Current subscribers: 117000000

Explanation

Since we want to fetch data about a YouTube channel it makes sense for us to inspect the channels.list resource.

channels.list

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.

channel id

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".

channels.list parameters

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

  1. Import the build() function from the googleapiclient.discovery module.

    from googleapiclient.discovery import build
  2. Instantiate a googleapiclient.discovery.Resource object for the youtube service. You'll need to input your API key here.

    youtube = build(
      serviceName='youtube', 
      version='v3', 
      developerKey='YOURAPIKEY'
    )
  3. Define the request.

    request = youtube.channels().list(
      part="snippet,statistics",
      id="UCX6OQ3DkcsbYNE6H8uQQuVA" # <- channel id
    )
  4. Execute the request.

    response = request.execute()
  5. The response is returned as a dictionary with nested data. We just have to traverse it to pick out the data elements we want.

    # Fetch the first (and only) item
    item = response['items'][0]
     
    ### Print stuff
    print(f"Channel inception: {item['snippet']['publishedAt']}")
    # Channel inception: 2012-02-20T00:43:50Z
     
    print(f"Current subscribers: {item['statistics']['subscriberCount']}")
    # Current subscribers: 117000000

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.

try it widget

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.