from Google_ import Create_Service import re from pprint import pprint from googleapiclient import errors class YouTube: def __init__(self, client_secret_file, scopes: list = None): self.client_secret_file = client_secret_file self.scopes = scopes def construct_service_instance(self): try: # API_NAME = 'youtube' # API_VERSION = 'v3' service = Create_Service(self.client_secret_file, 'youtube', 'v3', self.scopes) return service except Exception as e: print(e) return 'No service' @staticmethod def get_channel_videos_detail(service, channel_id): response = service.channels().list( part='contentDetails', id=channel_id ).execute() if response['pageInfo']['totalResults'] == 0: print('Channel not found') return '' else: uploaded_playlist_id = response.get('items')[0]['contentDetails']['relatedPlaylists']['uploads'] # print(response) # return uploaded_playlist_id try: response_playlist_items = service.playlistItems().list( part='contentDetails', playlistId=uploaded_playlist_id, maxResults=50, ) playlistItems = response_playlist_items['items'] nextPageToken = response_playlist_items.get('nextPageToken') while nextPageToken: response_playlist_items = service.playlistItems().list( part='contentDetails', playlistId=uploaded_playlist_id, maxResults=50, pageToken=nextPageToken ).execute() playlistItems.extend(response_playlist_items['items']) nextPageToken = response_playlist_items.get('nextPageToken') print('Token {0}'.format(nextPageToken)) videos = tuple(v['contentDetails'] for v in playlistItems) videos_info = [] for batch_num in range(0, len(videos), 50): video_batch = videos[batch_num: batch_num + 50] response_videos = service.videos().list( id=','.join(list(map(lambda v:v['videoId'], video_batch))), part='snippet,contentDetails,statistics', maxResults=50 ).execute() videos_info.extend(response_videos['items']) return videos_info #return playlistItems except errors.HttpError: print('Channel has 0 videos') return '' except Exception as e: print(e) return 0 @staticmethod def convert_duration(duration): try: h = int(re.search('\d+H', duration)[0][:-1]) * 60**2 if re.search('\d+H', duration) else 0 m = int(re.search('\d+M', duration)[0][:-1]) * 60 if re.search('\d+M', duration) else 0 s = int(re.search('\d+S', duration)[0][:-1]) if re.search('\d+S', duration) else 0 return h + m + s except Exception as e: print(e) return 0 if __name__ == '__main__': CLIENT_SECRET_FILE = 'client_secret.json' SCOPES = ['https://www.googleapis.com/auth/youtube'] yt = YouTube(CLIENT_SECRET_FILE, SCOPES) service = yt.construct_service_instance() channel_id = 'UCJ6Pq_rvhBWQW8sw7rYlkfw' pprint(yt.get_channel_videos_detail(service, channel_id))
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question