Nick Ficano
commited on
Commit
·
6d999e1
1
Parent(s):
f8d091a
clean up, fixed bug in reusing instance.
Browse files* ``_js_code`` became ``_js_cache``
* clear ``_js_cache`` after video objects are created.
* updated tests.
- pytube/api.py +9 -5
- tests/test_pytube.py +1 -1
pytube/api.py
CHANGED
@@ -65,7 +65,7 @@ class YouTube(object):
|
|
65 |
"""
|
66 |
self._filename = None
|
67 |
self._video_url = None
|
68 |
-
self.
|
69 |
self._videos = []
|
70 |
if url:
|
71 |
self.from_url(url)
|
@@ -193,6 +193,9 @@ class YouTube(object):
|
|
193 |
signature = self._get_cipher(stream_map["s"][idx], js_url)
|
194 |
url = "{}&signature={}".format(url, signature)
|
195 |
self._add_video(url, self.filename, **quality_profile)
|
|
|
|
|
|
|
196 |
|
197 |
def get(self, extension=None, resolution=None, profile=None):
|
198 |
"""Gets a single video given a file extention (and/or resolution
|
@@ -348,18 +351,19 @@ class YouTube(object):
|
|
348 |
The url of the javascript file.
|
349 |
"""
|
350 |
reg_exp = re.compile(r'\.sig\|\|([a-zA-Z0-9$]+)\(')
|
351 |
-
|
|
|
352 |
response = urlopen(url)
|
353 |
if not response:
|
354 |
raise PytubeError("Unable to open url: {}".format(self.url))
|
355 |
-
self.
|
356 |
try:
|
357 |
-
matches = reg_exp.search(self.
|
358 |
if matches:
|
359 |
# Return the first matching group.
|
360 |
func = next(g for g in matches.groups() if g is not None)
|
361 |
# Load js into JS Python interpreter.
|
362 |
-
jsi = JSInterpreter(self.
|
363 |
initial_function = jsi.extract_function(func)
|
364 |
return initial_function([signature])
|
365 |
except Exception as e:
|
|
|
65 |
"""
|
66 |
self._filename = None
|
67 |
self._video_url = None
|
68 |
+
self._js_cache = False
|
69 |
self._videos = []
|
70 |
if url:
|
71 |
self.from_url(url)
|
|
|
193 |
signature = self._get_cipher(stream_map["s"][idx], js_url)
|
194 |
url = "{}&signature={}".format(url, signature)
|
195 |
self._add_video(url, self.filename, **quality_profile)
|
196 |
+
# Clear the cached js. Make sure to keep this at the end of
|
197 |
+
# ``from_url()`` so we can mock inject the js in tests.
|
198 |
+
self._js_cache = None
|
199 |
|
200 |
def get(self, extension=None, resolution=None, profile=None):
|
201 |
"""Gets a single video given a file extention (and/or resolution
|
|
|
351 |
The url of the javascript file.
|
352 |
"""
|
353 |
reg_exp = re.compile(r'\.sig\|\|([a-zA-Z0-9$]+)\(')
|
354 |
+
# Cache the js since ``_get_cipher()`` will be called for each video.
|
355 |
+
if not self._js_cache:
|
356 |
response = urlopen(url)
|
357 |
if not response:
|
358 |
raise PytubeError("Unable to open url: {}".format(self.url))
|
359 |
+
self._js_cache = response.read().decode("utf-8")
|
360 |
try:
|
361 |
+
matches = reg_exp.search(self._js_cache)
|
362 |
if matches:
|
363 |
# Return the first matching group.
|
364 |
func = next(g for g in matches.groups() if g is not None)
|
365 |
# Load js into JS Python interpreter.
|
366 |
+
jsi = JSInterpreter(self._js_cache)
|
367 |
initial_function = jsi.extract_function(func)
|
368 |
return initial_function([signature])
|
369 |
except Exception as e:
|
tests/test_pytube.py
CHANGED
@@ -15,7 +15,7 @@ class TestPytube(object):
|
|
15 |
with mock.patch('pytube.api.urlopen') as urlopen:
|
16 |
urlopen.return_value.read.return_value = self.mock_html
|
17 |
self.yt = api.YouTube()
|
18 |
-
self.yt.
|
19 |
self.yt.from_url(url)
|
20 |
|
21 |
def test_get_video_id(self):
|
|
|
15 |
with mock.patch('pytube.api.urlopen') as urlopen:
|
16 |
urlopen.return_value.read.return_value = self.mock_html
|
17 |
self.yt = api.YouTube()
|
18 |
+
self.yt._js_cache = self.mock_js
|
19 |
self.yt.from_url(url)
|
20 |
|
21 |
def test_get_video_id(self):
|