get_transcripts now returns a list of exceptions instead of video ids of failed videos

This commit is contained in:
Jonas Depoix 2019-12-30 16:13:18 +01:00
parent 66d02c08a1
commit 4b75a47a74
2 changed files with 6 additions and 6 deletions

View File

@ -72,11 +72,11 @@ class YouTubeTranscriptApi():
:param proxies: a dictionary mapping of http and https proxies to be used for the network requests :param proxies: a dictionary mapping of http and https proxies to be used for the network requests
:type proxies: {'http': str, 'https': str} - http://docs.python-requests.org/en/master/user/advanced/#proxies :type proxies: {'http': str, 'https': str} - http://docs.python-requests.org/en/master/user/advanced/#proxies
:return: a tuple containing a dictionary mapping video ids onto their corresponding transcripts, and a list of :return: a tuple containing a dictionary mapping video ids onto their corresponding transcripts, and a list of
video ids, which could not be retrieved exceptions which occurred for the videos which could not be retrieved
:rtype ({str: [{'text': str, 'start': float, 'end': float}]}, [str]}): :rtype ({str: [{'text': str, 'start': float, 'end': float}]}, [CouldNotRetrieveTranscript]}):
""" """
data = {} data = {}
unretrievable_videos = [] exceptions = []
for video_id in video_ids: for video_id in video_ids:
try: try:
@ -85,9 +85,9 @@ class YouTubeTranscriptApi():
if not continue_after_error: if not continue_after_error:
raise exception raise exception
unretrievable_videos.append(video_id) exceptions.append(exception)
return data, unretrievable_videos return data, exceptions
@classmethod @classmethod
def get_transcript(cls, video_id, languages=('en',), proxies=None): def get_transcript(cls, video_id, languages=('en',), proxies=None):

View File

@ -26,7 +26,7 @@ class YouTubeTranscriptCli():
) )
return '\n\n'.join( return '\n\n'.join(
[str(YouTubeTranscriptApi.CouldNotRetrieveTranscript(video_id)) for video_id in unretrievable_videos] [str(exception) for exception in unretrievable_videos]
+ ([json.dumps(transcripts) if parsed_args.json else pprint.pformat(transcripts)] if transcripts else []) + ([json.dumps(transcripts) if parsed_args.json else pprint.pformat(transcripts)] if transcripts else [])
) )