Skip to content

Instantly share code, notes, and snippets.

@tbass134 tbass134/
Last active Sep 8, 2018

What would you like to do?
Download Transcriptions from Youtube playlist
import requests
import json
import pandas as pd
from import json_normalize #package for flattening json in pandas df
from xml.etree import ElementTree
from import json_normalize
import sys
YT_API_KEY = '{youtube_api_key}'
playlist_id = '{playlist_id}'
results = []
def load_playlist(api_key, playlist_id, next_page_token=None):
print("loading playlist next_page_token:", next_page_token)
url = ""
querystring = {
if next_page_token is not None:
querystring['pageToken'] = next_page_token
response = requests.request("GET", url, params=querystring)
json = response.json()
items = json['items']
for item in items:
video_id = item['snippet']['resourceId']['videoId']
item['transcript'] = load_transcript(video_id)
if 'nextPageToken' in json:
load_playlist(api_key, playlist_id, json['nextPageToken'])
return results
def load_transcript(video_id):
print("download transcript video_id: {}".format(video_id))
url = ""
querystring = {
# print("load video",video_id)
response = requests.request("GET", url, params=querystring)
if not response.ok:
print("could not get transcript for {} !200".format(video_id))
return ''
transcript = ''
tree = ElementTree.fromstring(response.text)
for elem in tree:
transcript += elem.text + " "
return transcript
print("could not get transcript for {} xml parse error".format(video_id))
return ''
def main(api_key=YT_API_KEY, playlist_id=playlist_id):
json_results = load_playlist(api_key, playlist_id)
#flatten the list
flat_list = []
for sublist in json_results:
for item in sublist:
#convert to dataframe
df = json_normalize(flat_list)
#save the dataframe as csv
print("file saved")
if __name__ == '__main__':
main(api_key=YT_API_KEY, playlist_id=playlist_id)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.