mardi 3 août 2021

Getting RANDOM YouTube API data with 2 API calls

I am trying to get data from YouTube API, random videos along with their statistics, and store in pandas, but I keep getting error. I used two differnte API keys, one for search and one for videos' statistics. Python code:

API_KEY=xxx
def get_video_details(video_id):

    #collecting view, like, dislike, comment counts
    url_video_stats = "https://www.googleapis.com/youtube/v3/videos?id="+video_id+"&part=statistics&key="+API_KEY
    response_video_stats = requests.get(url_video_stats).json()

    view_count = response_video_stats['items'][0]['statistics']['viewCount']
    like_count = response_video_stats['items'][0]['statistics']['likeCount']
    dislike_count = response_video_stats['items'][0]['statistics']['dislikeCount']
    comment_count = response_video_stats['items'][0]['statistics']['commentCount']

    return view_count, like_count, dislike_count, comment_count

count = 50
random = ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(3))
API_KEY=xxxxxx
def get_videos(df):
    pageToken = ""
    while 1:
        url = "https://www.googleapis.com/youtube/v3/search?key={}&maxResults={}&part=snippet&type=video&q={}".format(API_KEY,count,random)
        response = requests.get(url).json()
        time.sleep(1) #give it a second before starting the for loop
        for video in response['items']:
            if video['id']['kind'] == "youtube#video":
                video_id = video['id']['videoId']
                video_title = video['snippet']['title']
                video_title = str(video_title).replace("&","")
                upload_date = video['snippet']['publishedAt']
                upload_date = str(upload_date).split("T")[0]
                view_count, like_count, dislike_count, comment_count = get_video_details(video_id)

                df = df.append({'video_id':video_id,'video_title':video_title,
                                "upload_date":upload_date,"view_count":view_count,
                                "like_count":like_count,"dislike_count":dislike_count,
                                "comment_count":comment_count},ignore_index=True)
        try:
            if response['nextPageToken'] != None: #if none, it means it reached the last page and break out of it
                pageToken = "pageToken=" + response['nextPageToken']
        except:
            break
    return df

#dataframe
df2 = pd.DataFrame(columns=["video_id","video_title","upload_date","view_count","like_count","dislike_count","comment_count"]) 
df2 = get_videos(df2)
df2

And I keep getting this error:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-1-bc6e180554c2> in <module>
     54 #dataframe
     55 df2 = pd.DataFrame(columns=["video_id","video_title","upload_date","view_count","like_count","dislike_count","comment_count"])
---> 56 df2 = get_videos(df2)
     57 df2

<ipython-input-1-bc6e180554c2> in get_videos(df)
     32         response = requests.get(url).json()
     33         time.sleep(1) #give it a second before starting the for loop
---> 34         for video in response['items']:
     35             if video['id']['kind'] == "youtube#video":
     36                 video_id = video['id']['videoId']

KeyError: 'items'

When I run in parts, I see that df is not returning and the response variable runs only when I import string right before defining random. Any ideas on how to solve this issue? I suspect the 50 random videos aren't been imported properly at url. Also, is it possible to get more than 50?




Aucun commentaire:

Enregistrer un commentaire