@@ -199,7 +199,7 @@ def subset_videos_json(videos, subset_by, subset_videos):
199199 """make a list of popular or recent videos"""
200200 playlist_id = videos [0 ]["snippet" ]["playlistId" ]
201201 video_ids = [video ["contentDetails" ]["videoId" ] for video in videos ]
202- # we get the video statistics via Youtube API
202+ # we get the video statistics via Youtube API
203203 video_stats = {}
204204 for i in range (0 , len (video_ids ), 50 ):
205205 video_ids_chunk = video_ids [i : i + 50 ]
@@ -217,10 +217,11 @@ def subset_videos_json(videos, subset_by, subset_videos):
217217 video_stats_json = req .json ()
218218 for video in video_stats_json ["items" ]:
219219 video_stats [video ["id" ]] = video ["statistics" ]
220- # we add the statistics to the videos
220+ for video_id in video_ids_chunk :
221+ if video_id not in video_stats :
222+ video_stats [video_id ] = {"viewCount" : 0 , "likeCount" : 0 , "dislikeCount" : 0 }
221223 for video in videos :
222224 video ["statistics" ] = video_stats [video ["contentDetails" ]["videoId" ]]
223- # we sort the videos
224225 if subset_by == "views" :
225226 videos = sorted (videos , key = lambda video : video ["statistics" ]["viewCount" ], reverse = True )
226227 elif subset_by == "recent" :
@@ -234,9 +235,9 @@ def subset_videos_json(videos, subset_by, subset_videos):
234235 years = now .year - published_at .year
235236 video ["statistics" ]["views_per_year" ] = int (views ) / (years + 1 )
236237 videos = sorted (videos , key = lambda video : video ["statistics" ]["views_per_year" ], reverse = True )
237- # we make a subset of the videos
238+ # we limit the number of videos if needed
238239 if subset_videos is not None :
239- videos = videos [:max_videos ]
240+ videos = videos [:subset_videos ]
240241 save_json (YOUTUBE .cache_dir , f"playlist_{ playlist_id } _videos" , videos )
241242 return videos
242243
0 commit comments