Add format kwarg to method calls to pass around

Add format kwarg that defaults to None which still gets interpreted to use JSON when its not passed. This kwarg was given to `.get_transcripts()` and `.get_transcript()` since one relies on the other therefore can forward the kwarg.
This commit is contained in:
Chris Howell 2020-07-08 15:20:47 -07:00
parent 0e6fae2504
commit b4592043dc
1 changed files with 12 additions and 4 deletions

View File

@ -12,6 +12,8 @@ from ._errors import (
CookiePathInvalid, CookiePathInvalid,
CookiesInvalid CookiesInvalid
) )
from .formatters import formats
class YouTubeTranscriptApi(): class YouTubeTranscriptApi():
@classmethod @classmethod
@ -70,7 +72,8 @@ class YouTubeTranscriptApi():
return TranscriptListFetcher(http_client).fetch(video_id) return TranscriptListFetcher(http_client).fetch(video_id)
@classmethod @classmethod
def get_transcripts(cls, video_ids, languages=('en',), continue_after_error=False, proxies=None, cookies=None): def get_transcripts(cls, video_ids, languages=('en',),
continue_after_error=False, proxies=None, cookies=None, format=None):
""" """
Retrieves the transcripts for a list of videos. Retrieves the transcripts for a list of videos.
@ -96,7 +99,8 @@ class YouTubeTranscriptApi():
for video_id in video_ids: for video_id in video_ids:
try: try:
data[video_id] = cls.get_transcript(video_id, languages, proxies, cookies) data[video_id] = cls.get_transcript(video_id, languages,
proxies, cookies, format=format)
except Exception as exception: except Exception as exception:
if not continue_after_error: if not continue_after_error:
raise exception raise exception
@ -106,7 +110,8 @@ class YouTubeTranscriptApi():
return data, unretrievable_videos return data, unretrievable_videos
@classmethod @classmethod
def get_transcript(cls, video_id, languages=('en',), proxies=None, cookies=None): def get_transcript(cls, video_id, languages=('en',), proxies=None,
cookies=None, format=None):
""" """
Retrieves the transcript for a single video. This is just a shortcut for calling:: Retrieves the transcript for a single video. This is just a shortcut for calling::
@ -125,7 +130,10 @@ class YouTubeTranscriptApi():
:return: a list of dictionaries containing the 'text', 'start' and 'duration' keys :return: a list of dictionaries containing the 'text', 'start' and 'duration' keys
:rtype [{'text': str, 'start': float, 'end': float}]: :rtype [{'text': str, 'start': float, 'end': float}]:
""" """
return cls.list_transcripts(video_id, proxies, cookies).find_transcript(languages).fetch() Formatter = formats.get_formatter(format)
transcript = cls.list_transcripts(video_id, proxies, cookies,
format=format).find_transcript(languages).fetch()
return ''.join(Formatter.format(transcript))
@classmethod @classmethod
def _load_cookies(cls, cookies, video_id): def _load_cookies(cls, cookies, video_id):