Using the YouTube API to track video or channel performance

Julian M. Dlugosch
19 min readNov 14, 2023

--

Ever wanted to dive into the world of YouTube analytics? Whether you’re exploring market segments or searching for a captivating data science weekend project, your journey starts here. Skip the initial hurdles — five years ago, I have written a Python script to gather YouTube data, and it’s been running seamlessly ever since. Now, I’m here to guide you through the process effortlessly. Set aside exhaustive API references; this is your hands-on, step-by-step guide to leveraging the YouTube Data API using Python.

Discover how to obtain a Google API key, make API calls, and extract valuable insights from video performance metrics. Join me as we uncover the top 20 cat videos on YouTube, revealing the channels behind them and quantifying views, likes, and comments. Let’s transform data into meaningful insights together!

Get a YouTube API Key

First and foremost, let’s secure an API key to enable queries to the YouTube API. Each API key comes with a free quota, generally sufficient for most types of usage. If you find yourself debugging extensively, especially during API searches, you might encounter quota limitations. In such cases, a simple solution is to wait for 24 hours, and the quota will reset automatically. Have a look here if you want to learn more about API quota.

Now, how do you obtain a YouTube API Key? Follow these steps:

  1. Navigate to console.cloud.google.com and log in using your Google credentials.
  2. Create a new project to which the API key will be assigned. To do this, select ‘Select a project’ in the top navigation bar, and then click on ‘New Project.’”

Provide your project with a descriptive name and link it to a billing account (create one if needed). It’s important to note that as long as you don’t actively expand your quota, there will be no associated costs with using only the API.

On the page that appears, enter ‘YouTube’ into the search bar located in the top navigation, and choose “YouTube Data API v3” from the results. Once selected, click on “Enable.”

Navigate to the next screen and click on “Credentials,” followed by selecting “Create credentials,” then choose “API Key.” Allow a few moments for the system to generate the API key.

Save the generated key locally, ensuring it’s stored securely. When utilizing the key in the future, exercise caution. If you plan to share your code, it’s crucial never to expose the API key in clear text, as this could be exploited by anyone to make API calls on your behalf. A recommended practice is to load the key from a .txt file that is not included in your repository, or store it securely (e.g. using the Secret Manager, if your code runs on Google Cloud Platform).

With these precautions in place, you now have everything you need to kick-start your project.

YouTube Data API Usage: General

To provide a brief overview: the YouTube Data API encompasses a resource representation for various entities, including video, search, or channel. The list() function essentially mirrors the functionality of a GET request in the API and retrieves the specified resource(s) based on the parameters provided within list(). It's important to note that each type of resource has its own set of parameters.

If the preceding paragraph seems a bit complex, don’t worry — everything will become much clearer as we delve into retrieving video and search data. So let’s get started!

YouTube Data API Usage: Video

You are now ready to make calls to the YouTube Data API using a Python script to retrieve data for a specific YouTube video. Videos on YouTube are identified by their unique YouTubeID, which is found in the video’s URL. If you open any YouTube video, you’ll notice that its URL contains the ID, and sometimes a timestamp indicating when the video starts. For example:

https://www.youtube.com/watch?v=ZqlTSCvP-Z0&t=15s

In this URL, ZqlTSCvP-Z0 is the YouTubeID, and 15s is the timestamp.

Now, let’s get started on a minimal example of fetching the data from the YouTube Data API. First, install the google-api-python-client package. Then, we'll begin by setting up a client

from googleapiclient.discovery import build

DEVELOPER_KEY = # you API key goes here
YOUTUBE_API_SERVICE_NAME = 'youtube'
YOUTUBE_API_VERSION = 'v3'

# Set up a client
client = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION,
developerKey=DEVELOPER_KEY)

Now, with our configured client, we can proceed to request data from a specific YouTube video. To achieve this, we need to specify the data, also known as ‘parts,’ that we want, along with the ID of the video we are interested in.

response = client.videos().list(
part='id,'
'snippet,'
'statistics',
id='ZqLTSCvP-Z0',
).execute()

print(response)

Notice that the parts are separated by commas, but for better readability in our code, we have placed them on different lines.

Each part corresponds to a distinct property of the video, and it may contain multiple child properties. If we specify a property with child properties, such as ‘snippet’ or ‘statistics,’ the API response will include all of those properties. Have a look at the official documentation, if you want to learn more about the parameters of the video().list() function.

For now, let’s explore what we receive in response to the above API call:

{'kind': 'youtube#videoListResponse', 'etag': 'Th5FT4yDlfWx7n9_iMnk3DisriI', 'items': [{'kind': 'youtube#video', 'etag': '9yF5v2dWKXtwV2yoG6p9V06lyAw', 'id': 'ZqlTSCvP-Z0', 'snippet': {'publishedAt': '2019-01-17T17:56:07Z', 'channelId': 'UCsT0YIqwnpJCM-mx7-gSA4Q', 'title': 'How drawing helps you think | Ralph Ammer | TEDxTUM', 'description': "You don't have to be an artist to draw! In this beautifully illustrated talk, Ralph Ammer shows how drawing your thoughts can be a powerful tool for improving your thinking, creativity and communication. He wants you to believe in your drawing abilities, and provides numerous exercises to help you get started.\n\n Ralph Ammer is a professor at the Munich University of Applied Sciences and teaches biophilic design, which aims to create life-friendly objects, images, and services based on nature. Ralph believes in the diversity of 21st century craftsmen, regardless of whether they produce well-written programming code, carefully crafted prints, or the occasional ceramic vessel. This talk was given at a TEDx event using the TED conference format but independently organized by a local community. Learn more at https://www.ted.com/tedx", 'thumbnails': {'default': {'url': 'https://i.ytimg.com/vi/ZqlTSCvP-Z0/default.jpg', 'width': 120, 'height': 90}, 'medium': {'url': 'https://i.ytimg.com/vi/ZqlTSCvP-Z0/mqdefault.jpg', 'width': 320, 'height': 180}, 'high': {'url': 'https://i.ytimg.com/vi/ZqlTSCvP-Z0/hqdefault.jpg', 'width': 480, 'height': 360}, 'standard': {'url': 'https://i.ytimg.com/vi/ZqlTSCvP-Z0/sddefault.jpg', 'width': 640, 'height': 480}, 'maxres': {'url': 'https://i.ytimg.com/vi/ZqlTSCvP-Z0/maxresdefault.jpg', 'width': 1280, 'height': 720}}, 'channelTitle': 'TEDx Talks', 'tags': ['TEDxTalks', 'English', 'Art', 'Arts education', 'Beauty', 'Communication', 'Creation', 'Creativity', 'Design', 'Ideas', 'Learning', 'Painting', 'Personal growth', 'Visualization'], 'categoryId': '29', 'liveBroadcastContent': 'none', 'localized': {'title': 'How drawing helps you think | Ralph Ammer | TEDxTUM', 'description': "You don't have to be an artist to draw! In this beautifully illustrated talk, Ralph Ammer shows how drawing your thoughts can be a powerful tool for improving your thinking, creativity and communication. He wants you to believe in your drawing abilities, and provides numerous exercises to help you get started.\n\n Ralph Ammer is a professor at the Munich University of Applied Sciences and teaches biophilic design, which aims to create life-friendly objects, images, and services based on nature. Ralph believes in the diversity of 21st century craftsmen, regardless of whether they produce well-written programming code, carefully crafted prints, or the occasional ceramic vessel. This talk was given at a TEDx event using the TED conference format but independently organized by a local community. Learn more at https://www.ted.com/tedx"}, 'defaultAudioLanguage': 'en'}, 'statistics': {'viewCount': '780003', 'likeCount': '28106', 'favoriteCount': '0', 'commentCount': '507'}}], 'pageInfo': {'totalResults': 1, 'resultsPerPage': 1}}So where are all the stats? They are ‘hidden’ inside the ‘items’. Let’s have a look:

It's crucial to grasp that, despite our request being for the result of only one video, the 'snippet' is structured as a list of videos. To extract the data from 'snippet' or 'statistics,' we must first access this list structure.

From the code:

print(response.get('items')[0]['snippet'])

we obtain the following response, a dictionary containing the snippet data for our video:

{'publishedAt': '2019-01-17T17:56:07Z', 'channelId': 'UCsT0YIqwnpJCM-mx7-gSA4Q', 'title': 'How drawing helps you think | Ralph Ammer | TEDxTUM', 'description': "You don't have to be an artist to draw! In this beautifully illustrated talk, Ralph Ammer shows how drawing your thoughts can be a powerful tool for improving your thinking, creativity and communication. He wants you to believe in your drawing abilities, and provides numerous exercises to help you get started.\n\n Ralph Ammer is a professor at the Munich University of Applied Sciences and teaches biophilic design, which aims to create life-friendly objects, images, and services based on nature. Ralph believes in the diversity of 21st century craftsmen, regardless of whether they produce well-written programming code, carefully crafted prints, or the occasional ceramic vessel. This talk was given at a TEDx event using the TED conference format but independently organized by a local community. Learn more at https://www.ted.com/tedx", 'thumbnails': {'default': {'url': 'https://i.ytimg.com/vi/ZqlTSCvP-Z0/default.jpg', 'width': 120, 'height': 90}, 'medium': {'url': 'https://i.ytimg.com/vi/ZqlTSCvP-Z0/mqdefault.jpg', 'width': 320, 'height': 180}, 'high': {'url': 'https://i.ytimg.com/vi/ZqlTSCvP-Z0/hqdefault.jpg', 'width': 480, 'height': 360}, 'standard': {'url': 'https://i.ytimg.com/vi/ZqlTSCvP-Z0/sddefault.jpg', 'width': 640, 'height': 480}, 'maxres': {'url': 'https://i.ytimg.com/vi/ZqlTSCvP-Z0/maxresdefault.jpg', 'width': 1280, 'height': 720}}, 'channelTitle': 'TEDx Talks', 'tags': ['TEDxTalks', 'English', 'Art', 'Arts education', 'Beauty', 'Communication', 'Creation', 'Creativity', 'Design', 'Ideas', 'Learning', 'Painting', 'Personal growth', 'Visualization'], 'categoryId': '29', 'liveBroadcastContent': 'none', 'localized': {'title': 'How drawing helps you think | Ralph Ammer | TEDxTUM', 'description': "You don't have to be an artist to draw! In this beautifully illustrated talk, Ralph Ammer shows how drawing your thoughts can be a powerful tool for improving your thinking, creativity and communication. He wants you to believe in your drawing abilities, and provides numerous exercises to help you get started.\n\n Ralph Ammer is a professor at the Munich University of Applied Sciences and teaches biophilic design, which aims to create life-friendly objects, images, and services based on nature. Ralph believes in the diversity of 21st century craftsmen, regardless of whether they produce well-written programming code, carefully crafted prints, or the occasional ceramic vessel. This talk was given at a TEDx event using the TED conference format but independently organized by a local community. Learn more at https://www.ted.com/tedx"}, 'defaultAudioLanguage': 'en'}

Similarly, we can access statistics data, such as ‘viewCount,’ using the following code:

stats_dict = response.get('items')[0]['statistics']
print(stats_dict['viewCount'])

I encourage you to explore and print various properties from the API response. Don’t worry about your quota; you have 10,000 of these calls per day, and manual testing is unlikely to reach that limit.

YouTube Data API usage: Search

Indeed, manually creating a list of YouTube IDs can be cumbersome, so a more elegant solution is to leverage the API to search for videos. This approach not only simplifies tracking but also allows you to monitor all videos from a particular YouTube channel without the need for manual checks.

The Python code for searching videos is quite similar to retrieving data for specific videos. We utilize the previously set-up client to configure the search parameters and then execute the search. In this example, let’s search for ‘surfing’:

search_response = client.search().list(
q='surfing',
maxResults=25,
part='id,snippet',
type='video',
channelId='UCsT0YIqwnpJCM-mx7-gSA4Q',
pageToken=token,
order='viewCount'
).execute()

Let’s break down what’s happening in the code:

  • q defines the search term we are looking for.
  • maxResults sets the maximum number of results in the search response, capped at 50.
  • part is a parameter we're familiar with from video().list(). However, it's worth noting that statistics are not available for search results.
  • type specifies the type of search response. It can be 'video,' 'channel,' or 'playlist.' This is crucial as their IDs will be different (videoId, channelId, or playlistId). If you encounter an error stating that videoId does not exist, it's likely due to non-video type responses in your search.
  • Within search().list(), channelId allows us to specify a channel to be searched. While finding a channel ID manually might be a bit more challenging than a video ID, there are convenient methods to obtain it.
  • If you wish to analyze more than 50 search results, pageToken becomes relevant. Each search page receives a unique identifier—the pageToken. Each call to search().list() provides the pageToken associated with that particular search page in the response. We can access it via:
token = search_response.get('nextPageToken', None)
  • Finally, you can influence how the results are ordered using the order parameter, which can take values like 'relevance' (the default if not specified), 'viewCount,' or 'date.'

For additional options, please consult the official documentaion.

A response from using the search function via the API may look something like this:

{'kind': 'youtube#searchListResponse', 'etag': 'PZ1oJYvtSBiJazxweK3o_TiBcPo', 'nextPageToken': 'CBkQAA', 'regionCode': 'DE', 'pageInfo': {'totalResults': 1000000, 'resultsPerPage': 25}, 'items': [{'kind': 'youtube#searchResult', 'etag': 'J3OHQbtoRoh0yhuf4NBSfbnRpHQ', 'id': {'kind': 'youtube#video', 'videoId': 'kJO4RT9WTIo'}, 'snippet': {'publishedAt': '2023-11-11T03:59:20Z', 'channelId': 'UCn7s1bTIbscwY0w65U7WFAw', 'title': 'Surfing. Kelly Slater, JJ Florence, Mick Fanning, Parko & Co! A Grade Surfers - A Grade Waves!', 'description': 'The Gold Coast Points were pumping during Autumn 2017 with the worlds best surfing along with the locals. Music: Bigfellalinc.', 'thumbnails': {'default': {'url': 'https://i.ytimg.com/vi/kJO4RT9WTIo/default.jpg', 'width': 120, 'height': 90}, 'medium': {'url': 'https://i.ytimg.com/vi/kJO4RT9WTIo/mqdefault.jpg', 'width': 320, 'height': 180}, 'high': {'url': 'https://i.ytimg.com/vi/kJO4RT9WTIo/hqdefault.jpg', 'width': 480, 'height': 360}}, 'channelTitle': 'surfonmars', 'liveBroadcastContent': 'none', 'publishTime': '2023-11-11T03:59:20Z'}}, {'kind': 'youtube#searchResult', 'etag': 'NApOA8EzcK1pYZUCqtchIG2nTjw', 'id': {'kind': 'youtube#video', 'videoId': 'ma67yOdMQfs'}, 'snippet': {'publishedAt': '2021-01-23T17:00:15Z', 'channelId': 'UC--3c8RqSfAqYBdDjIG3UNA', 'title': 'These Were The All-Time Surfing Moments Of The Year | Best Of 2020', 'description': "Well, that was a weird ride. Though it hasn't been easy, at least when we fixed our gaze on the ocean - or favorite place in the ...", 'thumbnails': {'default': {'url': 'https://i.ytimg.com/vi/ma67yOdMQfs/default.jpg', 'width': 120, 'height': 90}, 'medium': {'url': 'https://i.ytimg.com/vi/ma67yOdMQfs/mqdefault.jpg', 'width': 320, 'height': 180}, 'high': {'url': 'https://i.ytimg.com/vi/ma67yOdMQfs/hqdefault.jpg', 'width': 480, 'height': 360}}, 'channelTitle': 'Red Bull Surfing', 'liveBroadcastContent': 'none', 'publishTime': '2021-01-23T17:00:15Z'}}, {'kind': 'youtube#searchResult', 'etag': '7jH7r6i1sf20uhuWUUnUr7b0FGQ', 'id': {'kind': 'youtube#video', 'videoId': 'xtHZKToMvus'}, 'snippet': {'publishedAt': '2022-11-02T17:46:49Z', 'channelId': 'UCTYHNSWYy4jCSCj1Q1Fq0ew', 'title': 'Interlusion | A Billabong Surf Film Shot in the Mentawai Islands', 'description': "Welcome to the Interlusion. What happens when the best surf trip of former world champ Joel Parkinson's life happens years after ...", 'thumbnails': {'default': {'url': 'https://i.ytimg.com/vi/xtHZKToMvus/default.jpg', 'width': 120, 'height': 90}, 'medium': {'url': 'https://i.ytimg.com/vi/xtHZKToMvus/mqdefault.jpg', 'width': 320, 'height': 180}, 'high': {'url': 'https://i.ytimg.com/vi/xtHZKToMvus/hqdefault.jpg', 'width': 480, 'height': 360}}, 'channelTitle': 'Billabong', 'liveBroadcastContent': 'none', 'publishTime': '2022-11-02T17:46:49Z'}}, {'kind': 'youtube#searchResult', 'etag': 'PNWsfE9WO3BbSqZXHxxChMrrDic', 'id': {'kind': 'youtube#video', 'videoId': 'eSWST13stO4'}, 'snippet': {'publishedAt': '2022-03-27T15:00:32Z', 'channelId': 'UCVo06dBGK9VBBhq15wJxZHQ', 'title': '🔵4k (ASMR) 10 Hour Store Loop - Hawaii Surfing - With Relaxing Music☑️', 'description': 'Waves of the World Filmers: Chris Kincade: https://www.instagram.com/chriskincade.wotw Andre Botha: ...', 'thumbnails': {'default': {'url': 'https://i.ytimg.com/vi/eSWST13stO4/default.jpg', 'width': 120, 'height': 90}, 'medium': {'url': 'https://i.ytimg.com/vi/eSWST13stO4/mqdefault.jpg', 'width': 320, 'height': 180}, 'high': {'url': 'https://i.ytimg.com/vi/eSWST13stO4/hqdefault.jpg', 'width': 480, 'height': 360}}, 'channelTitle': 'Waves of the World', 'liveBroadcastContent': 'none', 'publishTime': '2022-03-27T15:00:32Z'}}, {'kind': 'youtube#searchResult', 'etag': 'N4sIMI5TRDmkdX1G0-zddgopw3M', 'id': {'kind': 'youtube#video', 'videoId': 'xbFNjMTlvz0'}, 'snippet': {'publishedAt': '2023-11-12T15:30:21Z', 'channelId': 'UCzmxGGgSDALb59uiMFyt7AQ', 'title': 'Giant Waves This 11 November, Hossegor !! The MOST Big SURFING', 'description': "Découvrez l'incroyable journée de surf du 11 novembre à Hossegor Une session épique avec des vagues énormes sur le spot de ...", 'thumbnails': {'default': {'url': 'https://i.ytimg.com/vi/xbFNjMTlvz0/default.jpg', 'width': 120, 'height': 90}, 'medium': {'url': 'https://i.ytimg.com/vi/xbFNjMTlvz0/mqdefault.jpg', 'width': 320, 'height': 180}, 'high': {'url': 'https://i.ytimg.com/vi/xbFNjMTlvz0/hqdefault.jpg', 'width': 480, 'height': 360}}, 'channelTitle': 'SURF FRANCE HOSSEGOR-CAPBRETON', 'liveBroadcastContent': 'none', 'publishTime': '2023-11-12T15:30:21Z'}}, {'kind': 'youtube#searchResult', 'etag': 'wzo67Yh0HmkGc5sS2py3LKUQMYM', 'id': {'kind': 'youtube#video', 'videoId': 'hpoY-T6ys2I'}, 'snippet': {'publishedAt': '2023-11-11T19:30:03Z', 'channelId': 'UCLdPicN16eAKPKir8EY1UXQ', 'title': 'The beautiful beach break waves of France', 'description': "France's autumn has been one to remember. Literally the most perfect season ever with pristine banks, consistent waves and ...", 'thumbnails': {'default': {'url': 'https://i.ytimg.com/vi/hpoY-T6ys2I/default.jpg', 'width': 120, 'height': 90}, 'medium': {'url': 'https://i.ytimg.com/vi/hpoY-T6ys2I/mqdefault.jpg', 'width': 320, 'height': 180}, 'high': {'url': 'https://i.ytimg.com/vi/hpoY-T6ys2I/hqdefault.jpg', 'width': 480, 'height': 360}}, 'channelTitle': 'Kale Brock', 'liveBroadcastContent': 'none', 'publishTime': '2023-11-11T19:30:03Z'}}, {'kind': 'youtube#searchResult', 'etag': 'JzkzP9aVj7oUx0VWuPclJVTasH8', 'id': {'kind': 'youtube#video', 'videoId': 'FPJ_G0OxNso'}, 'snippet': {'publishedAt': '2023-11-01T06:30:04Z', 'channelId': 'UCoicMEw3njXscfwHlaAJVcQ', 'title': 'SURFING UP THE COAST IN MAGICAL WATER! (SURF VLOG)', 'description': "I went up north again in my van! Here's some POV and beach footage from the first day Video #2 is dropping later this week!", 'thumbnails': {'default': {'url': 'https://i.ytimg.com/vi/FPJ_G0OxNso/default.jpg', 'width': 120, 'height': 90}, 'medium': {'url': 'https://i.ytimg.com/vi/FPJ_G0OxNso/mqdefault.jpg', 'width': 320, 'height': 180}, 'high': {'url': 'https://i.ytimg.com/vi/FPJ_G0OxNso/hqdefault.jpg', 'width': 480, 'height': 360}}, 'channelTitle': 'Surfing With Noz', 'liveBroadcastContent': 'none', 'publishTime': '2023-11-01T06:30:04Z'}}, {'kind': 'youtube#searchResult', 'etag': '0caJTZMXcfPOTXKiqyAs2Z_k4mU', 'id': {'kind': 'youtube#video', 'videoId': 'IHcEo09a5qk'}, 'snippet': {'publishedAt': '2023-11-10T22:18:16Z', 'channelId': 'UC1SE_GEw_w9c56Qn3M9Xxew', 'title': 'NAZARÉ GOES XL - FIRST BIG SWELL OF THE SEASON - epic drone view', 'description': 'First surfable XL swell meets Praia do Norte! Check out some of the bombs from today, November 10th. Enjoy :) NAZARÉ GOES ...', 'thumbnails': {'default': {'url': 'https://i.ytimg.com/vi/IHcEo09a5qk/default.jpg', 'width': 120, 'height': 90}, 'medium': {'url': 'https://i.ytimg.com/vi/IHcEo09a5qk/mqdefault.jpg', 'width': 320, 'height': 180}, 'high': {'url': 'https://i.ytimg.com/vi/IHcEo09a5qk/hqdefault.jpg', 'width': 480, 'height': 360}}, 'channelTitle': 'Johannes Hertel', 'liveBroadcastContent': 'none', 'publishTime': '2023-11-10T22:18:16Z'}}, {'kind': 'youtube#searchResult', 'etag': 'J9hnXcSak2gMsWMjpKfGjiEjaNM', 'id': {'kind': 'youtube#video', 'videoId': '2ERgmSIx9gk'}, 'snippet': {'publishedAt': '2023-11-06T19:00:01Z', 'channelId': 'UCo_q6aOlvPH7M-j_XGWVgXg', 'title': 'PIPELINE IS PUMPING (EARLY SEASON SWELLS HIT HAWAII)', 'description': "STAY PSYCHED MERCHANDISE: https://staypsyched.com - JAMIE O'BRIEN SURF SCHOOL: ...", 'thumbnails': {'default': {'url': 'https://i.ytimg.com/vi/2ERgmSIx9gk/default.jpg', 'width': 120, 'height': 90}, 'medium': {'url': 'https://i.ytimg.com/vi/2ERgmSIx9gk/mqdefault.jpg', 'width': 320, 'height': 180}, 'high': {'url': 'https://i.ytimg.com/vi/2ERgmSIx9gk/hqdefault.jpg', 'width': 480, 'height': 360}}, 'channelTitle': "Jamie O'Brien", 'liveBroadcastContent': 'none', 'publishTime': '2023-11-06T19:00:01Z'}}, {'kind': 'youtube#searchResult', 'etag': 'OxFfZryHRKDvLgLRaJOGodxYerc', 'id': {'kind': 'youtube#video', 'videoId': 'arnqEkJoWSw'}, 'snippet': {'publishedAt': '2023-11-12T11:00:02Z', 'channelId': 'UCzcQOTuXYGuCvTekySb_CeQ', 'title': 'How To End Your Day Happy  - Uluwatu', 'description': 'A persistent onshore wind showed no sign of backing down for the late afternoon session at Uluwatu last week, but it was still nice ...', 'thumbnails': {'default': {'url': 'https://i.ytimg.com/vi/arnqEkJoWSw/default.jpg', 'width': 120, 'height': 90}, 'medium': {'url': 'https://i.ytimg.com/vi/arnqEkJoWSw/mqdefault.jpg', 'width': 320, 'height': 180}, 'high': {'url': 'https://i.ytimg.com/vi/arnqEkJoWSw/hqdefault.jpg', 'width': 480, 'height': 360}}, 'channelTitle': 'Surfers of Bali', 'liveBroadcastContent': 'none', 'publishTime': '2023-11-12T11:00:02Z'}}, {'kind': 'youtube#searchResult', 'etag': 'QasAi7czWvz0flSptBnTQKcCPwo', 'id': {'kind': 'youtube#video', 'videoId': '6OcW4j5UBkU'}, 'snippet': {'publishedAt': '2023-11-10T07:52:12Z', 'channelId': 'UC3DIR1s_jw1wr4p4wWg7JGQ', 'title': 'SOLID SWELL HITS DESERT POINT, MOROCCO 🇲🇦 AFRICA', 'description': 'Morocco received its first major swell of the season and i was lucky to be around to film and surf it. Was my first surf trip to Africa ...', 'thumbnails': {'default': {'url': 'https://i.ytimg.com/vi/6OcW4j5UBkU/default.jpg', 'width': 120, 'height': 90}, 'medium': {'url': 'https://i.ytimg.com/vi/6OcW4j5UBkU/mqdefault.jpg', 'width': 320, 'height': 180}, 'high': {'url': 'https://i.ytimg.com/vi/6OcW4j5UBkU/hqdefault.jpg', 'width': 480, 'height': 360}}, 'channelTitle': 'Raw Surfing', 'liveBroadcastContent': 'none', 'publishTime': '2023-11-10T07:52:12Z'}}, {'kind': 'youtube#searchResult', 'etag': 'iYt9sKjxm3HZBarsmNb107W8_Nc', 'id': {'kind': 'youtube#video', 'videoId': 'a48-c7kCdYI'}, 'snippet': {'publishedAt': '2023-08-31T08:00:08Z', 'channelId': 'UCoicMEw3njXscfwHlaAJVcQ', 'title': 'SURFING A GLASSY REEF BREAK! (4K POV)', 'description': 'In this video, we went and surfed one of our favourite reef breaks! It got super glassy Then we went for a skate at the bowl For ...', 'thumbnails': {'default': {'url': 'https://i.ytimg.com/vi/a48-c7kCdYI/default.jpg', 'width': 120, 'height': 90}, 'medium': {'url': 'https://i.ytimg.com/vi/a48-c7kCdYI/mqdefault.jpg', 'width': 320, 'height': 180}, 'high': {'url': 'https://i.ytimg.com/vi/a48-c7kCdYI/hqdefault.jpg', 'width': 480, 'height': 360}}, 'channelTitle': 'Surfing With Noz', 'liveBroadcastContent': 'none', 'publishTime': '2023-08-31T08:00:08Z'}}, {'kind': 'youtube#searchResult', 'etag': 'UOiKWu3-LI1lcMw-TZytERs8ZCM', 'id': {'kind': 'youtube#video', 'videoId': 'dAIz7xe0Nx4'}, 'snippet': {'publishedAt': '2023-10-27T14:00:32Z', 'channelId': 'UCeYue9Nbodzg3T1Nt88E3fg', 'title': 'Mason Ho | A Week In October | North Shore', 'description': 'Mason Ho arrives home from France and gets straight into some early season North Shore waves. Hitting some of his favorite surf ...', 'thumbnails': {'default': {'url': 'https://i.ytimg.com/vi/dAIz7xe0Nx4/default.jpg', 'width': 120, 'height': 90}, 'medium': {'url': 'https://i.ytimg.com/vi/dAIz7xe0Nx4/mqdefault.jpg', 'width': 320, 'height': 180}, 'high': {'url': 'https://i.ytimg.com/vi/dAIz7xe0Nx4/hqdefault.jpg', 'width': 480, 'height': 360}}, 'channelTitle': 'Ho & Pringle Productions', 'liveBroadcastContent': 'none', 'publishTime': '2023-10-27T14:00:32Z'}}, {'kind': 'youtube#searchResult', 'etag': '0nLeikZw86q2aBIIVhaVMIUG0Ck', 'id': {'kind': 'youtube#video', 'videoId': '0nx5z7JJO5c'}, 'snippet': {'publishedAt': '2023-11-10T14:00:01Z', 'channelId': 'UCeYue9Nbodzg3T1Nt88E3fg', 'title': 'Mason Ho Eating Rocks Surfing POV', 'description': 'Yesterday morning Mason Ho grabbed a new GoPro HERO 12 and surfed his beloved garden of rocks. Maybe having a little too ...', 'thumbnails': {'default': {'url': 'https://i.ytimg.com/vi/0nx5z7JJO5c/default.jpg', 'width': 120, 'height': 90}, 'medium': {'url': 'https://i.ytimg.com/vi/0nx5z7JJO5c/mqdefault.jpg', 'width': 320, 'height': 180}, 'high': {'url': 'https://i.ytimg.com/vi/0nx5z7JJO5c/hqdefault.jpg', 'width': 480, 'height': 360}}, 'channelTitle': 'Ho & Pringle Productions', 'liveBroadcastContent': 'none', 'publishTime': '2023-11-10T14:00:01Z'}}, {'kind': 'youtube#searchResult', 'etag': 'qjkKosWTy0lNlCbVF3Yh9DSiNmw', 'id': {'kind': 'youtube#channel', 'channelId': 'UCOtHosOqPe9d6vLy-8LfHzQ'}, 'snippet': {'publishedAt': '2015-02-17T03:47:32Z', 'channelId': 'UCOtHosOqPe9d6vLy-8LfHzQ', 'title': 'NobodySurf : Surfing Videos', 'description': 'Surf videos everyday, worldwide. Surf videos make us happy, no matter who we are, or where we are. We love them. The creators ...', 'thumbnails': {'default': {'url': 'https://yt3.ggpht.com/ytc/APkrFKZs1-mdT4vOFeg90UyBIRg5jrSduNe4kRUNg9H3HQ=s88-c-k-c0xffffffff-no-rj-mo'}, 'medium': {'url': 'https://yt3.ggpht.com/ytc/APkrFKZs1-mdT4vOFeg90UyBIRg5jrSduNe4kRUNg9H3HQ=s240-c-k-c0xffffffff-no-rj-mo'}, 'high': {'url': 'https://yt3.ggpht.com/ytc/APkrFKZs1-mdT4vOFeg90UyBIRg5jrSduNe4kRUNg9H3HQ=s800-c-k-c0xffffffff-no-rj-mo'}}, 'channelTitle': 'NobodySurf : Surfing Videos', 'liveBroadcastContent': 'none', 'publishTime': '2015-02-17T03:47:32Z'}}, {'kind': 'youtube#searchResult', 'etag': 'EVemgAjK00l3oRrYXKztNYYiJDw', 'id': {'kind': 'youtube#video', 'videoId': 'eFhLPPYkPo8'}, 'snippet': {'publishedAt': '2023-10-31T14:48:02Z', 'channelId': 'UC4qa5EroJxnQM6Sng9WhgsQ', 'title': 'Surfing Japan's Futuristic Wave Pool', 'description': 'It has begun! Thanks to @RedBullSurfing our 7 Seas in 7 Days project is under way! First stop is the artificial ocean of the Surf ...', 'thumbnails': {'default': {'url': 'https://i.ytimg.com/vi/eFhLPPYkPo8/default.jpg', 'width': 120, 'height': 90}, 'medium': {'url': 'https://i.ytimg.com/vi/eFhLPPYkPo8/mqdefault.jpg', 'width': 320, 'height': 180}, 'high': {'url': 'https://i.ytimg.com/vi/eFhLPPYkPo8/hqdefault.jpg', 'width': 480, 'height': 360}}, 'channelTitle': 'Ben Gravy', 'liveBroadcastContent': 'none', 'publishTime': '2023-10-31T14:48:02Z'}}, {'kind': 'youtube#searchResult', 'etag': 'TXqNeaEKtMA78rd8XLCXcLm1aQU', 'id': {'kind': 'youtube#video', 'videoId': 'zZqxPozl2Ec'}, 'snippet': {'publishedAt': '2023-01-24T07:00:09Z', 'channelId': 'UCEUYW6zm6KnUxPcwY1D8HsQ', 'title': 'Surfing Massive Waves Waimea Bay (Jan 22, 2023)  4K', 'description': "What occurred on January 22, 2023, at Waimea Bay on the North Shore of Oahu is nothing short of historical. We're simply doing ...", 'thumbnails': {'default': {'url': 'https://i.ytimg.com/vi/zZqxPozl2Ec/default.jpg', 'width': 120, 'height': 90}, 'medium': {'url': 'https://i.ytimg.com/vi/zZqxPozl2Ec/mqdefault.jpg', 'width': 320, 'height': 180}, 'high': {'url': 'https://i.ytimg.com/vi/zZqxPozl2Ec/hqdefault.jpg', 'width': 480, 'height': 360}}, 'channelTitle': 'Eimy's Hawaii Happy life 🏄🏻', 'liveBroadcastContent': 'none', 'publishTime': '2023-01-24T07:00:09Z'}}, {'kind': 'youtube#searchResult', 'etag': 'wlSFDwCHK-ouu7Io1lzwja_jVkU', 'id': {'kind': 'youtube#video', 'videoId': 'RNxsiIgsypU'}, 'snippet': {'publishedAt': '2023-11-12T09:27:02Z', 'channelId': 'UChuLeaTGRcfzo0UjL-2qSbQ', 'title': 'WATCH LIVE: Taiwan Open of Surfing - Final Day', 'description': 'Watch all the action LIVE from Jinzun Harbor, Taitung County, Taiwan. #WSL.', 'thumbnails': {'default': {'url': 'https://i.ytimg.com/vi/RNxsiIgsypU/default.jpg', 'width': 120, 'height': 90}, 'medium': {'url': 'https://i.ytimg.com/vi/RNxsiIgsypU/mqdefault.jpg', 'width': 320, 'height': 180}, 'high': {'url': 'https://i.ytimg.com/vi/RNxsiIgsypU/hqdefault.jpg', 'width': 480, 'height': 360}}, 'channelTitle': 'World Surf League', 'liveBroadcastContent': 'none', 'publishTime': '2023-11-12T09:27:02Z'}}, {'kind': 'youtube#searchResult', 'etag': '643fc1b5CIkDTJwTo_RenccqcQA', 'id': {'kind': 'youtube#video', 'videoId': 'hCMdVywETkU'}, 'snippet': {'publishedAt': '2023-08-21T19:00:19Z', 'channelId': 'UCo_q6aOlvPH7M-j_XGWVgXg', 'title': 'SURFING SKELETON BAY WITH KELLY SLATER & EVERY PRO SURFER ON THE PLANET OF EARTH!', 'description': "STAY PSYCHED MERCHANDISE: https://staypsyched.com - JAMIE O'BRIEN SURF SCHOOL: ...", 'thumbnails': {'default': {'url': 'https://i.ytimg.com/vi/hCMdVywETkU/default.jpg', 'width': 120, 'height': 90}, 'medium': {'url': 'https://i.ytimg.com/vi/hCMdVywETkU/mqdefault.jpg', 'width': 320, 'height': 180}, 'high': {'url': 'https://i.ytimg.com/vi/hCMdVywETkU/hqdefault.jpg', 'width': 480, 'height': 360}}, 'channelTitle': "Jamie O'Brien", 'liveBroadcastContent': 'none', 'publishTime': '2023-08-21T19:00:19Z'}}, {'kind': 'youtube#searchResult', 'etag': 'VHJX6lDqz8Nnszgcw3ZZeZvgFwg', 'id': {'kind': 'youtube#video', 'videoId': 'RmW9kugujus'}, 'snippet': {'publishedAt': '2023-10-30T11:23:21Z', 'channelId': 'UCrL4HtiKWddH-9jkMXgudEA', 'title': 'Winners of the 2023 New Big Wave Challenge Awards', 'description': 'Justine Dupont was recognized with premier accolades at the debut New Big Wave Challenge awards in Nazaré, Portugal.', 'thumbnails': {'default': {'url': 'https://i.ytimg.com/vi/RmW9kugujus/default.jpg', 'width': 120, 'height': 90}, 'medium': {'url': 'https://i.ytimg.com/vi/RmW9kugujus/mqdefault.jpg', 'width': 320, 'height': 180}, 'high': {'url': 'https://i.ytimg.com/vi/RmW9kugujus/hqdefault.jpg', 'width': 480, 'height': 360}}, 'channelTitle': 'SurferToday', 'liveBroadcastContent': 'none', 'publishTime': '2023-10-30T11:23:21Z'}}, {'kind': 'youtube#searchResult', 'etag': 'onLqoIWd3obBMDJr8s7icp313h4', 'id': {'kind': 'youtube#video', 'videoId': '8pmO46pMtwM'}, 'snippet': {'publishedAt': '2023-10-26T11:00:06Z', 'channelId': 'UCzcQOTuXYGuCvTekySb_CeQ', 'title': 'September Long Edition - Bali 2023', 'description': 'September delivered plenty of medium to solid swells with enough SE trades to keep Uluwatu clean and organized. Canggu had ...', 'thumbnails': {'default': {'url': 'https://i.ytimg.com/vi/8pmO46pMtwM/default.jpg', 'width': 120, 'height': 90}, 'medium': {'url': 'https://i.ytimg.com/vi/8pmO46pMtwM/mqdefault.jpg', 'width': 320, 'height': 180}, 'high': {'url': 'https://i.ytimg.com/vi/8pmO46pMtwM/hqdefault.jpg', 'width': 480, 'height': 360}}, 'channelTitle': 'Surfers of Bali', 'liveBroadcastContent': 'none', 'publishTime': '2023-10-26T11:00:06Z'}}, {'kind': 'youtube#searchResult', 'etag': 'cneL0zPkyef8042OmLdF2CL1J0k', 'id': {'kind': 'youtube#video', 'videoId': 'sM6tDiOsSOo'}, 'snippet': {'publishedAt': '2023-06-23T18:00:32Z', 'channelId': 'UC--3c8RqSfAqYBdDjIG3UNA', 'title': 'KAI LENNY Surfs in Final Round of THE EDDIE!', 'description': 'Kai Lenny chases his big wave dream of a title at The Eddie Aikau Big Wave Invitational before switching gears to compete ...', 'thumbnails': {'default': {'url': 'https://i.ytimg.com/vi/sM6tDiOsSOo/default.jpg', 'width': 120, 'height': 90}, 'medium': {'url': 'https://i.ytimg.com/vi/sM6tDiOsSOo/mqdefault.jpg', 'width': 320, 'height': 180}, 'high': {'url': 'https://i.ytimg.com/vi/sM6tDiOsSOo/hqdefault.jpg', 'width': 480, 'height': 360}}, 'channelTitle': 'Red Bull Surfing', 'liveBroadcastContent': 'none', 'publishTime': '2023-06-23T18:00:32Z'}}, {'kind': 'youtube#searchResult', 'etag': '5t4RdcaIDZTfZaUOMdEuQsmWMSs', 'id': {'kind': 'youtube#video', 'videoId': 'pmlzdeZuAk8'}, 'snippet': {'publishedAt': '2021-02-27T14:28:13Z', 'channelId': 'UCxkIzPnPzWLz4IeuxIROflg', 'title': 'What Surfing Is ACTUALLY Like as a Beginner', 'description': "See what it's actually like to surf as a beginner, including why it's fun and why it kind of sucks at times! Click below to join the ...", 'thumbnails': {'default': {'url': 'https://i.ytimg.com/vi/pmlzdeZuAk8/default.jpg', 'width': 120, 'height': 90}, 'medium': {'url': 'https://i.ytimg.com/vi/pmlzdeZuAk8/mqdefault.jpg', 'width': 320, 'height': 180}, 'high': {'url': 'https://i.ytimg.com/vi/pmlzdeZuAk8/hqdefault.jpg', 'width': 480, 'height': 360}}, 'channelTitle': 'Travis Marziani', 'liveBroadcastContent': 'none', 'publishTime': '2021-02-27T14:28:13Z'}}, {'kind': 'youtube#searchResult', 'etag': 's5LxJxZn5UyHKKfk3r6znFU-OAE', 'id': {'kind': 'youtube#video', 'videoId': '8htT0Gr4qIo'}, 'snippet': {'publishedAt': '2023-07-15T13:13:43Z', 'channelId': 'UCzSnPzPi0EaKUqIApOrY3AQ', 'title': 'Glassy Shorebreaks at Brighton (POV SURF)', 'description': 'Glassy conditions at Brighton beach, no shoulder in sight but fun none the less #perth #povsurfing #scarborough #beach #gopro ...', 'thumbnails': {'default': {'url': 'https://i.ytimg.com/vi/8htT0Gr4qIo/default.jpg', 'width': 120, 'height': 90}, 'medium': {'url': 'https://i.ytimg.com/vi/8htT0Gr4qIo/mqdefault.jpg', 'width': 320, 'height': 180}, 'high': {'url': 'https://i.ytimg.com/vi/8htT0Gr4qIo/hqdefault.jpg', 'width': 480, 'height': 360}}, 'channelTitle': 'Tom Begley', 'liveBroadcastContent': 'none', 'publishTime': '2023-07-15T13:13:43Z'}}, {'kind': 'youtube#searchResult', 'etag': 'xD0v5qpSugEq61FKSHThrothDsQ', 'id': {'kind': 'youtube#video', 'videoId': 'VjBh06d7Cnc'}, 'snippet': {'publishedAt': '2023-11-04T00:38:21Z', 'channelId': 'UCgr_sR3fcLqRfXQPMd1hcjg', 'title': 'SURFING THE MAGIC WAVE KINGDOM! riding STORM SWELL in Cornwall', 'description': "We'd had 2 days of storms and then the wind dropped and went South Westerly. Day 1 I surfed the bay, it was big and most of the ...", 'thumbnails': {'default': {'url': 'https://i.ytimg.com/vi/VjBh06d7Cnc/default.jpg', 'width': 120, 'height': 90}, 'medium': {'url': 'https://i.ytimg.com/vi/VjBh06d7Cnc/mqdefault.jpg', 'width': 320, 'height': 180}, 'high': {'url': 'https://i.ytimg.com/vi/VjBh06d7Cnc/hqdefault.jpg', 'width': 480, 'height': 360}}, 'channelTitle': 'Guy Leverton', 'liveBroadcastContent': 'none', 'publishTime': '2023-11-04T00:38:21Z'}}]}

Putting it all together

Now that we’ve covered how to:

  • Search for videos and retrieve the videoIDs and channelTitle s from the search results.
  • Get video statistics based on videoIDs.

Let’s combine these steps to retrieve the top 20 cat videos on YouTube and store their statistics (views, likes, comments), along with their title and channel, in a Pandas DataFrame. This DataFrame can then be exported to a *.csv file for display on a webpage.

To begin, let’s initiate the process by searching for cat videos and sorting them by viewCount:

search_response = client.search().list(
q='cat',
maxResults=20,
part='id,snippet',
type='video',
order='viewCount'
).execute()

Get videoID , title and channelTitle from the search response and save them to lists

videos = search_response.get('items', [])

titles = []
channels = []
ids = []

for video in videos:
ids.append(video['id']['videoId'])
channels.append(video['snippet']['channelTitle'])
titles.append(video['snippet']['title'])

Now we call the API again for all IDs and extract views, comments and likes from the response to lists, that exist outside the loop:

views = []
comments = []
likes = []

for id in ids:
response = client.videos().list(
part='id,'
'snippet,'
'statistics',
id=id,
).execute()

for result in response.get('items', []):
stats_dict = result['statistics']
print(result)
views.append(stats_dict.get('viewCount'))
comments.append(stats_dict.get('commentCount'))
likes.append(stats_dict.get('likeCount'))

finally, we build a dictionary from the lists that contain the stats and turn it into a dataframe:

keys = ['title', 'channel', 'ID', 'views', 'comments', 'likes']
values = [titles,channels, ids, views, comments, likes]
d = dict(zip(keys,values))
df = pd.DataFrame(d)

In case you are curious — here are the results (as of November 14, 2023):

Congratulations! You’ve just mastered the art of utilizing the YouTube Data API to extract meaningful insights. Now, let’s explore some exciting follow-up projects:

  1. Top 10 Channels in a Niche and Region: Discover the most-viewed channels within a specific niche (e.g., outdoor, science communication) and region. Analyze their video release cycle, typical video length, and the variance in video views per channel.
  2. Comment Analysis: Identify the most active commenters on a particular channel or around a specific topic. Dive into their commenting habits, including the length of comments and the average time it takes them to comment after a video’s release.
  3. Sentiment Analysis: Perform sentiment analysis on all comments for a specific channel or the top-performing videos related to a search term. Rank the videos based on a metric related to positive comments, such as the number of positive comments.
  4. Channel Statistics Over Time: Track the evolving statistics (views, likes, comments, release cycle) of a fixed number of channels over time. Gain insights into how these metrics change and evolve.
  5. Daily New Video Tracker: Monitor the daily release of new videos for a specific search term, within a particular region, or from specific channels. Stay informed about the latest content trends (almost) in real-time.

These follow-up projects will not only deepen your understanding of YouTube analytics but also provide valuable insights into user behavior, content trends, and community engagement. If you chose to do so — feel free to drop a link to your project in the comments below. Happy exploring!

--

--