1
1
import warnings
2
- from pathlib import Path
3
- from typing import Optional , Iterable , Union
4
-
5
- from http .cookiejar import MozillaCookieJar , LoadError
2
+ from typing import Optional , Iterable
6
3
7
4
from requests import Session
8
5
9
6
from .proxies import ProxyConfig , GenericProxyConfig
10
7
11
8
from ._transcripts import TranscriptListFetcher , FetchedTranscript , TranscriptList
12
9
13
- from ._errors import CookiePathInvalid , CookieInvalid
14
-
15
-
16
- def _load_cookie_jar (cookies : Union [Path , str ]) -> MozillaCookieJar :
17
- try :
18
- cookie_jar = MozillaCookieJar ()
19
- cookie_jar .load (str (cookies ))
20
- if not cookie_jar :
21
- raise CookieInvalid (cookies )
22
- return cookie_jar
23
- except (FileNotFoundError , LoadError ):
24
- raise CookiePathInvalid (cookies )
25
-
26
10
27
11
class YouTubeTranscriptApi :
28
12
def __init__ (
29
13
self ,
30
- cookie_path : Optional [Union [Path , str ]] = None ,
31
14
proxy_config : Optional [ProxyConfig ] = None ,
32
15
http_client : Optional [Session ] = None ,
33
16
):
@@ -36,7 +19,6 @@ def __init__(
36
19
object, it is not thread-safe. Make sure to initialize an instance of
37
20
`YouTubeTranscriptApi` per thread, if used in a multi-threading scenario!
38
21
39
- :param cookie_path: Path to a text file containing YouTube authorization cookies
40
22
:param proxy_config: an optional ProxyConfig object, defining proxies used for
41
23
all network requests. This can be used to work around your IP being blocked
42
24
by YouTube, as described in the "Working around IP bans" section of the
@@ -48,8 +30,10 @@ def __init__(
48
30
"""
49
31
http_client = Session () if http_client is None else http_client
50
32
http_client .headers .update ({"Accept-Language" : "en-US" })
51
- if cookie_path is not None :
52
- http_client .cookies = _load_cookie_jar (cookie_path )
33
+ # Cookie auth has been temporarily disabled, as it is not working properly with
34
+ # YouTube's most recent changes.
35
+ # if cookie_path is not None:
36
+ # http_client.cookies = _load_cookie_jar(cookie_path)
53
37
if proxy_config is not None :
54
38
http_client .proxies = proxy_config .to_requests_dict ()
55
39
if proxy_config .prevent_keeping_connections_alive :
@@ -135,7 +119,7 @@ def list(
135
119
return self ._fetcher .fetch (video_id )
136
120
137
121
@classmethod
138
- def list_transcripts (cls , video_id , proxies = None , cookies = None ):
122
+ def list_transcripts (cls , video_id , proxies = None ):
139
123
"""
140
124
DEPRECATED: use the `list` method instead!
141
125
@@ -180,8 +164,6 @@ def list_transcripts(cls, video_id, proxies=None, cookies=None):
180
164
:type video_id: str
181
165
:param proxies: a dictionary mapping of http and https proxies to be used for the network requests
182
166
:type proxies: {'http': str, 'https': str} - http://docs.python-requests.org/en/master/user/advanced/#proxies
183
- :param cookies: a string of the path to a text file containing youtube authorization cookies
184
- :type cookies: str
185
167
:return: the list of available transcripts
186
168
:rtype TranscriptList:
187
169
"""
@@ -202,7 +184,6 @@ def list_transcripts(cls, video_id, proxies=None, cookies=None):
202
184
203
185
ytt_api = YouTubeTranscriptApi (
204
186
proxy_config = proxy_config ,
205
- cookie_path = Path (cookies ) if cookies else None ,
206
187
)
207
188
return ytt_api .list (video_id )
208
189
@@ -213,7 +194,6 @@ def get_transcripts(
213
194
languages = ("en" ,),
214
195
continue_after_error = False ,
215
196
proxies = None ,
216
- cookies = None ,
217
197
preserve_formatting = False ,
218
198
):
219
199
"""
@@ -232,8 +212,6 @@ def get_transcripts(
232
212
:type continue_after_error: bool
233
213
:param proxies: a dictionary mapping of http and https proxies to be used for the network requests
234
214
:type proxies: {'http': str, 'https': str} - http://docs.python-requests.org/en/master/user/advanced/#proxies
235
- :param cookies: a string of the path to a text file containing youtube authorization cookies
236
- :type cookies: str
237
215
:param preserve_formatting: whether to keep select HTML text formatting
238
216
:type preserve_formatting: bool
239
217
:return: a tuple containing a dictionary mapping video ids onto their corresponding transcripts, and a list of
@@ -254,7 +232,7 @@ def get_transcripts(
254
232
for video_id in video_ids :
255
233
try :
256
234
data [video_id ] = cls .get_transcript (
257
- video_id , languages , proxies , cookies , preserve_formatting
235
+ video_id , languages , proxies , preserve_formatting
258
236
)
259
237
except Exception as exception :
260
238
if not continue_after_error :
@@ -270,7 +248,6 @@ def get_transcript(
270
248
video_id ,
271
249
languages = ("en" ,),
272
250
proxies = None ,
273
- cookies = None ,
274
251
preserve_formatting = False ,
275
252
):
276
253
"""
@@ -288,8 +265,6 @@ def get_transcript(
288
265
:type languages: list[str]
289
266
:param proxies: a dictionary mapping of http and https proxies to be used for the network requests
290
267
:type proxies: {'http': str, 'https': str} - http://docs.python-requests.org/en/master/user/advanced/#proxies
291
- :param cookies: a string of the path to a text file containing youtube authorization cookies
292
- :type cookies: str
293
268
:param preserve_formatting: whether to keep select HTML text formatting
294
269
:type preserve_formatting: bool
295
270
:return: a list of dictionaries containing the 'text', 'start' and 'duration' keys
@@ -303,7 +278,7 @@ def get_transcript(
303
278
304
279
assert isinstance (video_id , str ), "`video_id` must be a string"
305
280
return (
306
- cls .list_transcripts (video_id , proxies , cookies )
281
+ cls .list_transcripts (video_id , proxies )
307
282
.find_transcript (languages )
308
283
.fetch (preserve_formatting = preserve_formatting )
309
284
.to_raw_data ()
0 commit comments