Installation

It is recommended to install this module by using pip:

pip install youtube-data-api

Quickstart

import os
import pandas as pd
from youtube_api import YoutubeDataApi

In order to access the API, you’ll need to get a service key from the Google Cloud Console . I like to store my API keys as environment variables in ~/.bash_profile so that I don’t have to hard code them.:

YT_KEY = os.environ.get('YOUTUBE_API_KEY') # you can hardcode this, too.
yt = YouTubeDataAPI(YT_KEY)

We now have created a YouTubeDataAPI class as yt, which can be used to make API calls, such as searching for the most relevant videos of Alexandria Ocasio-Cortez.

searches = yt.search(q='alexandria ocasio-cortez',
                     max_results=5)
print(searches[0])
{'video_id': 'LlillsHgcaw',
 'channel_title': 'Fox News',
 'channel_id': 'UCXIJgqnII2ZOINSWNOGFThA',
 'video_publish_date': datetime.datetime(2019, 2, 19, 4, 57, 51),
 'video_title': 'Rep. Alexandria Ocasio-Cortez taken to task by fellow progressives',
 'video_description': 'New York City Mayor Bill de Blasio criticizes Alexandria Ocasio-Cortez over her opposition to the Amazon deal.',
 'video_category': None,
 'video_thumbnail': 'https://i.ytimg.com/vi/LlillsHgcaw/hqdefault.jpg',
 'collection_date': datetime.datetime(2019, 2, 20, 14, 48, 19, 487877)}

All API requests are parsed from raw JSON into dictionaries.

Typically an API call returns a list of dictionary objects. This is perfect for converting into Pandas DataFrames, or saving as JSON.

df_search = pd.DataFrame(searches)
df_search.head(5)
channel_id channel_title collection_date video_category video_description video_id video_publish_date video_thumbnail video_title
0 UCXIJgqnII2ZOINSWNOGFThA Fox News 2019-02-20 14:49:34.262643 None New York City Mayor Bill de Blasio criticizes ... LlillsHgcaw 2019-02-19 04:57:51 https://i.ytimg.com/vi/LlillsHgcaw/hqdefault.jpg Rep. Alexandria Ocasio-Cortez taken to task by...
1 UCXIJgqnII2ZOINSWNOGFThA Fox News 2019-02-20 14:49:34.262672 None Alexandria Ocasio-Cortez's new environmental m... 3EazY4bw6u8 2019-02-19 00:34:22 https://i.ytimg.com/vi/3EazY4bw6u8/hqdefault.jpg Critics mock Ocasio-Cortez's Green New Deal ro...
2 UCeY0bbntWzzVIaj2z3QigXg NBC News 2019-02-20 14:49:34.262693 None Newly-elected Rep. Alexandria Ocasio-Cortez (D... 8YH0t3H1Y_Y 2019-02-16 21:40:10 https://i.ytimg.com/vi/8YH0t3H1Y_Y/hqdefault.jpg Rep. Ocasio-Cortez Defends Green New Deal In I...
3 UCnsvJeZO4RigQ898WdDNoBw EL PAIS 2019-02-20 14:49:34.262713 None Alexandria Ocasio-Cortez jura en Nueva York su... wAmEYOcnu_g 2019-02-17 11:03:09 https://i.ytimg.com/vi/wAmEYOcnu_g/hqdefault.jpg ALEXANDRIA OCASIO-CORTEZ: "Sed valientes con n...
4 UCJg9wBPyKMNA5sRDnvzmkdg FOX 10 Phoenix 2019-02-20 14:49:34.262733 None President Donald Trump is expected to urge Ven... VhEo5sm5Eu4 2019-02-18 22:20:22 https://i.ytimg.com/vi/VhEo5sm5Eu4/hqdefault.jpg NO SOCIALISM: President Trump Takes On Alexand...

Aside from the default parser, the parse argument allows users to create custom functions to parse and process API resonses. You can also get raw JSON from the API by using the youtube_api.parsers.raw_json() parser, or setting parser to None.

yt.search(q='alexandria ocasio-cortez',
          max_results=1,
          parser=None)
[{'kind': 'youtube#searchResult',
  'etag': '"XpPGQXPnxQJhLgs6enD_n8JR4Qk/aGNxqVTPJsGI6aEI2tVnYhn0vS8"',
  'id': {'kind': 'youtube#video', 'videoId': 'LlillsHgcaw'},
  'snippet': {'publishedAt': '2019-02-19T04:57:51.000Z',
   'channelId': 'UCXIJgqnII2ZOINSWNOGFThA',
   'title': 'Rep. Alexandria Ocasio-Cortez taken to task by fellow progressives',
   'description': 'New York City Mayor Bill de Blasio criticizes Alexandria Ocasio-Cortez over her opposition to the Amazon deal.',
   'thumbnails': {'default': {'url': 'https://i.ytimg.com/vi/LlillsHgcaw/default.jpg',
     'width': 120,
     'height': 90},
    'medium': {'url': 'https://i.ytimg.com/vi/LlillsHgcaw/mqdefault.jpg',
     'width': 320,
     'height': 180},
    'high': {'url': 'https://i.ytimg.com/vi/LlillsHgcaw/hqdefault.jpg',
     'width': 480,
     'height': 360}},
   'channelTitle': 'Fox News',
   'liveBroadcastContent': 'none'}}]

youtube_api.parsers are intended to allow customized data parsing for those who want it, with robust defaults for less advanced users.