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 Alexandra Ocasio-Cortez.

searches = yt.search(q='alexandria ocasio-cortez',
                     max_results=5)
searches[0]
OrderedDict([('video_id', 'byc_lBOY_rI'),
             ('channel_title', 'VICE News'),
             ('channel_id', 'UCZaT_X_mc0BI-djXOlfhqWQ'),
             ('video_publish_date',
              datetime.datetime(2018, 7, 31, 18, 52, 29)),
             ('video_title',
              "Who's Afraid Of Alexandria Ocasio-Cortez? Everyone (HBO)"),
             ('video_description',
              "Alexandria Ocasio-Cortez shocked Democrats when she won a New York City primary over one of the party's entrenched leaders. Her next chapter is likely to be ..."),
             ('video_thumbnail',
              'https://i.ytimg.com/vi/byc_lBOY_rI/hqdefault.jpg'),
             ('collection_date',
              datetime.datetime(2018, 9, 5, 14, 47, 53, 196104))])

All API requests are parsed from raw JSON into orderedDictionaries. Typically an API call returns a list of OrderedDict objects. This is perfect for converting into Pandas DataFrames, or saving as JSON.

df_search = pd.DataFrame(searches)
df_search
video_id channel_title channel_id video_publish_date video_title video_description video_category video_thumbnail collection_date
0 r1yvfdUG5pQ VICE News UCZaT_X_mc0BI-djXOlfhqWQ 2018-06-28 16:42:29 Alexandria Ocasio-Cortez: There's Room For Dem... First-time candidate and 28-year-old Alexandri... None https://i.ytimg.com/vi/r1yvfdUG5pQ/hqdefault.jpg 2018-09-05 13:00:59.386546
1 VjsjoaQXrhI TMZ UCK7IIV6Q2junGSdYK3BmZMg 2018-08-27 13:17:45 Viola Davis Endorses NY Congressional Candidat... Viola Davis has one person in mind when it com... None https://i.ytimg.com/vi/VjsjoaQXrhI/hqdefault.jpg 2018-09-05 13:00:59.386617
2 I3wSSShwwwo CNN UCupvZG-5ko_eiXAupbDfxWw 2018-08-09 09:31:43 Cuomo presses Ocasio-Cortez on healthcare Democratic congressional candidate Alexandria ... None https://i.ytimg.com/vi/I3wSSShwwwo/hqdefault.jpg 2018-09-05 13:00:59.386663
3 iC0l6tKbBJs Fox News Insider UCqlYzSgsh5jdtWYfVIBoTDw 2018-07-19 12:56:02 Joe Lieberman: If Ocasio-Cortez is a Party Mod... As seen on Your World with Neil Cavuto Former ... None https://i.ytimg.com/vi/iC0l6tKbBJs/hqdefault.jpg 2018-09-05 13:00:59.386708
4 lAb2QMw9h_w Guardian News UCIRYBXDze5krPDzAEOxFGVA 2018-06-28 09:16:39 'This is the beginning': Alexandria Ocasio-Cor... Victorious Democratic candidate addresses supp... None https://i.ytimg.com/vi/lAb2QMw9h_w/hqdefault.jpg 2018-09-05 13:00:59.386746

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': '"XI7nbFXulYBIpL0ayR_gDh3eu1k/iwS8DlBT9x9lWSRCq4JFPMR-Z00"',
  'id': {'kind': 'youtube#video', 'videoId': 'byc_lBOY_rI'},
  'snippet': {'publishedAt': '2018-07-31T18:52:29.000Z',
   'channelId': 'UCZaT_X_mc0BI-djXOlfhqWQ',
   'title': "Who's Afraid Of Alexandria Ocasio-Cortez? Everyone (HBO)",
   'description': "Alexandria Ocasio-Cortez shocked Democrats when she won a New York City primary over one of the party's entrenched leaders. Her next chapter is likely to be ...",
   'thumbnails': {'default': {'url': 'https://i.ytimg.com/vi/byc_lBOY_rI/default.jpg',
     'width': 120,
     'height': 90},
    'medium': {'url': 'https://i.ytimg.com/vi/byc_lBOY_rI/mqdefault.jpg',
     'width': 320,
     'height': 180},
    'high': {'url': 'https://i.ytimg.com/vi/byc_lBOY_rI/hqdefault.jpg',
     'width': 480,
     'height': 360}},
   'channelTitle': 'VICE 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.