def get_multi_youtube_video_info(): """ 一次获取max_results个youtube视频的时长,播放数等额外信息 :param user: :param max_results: <50 :return: """ video_list = Video.need_get_video_info.order_by('-publishedAt')[:50] video_id_list = [] for video in video_list: video_id_list.append(video.video_id) video_id_string = ', '.join(video_id_list) youtube = get_authenticated_service() # https://developers.google.com/youtube/v3/docs/videos/list res = youtube.videos().list(part="contentDetails, snippet, statistics", id=video_id_string).execute() # 循环获取完所有的结果 nextPageToken = res.get('nextPageToken') while ('nextPageToken' in res): nextPage = youtube.videos().list(part="contentDetails", id=video_list, pageToken=nextPageToken).execute() res['items'] = res['items'] + nextPage['items'] if 'nextPageToken' not in nextPage: res.pop('nextPageToken', None) else: nextPageToken = nextPage['nextPageToken'] # 从返回的对象里找出type为upload的 youtube_video_id_list = [] for result in res.get("items", []): video = Video.objects.get(pk=result['id']) # 某些youtube视频没有tags # 比如 https://www.youtube.com/watch?v=_Po6DWVPbmQ tags_list = result['snippet'].get('tags', None) video.tags = json.dumps(tags_list) # https://docs.djangoproject.com/en/1.6/ref/models/instances # /#specifying-which-fields-to-save video.view_count = result['statistics'].get('viewCount', 0) # 某些youtube视频没有likeCount # 比如 https://www.youtube.com/watch?v=YiTAEQNFI4A video.like_count = result['statistics'].get('likeCount', 0) # 将list转成json的格式保存到数据库中 # http://stackoverflow.com/a/16743442/1314124 duration = isodate.parse_duration(result['contentDetails']['duration']) video.duration = duration.total_seconds() video.save( update_fields=['view_count', 'like_count', 'tags', 'duration']) youtube_video_id_list.append(result['id']) return youtube_video_id_list
def get_multi_youtube_video_info(): """ 一次获取max_results个youtube视频的时长,播放数等额外信息 :param user: :param max_results: <50 :return: """ video_list = Video.need_get_video_info.order_by('-publishedAt')[:50] video_id_list = [] for video in video_list: video_id_list.append(video.video_id) video_id_string = ', '.join(video_id_list) youtube = get_authenticated_service() # https://developers.google.com/youtube/v3/docs/videos/list res = youtube.videos().list( part="contentDetails, snippet, statistics", id=video_id_string ).execute() # 循环获取完所有的结果 nextPageToken = res.get('nextPageToken') while ('nextPageToken' in res): nextPage = youtube.videos().list( part="contentDetails", id=video_list, pageToken=nextPageToken ).execute() res['items'] = res['items'] + nextPage['items'] if 'nextPageToken' not in nextPage: res.pop('nextPageToken', None) else: nextPageToken = nextPage['nextPageToken'] # 从返回的对象里找出type为upload的 youtube_video_id_list = [] for result in res.get("items", []): video = Video.objects.get(pk=result['id']) # 某些youtube视频没有tags # 比如 https://www.youtube.com/watch?v=_Po6DWVPbmQ tags_list = result['snippet'].get('tags', None) video.tags = json.dumps(tags_list) # https://docs.djangoproject.com/en/1.6/ref/models/instances # /#specifying-which-fields-to-save video.view_count = result['statistics'].get('viewCount', 0) # 某些youtube视频没有likeCount # 比如 https://www.youtube.com/watch?v=YiTAEQNFI4A video.like_count = result['statistics'].get('likeCount', 0) # 将list转成json的格式保存到数据库中 # http://stackoverflow.com/a/16743442/1314124 duration = isodate.parse_duration(result['contentDetails']['duration']) video.duration = duration.total_seconds() video.save( update_fields=['view_count', 'like_count', 'tags', 'duration']) youtube_video_id_list.append(result['id']) return youtube_video_id_list
def get_subscription_update_video(user, max_results): """ 获取认证用户的youtube首页显示的订阅频道的视频信息,保存到本地数据库 https://developers.google.com/youtube/v3/docs/activities/list#errors :param request: :return: """ youtube = get_authenticated_service(user) # home: This parameter can only be used in a properly # authorized request. Set this # parameter's value to true to retrieve the activity feed that displays on # the YouTube home page for the currently authenticated user. res = youtube.activities().list( part='snippet, contentDetails', home=True, maxResults=max_results).execute() # 循环获取完所有的结果 nextPageToken = res.get('nextPageToken') while ('nextPageToken' in res): nextPage = youtube.activities().list( part='snippet, contentDetails', home=True, maxResults=max_results, pageToken=nextPageToken ).execute() res['items'] = res['items'] + nextPage['items'] if 'nextPageToken' not in nextPage: res.pop('nextPageToken', None) else: nextPageToken = nextPage['nextPageToken'] # 从返回的对象里找出type为upload的 video_list = [] for result in res.get("items", []): channel = YouTubeChannel.objects.filter( channel_id=result['snippet']["channelId"]).first() if channel and channel.is_download: # 如果该视频所属的频道 is_download 属性被设置为True,才进行下载 # todo 待测试 if result['snippet']["type"] == 'upload': video = { 'video_id': result['contentDetails']["upload"]["videoId"], 'title': result['snippet']["title"], 'publishedAt': result['snippet']["publishedAt"], 'thumbnail': result['snippet']['thumbnails']["default"][ "url"], 'channel': result['snippet']["channelId"] } import datetime, dateutil.parser # publishedAt 为ISO 8601 ( # YYYY-MM-DDThh:mm:ss.sZ)格式,类似2008-09-26T01:51:42.000Z d = dateutil.parser.parse(video['publishedAt']) youtube_video, created = Video.objects.update_or_create( video_id=video['video_id'], defaults={'title': video['title'], 'publishedAt': d, 'thumbnail': video['thumbnail'], 'channel': channel } ) video_list.append(video) else: # https://developers.google.com/youtube/v3/docs/activities # https://developers.google.com/youtube/v3/docs/activities # #snippet.type # 有的type没有title continue return video_list
def get_subscription_update_video(user, max_results): """ 获取认证用户的youtube首页显示的订阅频道的视频信息,保存到本地数据库 https://developers.google.com/youtube/v3/docs/activities/list#errors :param request: :return: """ youtube = get_authenticated_service(user) # home: This parameter can only be used in a properly # authorized request. Set this # parameter's value to true to retrieve the activity feed that displays on # the YouTube home page for the currently authenticated user. res = youtube.activities().list(part='snippet, contentDetails', home=True, maxResults=max_results).execute() # 循环获取完所有的结果 nextPageToken = res.get('nextPageToken') while ('nextPageToken' in res): nextPage = youtube.activities().list( part='snippet, contentDetails', home=True, maxResults=max_results, pageToken=nextPageToken).execute() res['items'] = res['items'] + nextPage['items'] if 'nextPageToken' not in nextPage: res.pop('nextPageToken', None) else: nextPageToken = nextPage['nextPageToken'] # 从返回的对象里找出type为upload的 video_list = [] for result in res.get("items", []): channel = YouTubeChannel.objects.filter( channel_id=result['snippet']["channelId"]).first() if channel and channel.is_download: # 如果该视频所属的频道 is_download 属性被设置为True,才进行下载 # todo 待测试 if result['snippet']["type"] == 'upload': video = { 'video_id': result['contentDetails']["upload"]["videoId"], 'title': result['snippet']["title"], 'publishedAt': result['snippet']["publishedAt"], 'thumbnail': result['snippet']['thumbnails']["default"]["url"], 'channel': result['snippet']["channelId"] } import datetime, dateutil.parser # publishedAt 为ISO 8601 ( # YYYY-MM-DDThh:mm:ss.sZ)格式,类似2008-09-26T01:51:42.000Z d = dateutil.parser.parse(video['publishedAt']) youtube_video, created = Video.objects.update_or_create( video_id=video['video_id'], defaults={ 'title': video['title'], 'publishedAt': d, 'thumbnail': video['thumbnail'], 'channel': channel }) video_list.append(video) else: # https://developers.google.com/youtube/v3/docs/activities # https://developers.google.com/youtube/v3/docs/activities # #snippet.type # 有的type没有title continue return video_list