Home > Blockchain >  Youtube Data API: extract Transcript for a list of dictionaries
Youtube Data API: extract Transcript for a list of dictionaries

Time:09-16

Im trying to get the transcript of a number of videos of a playlist. When I run the code I get as a result the list below, which contains the id of each video as key of a dictionary, and a list of dictionaries as the value. Does anyone know how a could extract and join only the "text" from the list and store it in a variable named "GetText"?

this is the code:

from googleapiclient.discovery import build
from youtube_transcript_api import YouTubeTranscriptApi
import os

api_key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

nextPageToken = None
srt = []
vid_ids = []
vid_title = []

while True:
    #1.query API 
    rq = build("youtube", "v3", developerKey=api_key).playlistItems().list(
            part="contentDetails, snippet",
            playlistId="PLNIs-AWhQzckr8Dgmgb3akx_gFMnpxTN5",
            maxResults=50, 
            pageToken=nextPageToken       
            ).execute()
            
    #2.Create a list with video Ids and Titles   
    for item in rq["items"]:
        vid_ids.append(item["contentDetails"]["videoId"])
        vid_title.append(item["snippet"]["title"])
    
    nextPageToken = rq.get('nextPageToken')
    if not nextPageToken:
        break

#3.Get transcripts    
for i in vid_ids:
    try:
        srt  = [YouTubeTranscriptApi.get_transcripts([i])]                      
    except:
        print(f"{i} doesn't have a transcript")

 print(srt)

#4.For each video id extract the Key:"text" from a list of dictionaries 
   ?????????????????????

this is a part of the list of transcripts I get:

[({'KHO5NIcZAc4': [{'text': 'welcome to this wise ell tutorial in', 'start': 0.23, 'duration': 4.15}, {'text': "this video we're going to teach you", 'start': 3.06, 'duration': 3.09}, {'text': 'about working with the visual basic', 'start': 4.38, 'duration': 3.66}, {'text': 'editor application with a name to', 'start': 6.15, 'duration': 4.409}, {'text': 'writing some Excel VBA code in this', 'start': 8.04, 'duration': 3.66}, {'text': "video we're not going to write any code", 'start': 10.559, 'duration': 2.881}, {'text': 'itself but we are going to do is show', 'start': 11.7, 'duration': 3.45}, {'text': 'you how you can set up and work with the', 'start': 13.44, 'duration': 3.839}, {'text': "visual basic editor so I'll start by", 'start': 15.15, 'duration': 3.99}, {'text': 'showing you how you can access the VBA', 'start': 17.279, 'duration': 3.75}, {'text': 'deter from whichever version of Excel', 'start': 19.14, 'duration': 4.11}, {'text': "you happen to be working in we'll talk", 'start': 21.029, 'duration': 3.931}, {'text': 'about how you can switch between the the', 'start': 23.25, 'duration': 4.17}, {'text': 'VBA editor and Excel itself with some', 'start': 24.96, 'duration': 4.649}, {'text': "nice quick keyboard shortcuts we'll also", 'start': 27.42, 'duration': 3.54}, {'text': 'give you a quick whirlwind tour of the', 'start': 29.609, 'duration': 3.001}, {'text': 'VB screen and explain what the main', 'start': 30.96, 'duration': 4.259}, {'text': 'window is in the VB editor application', 'start': 32.61, 'duration': 5.4}, {'text': "now for once we've covered that we'll", 'start': 35.219, 'duration': 3.84}, {'text': 'move on to show you how you work with', 'start': 38.01, 'duration': 2.97}, {'text': 'modules which are the places where', 'start': 39.059, 'duration': 3.541}, {'text': "you're going to write your code so I'll", 'start': 40.98, 'duration': 3.149}, {'text': 'talk about how you can create and remove', 'start': 42.6, 'duration': 3.63}, {'text': 'modules how you can switch between the', 'start': 44.129, 'duration': 4.11}, {'text': "various ones you've created how you can", 'start': 46.23, 'duration': 4.2}, {'text': 'rename them and finally how you set up', 'start': 48.239, 'duration': 3.691}, {'text': "the fonts and colors that you're going", 'start': 50.43, 'duration': 3.39}, {'text': "to use while you're actually programming", 'start': 51.93, 'duration': 6.84}, {'text': "so let's get started before you can", 'start': 53.82, 'duration': 7.2}, {'text': 'start writing VBA code you first of all', 'start': 58.77, 'duration': 3.9}, {'text': 'need to open up the visual basic editor', 'start': 61.02, 'duration': 3.99}, {'text': 'application and the way that you do that', 'start': 62.67, 'duration': 3.989}, {'text': 'depends on which version of Excel you', 'start': 65.01, 'duration': 4.2}, {'text': "happen to be using if you're in Excel", 'start': 66.659, 'duration': 4.741}, {'text': '2003 you can quite simply head to the', 'start': 69.21, 'duration': 4.53}, {'text': 'Tools menu find the macro option in', 'start': 71.4, 'duration': 4.079}, {'text': 'there and then click the visual basic', 'start': 73.74, 'duration': 4.44}, {'text': "editor option there too you'll probably", 'start': 75.479, 'duration': 4.14}, {'text': 'find that a guess a bit annoying having', 'start': 78.18, 'duration': 3.27}, {'text': 'to go through this menu every time you', 'start': 79.619, 'duration': 3.661}, {'text': 'want to get to that option so', 'start': 81.45, 'duration': 3.599}, {'text': 'alternatively you could also display the', 'start': 83.28, 'duration': 5.28}, {'text': 'visual basic toolbar in Excel to do that', 'start': 85.049, 'duration': 5.28}, {'text': "you'd need to right-click somewhere at", 'start': 88.56, 'duration': 2.94}, {'text': 'the top of the screen where all the', 'start': 90.329, 'duration': 3.54}, {'text': 'existing little bars are choose visual', 'start': 91.5, 'duration': 4.979}, {'text': "basic and then I'm going to drag this", 'start': 93.869, 'duration': 4.14}, {'text': 'and nest it up at the top of the screen', 'start': 96.479, 'duration': 3.691}, {'text': 'like all the little buzz you should find', 'start': 98.009, 'duration': 3.871}, {'text': "on there that there's a symbol button", 'start': 100.17, 'duration': 3.3}, {'text': 'you can click on to take you to the', 'start': 101.88, 'duration': 5.419}, {'text': 'visual basic editor and there it is', 'start': 103.47, 'duration': 3.829}, {'text': 'to get into the VB editor in Excel 2007', 'start': 108.71, 'duration': 5.47}, {'text': 'I first of all need to display the', 'start': 112.41, 'duration': 3.81}, {'text': 'Developer tab in the ribbon at the top', 'start': 114.18, 'duration': 2.67}, {'text': 'of the screen', 'start': 116.22, 'duration': 2.61}, {'text': 'and to do that I need to go to the', 'start': 116.85, 'duration': 3.33}, {'text': 'office button at the top left hand', 'start': 118.83, 'duration': 5.07}, {'text': 'corner choose Excel options and then on', 'start': 120.18, 'duration': 6.06}, {'text': 'the popular page of the dialog box that', 'start': 123.9, 'duration': 4.44}, {'text': "I'm looking at find the checkbox which", 'start': 126.24, 'duration': 4.11}, {'text': 'says show Developer tab in the ribbon', 'start': 128.34, 'duration': 6.45}, {'text': 'check it and then click OK you should', 'start': 130.35, 'duration': 5.73}, {'text': "find that there's a new tab in the", 'start': 134.79, 'duration': 3.0}, {'text': 'ribbon now called Developer and if you', 'start': 136.08, 'duration': 3.69}, {'text': 'go to that one the very first button on', 'start': 137.79, 'duration': 4.83}, {'text': 'there visual basic clicking it will take', 'start': 139.77, 'duration': 8.01}, {'text': 'you to the VBA editor yet again if', 'start': 142.62, 'duration': 9.18}, {'text': "you're in Excel 2010 or 2013 you also", 'start': 147.78, 'duration': 5.88}, {'text': 'need to display the Developer tab in the', 'start': 151.8, 'duration': 3.42}, {'text': 'ribbon but the way you do it in these', 'start': 153.66, 'duration': 3.06}, {'text': 'two versions of Excel is slightly', 'start': 155.22, 'duration': 4.59}, {'text': 'different to 2007 what you need to do is', 'start': 156.72, 'duration': 5.58}, {'text': 'right-click on any existing ribbon tab', 'start': 159.81, 'duration': 5.79}, {'text': 'choose customize the ribbon and then on', 'start': 162.3, 'duration': 5.76}, {'text': 'the dialog box which appears find the', 'start': 165.6, 'duration': 4.44}, {'text': 'uncheck box next to the word Developer', 'start': 168.06, 'duration': 6.06}, {'text': 'check it and then click OK once again', 'start': 170.04, 'duration': 5.55}, {'text': "you'll find the Developer tab appears in", 'start': 174.12, 'duration': 3.48}, {'text': "the ribbon and if you select it you'll", 'start': 175.59, 'duration': 3.69}, {'text': 'be able to click on the first button', 'start': 177.6, 'duration': 3.6}, {'text': 'there visual basic to take you once', 'start': 179.28, 'duration': 5.3}, {'text': 'again into the visual basic editor', 'start': 181.2, 'duration': 3.38}, {'text': 'whichever version of Excel you happen to', 'start': 185.48, 'duration': 3.82}, {'text': 'be using when you want to switch back', 'start': 187.71, 'duration': 4.08}, {'text': 'into Excel itself you can simply click', 'start': 189.3, 'duration': 4.89}, {'text': 'on the first button on the VB editor', 'start': 191.79, 'duration': 3.96}, {'text': 'toolbar it looks like a little Excel', 'start': 194.19, 'duration': 3.42}, {'text': 'icon if you click on that it takes you', 'start': 195.75, 'duration': 5.16}, {'text': 'straight back to Excel you can also just', 'start': 197.61, 'duration': 4.8}, {'text': "use the taskbar although you can't see", 'start': 200.91, 'duration': 3.57}, {'text': 'it on my screen if I hover my mouse at', 'start': 202.41, 'duration': 3.75}, {'text': 'the bottom of the screen it will pop up', 'start': 204.48, 'duration': 3.42}, {'text': 'with the two icons related to the two', 'start': 206.16, 'duration': 3.69}, {'text': 'applications so you can simply click on', 'start': 207.9, 'duration': 3.6}, {'text': 'either of those two to switch you', 'start': 209.85, 'duration': 4.71}, {'text': 'between the two applications you might', 'start': 211.5, 'duration': 4.05}, {'text': "also have noticed while I've been", 'start': 214.56, '.....

CodePudding user response:

Frankly, I don't understand your problem.

This should be basis knowledge: use for-loops to work with list and dictionares.

That's all.

data = [({'KHO5NIcZAc4':
          [{'text': 'welcome to this wise ell tutorial in', 'start': 0.23, 'duration': 4.15}, {'text': "this video we're going to teach you", 'start': 3.06, 'duration': 3.09}, {'text': 'about working with the visual basic', 'start': 4.38, 'duration': 3.66}, {'text': 'editor application with a name to', 'start': 6.15, 'duration': 4.409}, {'text': 'writing some Excel VBA code in this', 'start': 8.04, 'duration': 3.66}, {'text': "video we're not going to write any code", 'start': 10.559, 'duration': 2.881}, {'text': 'itself but we are going to do is show', 'start': 11.7, 'duration': 3.45}, {'text': 'you how you can set up and work with the', 'start': 13.44, 'duration': 3.839}, {'text': "visual basic editor so I'll start by", 'start': 15.15, 'duration': 3.99}, {'text': 'showing you how you can access the VBA', 'start': 17.279, 'duration': 3.75}, {'text': 'deter from whichever version of Excel', 'start': 19.14, 'duration': 4.11}, {'text': "you happen to be working in we'll talk", 'start': 21.029, 'duration': 3.931}, {'text': 'about how you can switch between the the', 'start': 23.25, 'duration': 4.17}, {'text': 'VBA editor and Excel itself with some', 'start': 24.96, 'duration': 4.649}, {'text': "nice quick keyboard shortcuts we'll also", 'start': 27.42, 'duration': 3.54}, {'text': 'give you a quick whirlwind tour of the', 'start': 29.609, 'duration': 3.001}, {'text': 'VB screen and explain what the main', 'start': 30.96, 'duration': 4.259}, {'text': 'window is in the VB editor application', 'start': 32.61, 'duration': 5.4}]
        })]

for item in data:
    #print(item)
    for video_id, transcript in item.items():
        print('ID:', video_id)
        all_parts = []
        for part in transcript:
            #print(part['text'])
            all_parts.append(part['text'])
            
        full_text = " ".join(all_parts)
        print(full_text)

Result:

ID: KHO5NIcZAc4
welcome to this wise ell tutorial in this video we're going to teach you about working with the visual basic editor application with a name to writing some Excel VBA code in this video we're not going to write any code itself but we are going to do is show you how you can set up and work with the visual basic editor so I'll start by showing you how you can access the VBA deter from whichever version of Excel you happen to be working in we'll talk about how you can switch between the the VBA editor and Excel itself with some nice quick keyboard shortcuts we'll also give you a quick whirlwind tour of the VB screen and explain what the main window is in the VB editor application

BTW:

When you use for-loop to work with list or dictionary then you can use print(...), print(type(...)) and print( some_dictionary.keys() ) to see what you have in variables and what to use in nested for-loop.

  • Related