- # tubestates/youtube_api.py
+
+ ```python [|3|5-16|17-18|20-29|30-32]
+ # tubestates/youtube_api.py
-upload_playlist_ID = channel_data['upload_playlist_ID']
+ upload_playlist_ID = channel_data['upload_playlist_ID']
-video_response = []
-next_page_token = None
-while True:
- # obtaining video ID + titles
- playlist_request = self.youtube.playlistItems().list(
- part='snippet,contentDetails',
- maxResults=50, # API Limit is 50
- pageToken=next_page_token,
- playlistId=upload_playlist_ID,
- )
- playlist_response = playlist_request.execute()
- # isolating video ID
- vid_subset = [ vid_ID['contentDetails']['videoId']
- for vid_ID in playlist_response['items'] ]
- # retrieving video statistics
- vid_info_subset_request = self.youtube.videos().list(
- part='snippet,contentDetails,statistics',
- id=vid_subset
- )
- vid_info_subset_response = vid_info_subset_request.execute()
- video_response.append(vid_info_subset_response)
- # obtaining page token
- next_page_token = playlist_response.get('nextPageToken') # get method used because token may not exist
- if next_page_token is None:
- break
+ video_response = []
+ next_page_token = None
+ while True:
+ # obtaining video ID + titles
+ playlist_request = self.youtube.playlistItems().list(
+ part='snippet,contentDetails',
+ maxResults=50, # API Limit is 50
+ pageToken=next_page_token,
+ playlistId=upload_playlist_ID,
+ )
+ playlist_response = playlist_request.execute()
+ # isolating video ID
+ vid_subset = [ vid_ID['contentDetails']['videoId']
+ for vid_ID in playlist_response['items'] ]
+ # retrieving video statistics
+ vid_info_subset_request = self.youtube.videos().list(
+ part='snippet,contentDetails,statistics',
+ id=vid_subset
+ )
+ vid_info_subset_response = vid_info_subset_request.execute()
+ video_response.append(vid_info_subset_response)
+ # obtaining page token
+ next_page_token = playlist_response.get('nextPageToken') # get method used because token may not exist
+ if next_page_token is None:
+ break
-df = pd.json_normalize(video_response, 'items')
-return df
-
+ df = pd.json_normalize(video_response, 'items')
+ return df
+
+