Made testing more robust
This commit is contained in:
		
							parent
							
								
									f9e553ebaf
								
							
						
					
					
						commit
						67604ec46c
					
				|  | @ -53,20 +53,13 @@ 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 | ||||||
|         :param cookies: a string of the path to a text file containing youtube authorization cookies |         :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 |         :return: the list of available transcripts | ||||||
|         :rtype TranscriptList: |         :rtype TranscriptList: | ||||||
|         """ |         """ | ||||||
|         with requests.Session() as http_client: |         with requests.Session() as http_client: | ||||||
|             if cookies: |             if cookies: | ||||||
|                 try: |                 http_client.cookies = cls.load_cookies(cookies) | ||||||
|                     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.proxies = proxies if proxies else {} |             http_client.proxies = proxies if proxies else {} | ||||||
|             return TranscriptListFetcher(http_client).fetch(video_id) |             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 |         :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 | ||||||
|         :param cookies: a string of the path to a text file containing youtube authorization cookies |         :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 |         :return: a tuple containing a dictionary mapping video ids onto their corresponding transcripts, and a list of | ||||||
|         video ids, which could not be retrieved |         video ids, which could not be retrieved | ||||||
|         :rtype ({str: [{'text': str, 'start': float, 'end': float}]}, [str]}): |         :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 |         :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 | ||||||
|         :param cookies: a string of the path to a text file containing youtube authorization cookies |         :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 |         :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() |         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  | ||||||
|  |  | ||||||
|  | @ -185,6 +185,8 @@ class TestYouTubeTranscriptApi(TestCase): | ||||||
|         YouTubeTranscriptApi.get_transcripts(['GJLlxj_dtq8'], cookies=cookies) |         YouTubeTranscriptApi.get_transcripts(['GJLlxj_dtq8'], cookies=cookies) | ||||||
|         YouTubeTranscriptApi.get_transcript.assert_any_call('GJLlxj_dtq8', ('en',), None, 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): |     def test_get_transcript__with_proxies(self): | ||||||
|         proxies = {'http': '', 'https:': ''} |         proxies = {'http': '', 'https:': ''} | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue