How to pull video info from the YouTube data API

Posted by BigEyeGuy on 2020-02-02 17:57:09

If you want to create an XML video sitemap for the purposes of SEO, there are certain fields that are recommended for inclusion by Google, including thumbnail URL, title, duration, etc.

Grabbing the data for these fields for a few videos is easy enough to do manually, but if this data is needed for hundreds of videos, this is not an option. In this situation, the data can be requested from the Youtube Data API (v3), as described below for the 'duration' field example:

import requests
import json

list_of_video_ids = ['ID-1-v33Pk', 'ID-2-ZXmV', 'ID-3-yWz2']

URLS = []

for url in list_of_video_ids:
    URLS.append("{YOUR-API-KEY}" % url)

# for each url, we grab the nested json duration value and add to empty 'video_ids' list

video_ids = []
for url in URLS:
    r = requests.get(url=url)
    data = r.json()
    j = data['items'][0]
    k = j['contentDetails']['duration']


# we could then consolidate these lists using pandas if necessary

You could then copy URLs & video_ids lists from the terminal to an excel spreadsheet before adding to you XML video sitemap, or you could improve this script to add the duration to the relevant URL. For info on how to do this, see learnings from my previous post on How to alter XML shopping feeds.

Posted by BigEyeGuy on 2020-02-02 17:57:09