diff --git a/youtube_transcript_api/_api.py b/youtube_transcript_api/_api.py index ca2a000..93bbf48 100644 --- a/youtube_transcript_api/_api.py +++ b/youtube_transcript_api/_api.py @@ -53,20 +53,13 @@ class YouTubeTranscriptApi(): :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 :param cookies: a string of the path to a text file containing youtube authorization cookies - :type cookies: str - cookies.txt + :type cookies: str :return: the list of available transcripts :rtype TranscriptList: """ with requests.Session() as http_client: if cookies: - try: - cj = cookiejar.MozillaCookieJar() - cj.load(cookies) - http_client.cookies = cj - except IOError as e: - print("Warning: Path for cookies file was not valid. Did not load any cookies") - except FileNotFoundError as e: - print("Warning: Path for cookies file was not valid. Did not load any cookies") + http_client.cookies = cls.load_cookies(cookies) http_client.proxies = proxies if proxies else {} return TranscriptListFetcher(http_client).fetch(video_id) @@ -88,7 +81,7 @@ class YouTubeTranscriptApi(): :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 :param cookies: a string of the path to a text file containing youtube authorization cookies - :type cookies: str - cookies.txt + :type cookies: str :return: a tuple containing a dictionary mapping video ids onto their corresponding transcripts, and a list of video ids, which could not be retrieved :rtype ({str: [{'text': str, 'start': float, 'end': float}]}, [str]}): @@ -123,8 +116,23 @@ class YouTubeTranscriptApi(): :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 :param cookies: a string of the path to a text file containing youtube authorization cookies - :type cookies: str - cookies.txt + :type cookies: str :return: a list of dictionaries containing the 'text', 'start' and 'duration' keys :rtype [{'text': str, 'start': float, 'end': float}]: """ return cls.list_transcripts(video_id, proxies, cookies).find_transcript(languages).fetch() + + + @classmethod + def load_cookies(cls, cookies): + cj = {} + try: + cj = cookiejar.MozillaCookieJar() + cj.load(cookies) + except IOError as e: + print("Warning: Path for cookies file was not valid. Did not load any cookies") + except FileNotFoundError as e: + print("Warning: Path for cookies file was not valid. Did not load any cookies") + if not cj: + raise IOError + return cj diff --git a/youtube_transcript_api/test/test_api.py b/youtube_transcript_api/test/test_api.py index e13e7ac..5d050f0 100644 --- a/youtube_transcript_api/test/test_api.py +++ b/youtube_transcript_api/test/test_api.py @@ -185,6 +185,8 @@ class TestYouTubeTranscriptApi(TestCase): YouTubeTranscriptApi.get_transcripts(['GJLlxj_dtq8'], cookies=cookies) YouTubeTranscriptApi.get_transcript.assert_any_call('GJLlxj_dtq8', ('en',), None, cookies) + session_cookies = YouTubeTranscriptApi.load_cookies(cookies) + print("here: ", session_cookies.items()) def test_get_transcript__with_proxies(self): proxies = {'http': '', 'https:': ''}