def _fetch_all_youtube_data(self, youtube_build: Resource, data: dict) -> dict: # Video data extracting _video_data = youtube_build.videos().list( part='snippet', id=data['video_hash']).execute()['items'][0]['snippet'] tags = _video_data.get('tags', []) category_data = youtube_build.videoCategories().list( part='snippet', id=_video_data['categoryId']).execute()['items'][0] video_statistic = youtube_build.videos().list( part='statistics', id=data['video_hash']).execute()['items'][0]['statistics'] # Channel data extracting channel_data = youtube_build.channels().list( part='brandingSettings', id=data['channel_hash']).execute( )['items'][0]['brandingSettings']['channel'] source_data = youtube_build.channels().list( part='topicDetails', id=data['channel_hash']).execute()['items'][0].get( 'topicDetails', {}) return { 'video': { 'tags': [encode_str(tag) for tag in tags], 'youtube_data': { 'comment_count': video_statistic.get('commentCount', 0), 'positive_mark_number': video_statistic.get('likeCount', 0), 'negative_mark_number': video_statistic.get('dislikeCount', 0), 'view_count': video_statistic.get('viewCount', 0) }, 'category': { 'youtube_id': category_data['id'], 'etag': category_data['etag'], 'name': encode_str(category_data['snippet']['title'].lower()) } }, 'channel': { 'details': { 'description': encode_str(channel_data.get('description', '')), 'country': channel_data.get('country', ''), 'keywords': encode_str(channel_data.get('keywords', '')) }, 'sources': [ encode_str(name[name.find('/wiki/') + 6:]) for name in source_data.get('topicCategories', []) ] } }
def get_upload_playlist_id(youtube: api.Resource, channel_id: str): global _total_quota_usage _total_quota_usage += 1 request = youtube.channels().list(part="contentDetails", id=channel_id) response = YouTubeChannelListResponse.from_dict(request.execute()) if response.pageInfo.totalResults == 0 or response.items[ 0].contentDetails is None: return None return response.items[0].contentDetails.relatedPlaylists.uploads
def get_channel_picture(youtube: api.Resource, channel_id: str): global _total_quota_usage _total_quota_usage += 1 request = youtube.channels().list(part="snippet", id=channel_id) response = YouTubeChannelListResponse.from_dict(request.execute()) if response.pageInfo.totalResults == 0 or response.items[0].snippet is None: return None thumbnails = response.items[0].snippet.thumbnails if "default" in thumbnails: return thumbnails["default"].url return None
def get_channel_videos(youtube: d.Resource): print(f"Get videos for my channel") request = youtube.channels().list(mine=True, part='contentDetails') uploads = request.execute() playlist_id = \ uploads["items"][0]["contentDetails"]["relatedPlaylists"]["uploads"] uploaded_videos = [] next_page_token = None while 1: res = youtube.playlistItems().list( playlistId=playlist_id, part='snippet', maxResults=50, pageToken=next_page_token).execute() uploaded_videos += res['items'] next_page_token = res.get('nextPageToken') if next_page_token is None: break return uploaded_videos
def print_info(youtube: d.Resource): request = youtube.channels().list(part="snippet", mine=True) response = request.execute() channel_id = response['items'][0]['id'] channel_name = response['items'][0]['snippet']['title'] print(f'Channel info:\n\t- name: {channel_name}\n\t- id: {channel_id}')
def get_id(youtube: d.Resource): request = youtube.channels().list(part="id", mine=True) response = request.execute() channel_id = response['items'][0]['id'] return channel_id