added tests
Browse files- pytube/cipher.py +5 -4
- tests/test_cipher.py +26 -0
- tests/test_extract.py +20 -0
pytube/cipher.py
CHANGED
|
@@ -16,7 +16,7 @@ signature and decoding it.
|
|
| 16 |
|
| 17 |
import re
|
| 18 |
from itertools import chain
|
| 19 |
-
from typing import List
|
| 20 |
|
| 21 |
from pytube.exceptions import RegexMatchError
|
| 22 |
from pytube.helpers import regex_search, create_logger
|
|
@@ -227,7 +227,7 @@ def map_functions(js_func):
|
|
| 227 |
)
|
| 228 |
|
| 229 |
|
| 230 |
-
def parse_function(js_func: str):
|
| 231 |
"""Parse the Javascript transform function.
|
| 232 |
|
| 233 |
Break a JavaScript transform function down into a two element ``tuple``
|
|
@@ -253,7 +253,8 @@ def parse_function(js_func: str):
|
|
| 253 |
raise RegexMatchError(
|
| 254 |
"regex pattern ({pattern}) had zero matches".format(pattern=pattern),
|
| 255 |
)
|
| 256 |
-
|
|
|
|
| 257 |
|
| 258 |
|
| 259 |
def get_signature(js: str, ciphered_signature: str) -> str:
|
|
@@ -278,7 +279,7 @@ def get_signature(js: str, ciphered_signature: str) -> str:
|
|
| 278 |
|
| 279 |
for js_func in transform_plan:
|
| 280 |
name, argument = parse_function(js_func)
|
| 281 |
-
signature = transform_map[name](signature,
|
| 282 |
logger.debug("applied transform function\n")
|
| 283 |
# pprint.pformat(
|
| 284 |
# {
|
|
|
|
| 16 |
|
| 17 |
import re
|
| 18 |
from itertools import chain
|
| 19 |
+
from typing import List, Tuple
|
| 20 |
|
| 21 |
from pytube.exceptions import RegexMatchError
|
| 22 |
from pytube.helpers import regex_search, create_logger
|
|
|
|
| 227 |
)
|
| 228 |
|
| 229 |
|
| 230 |
+
def parse_function(js_func: str) -> Tuple[str, int]:
|
| 231 |
"""Parse the Javascript transform function.
|
| 232 |
|
| 233 |
Break a JavaScript transform function down into a two element ``tuple``
|
|
|
|
| 253 |
raise RegexMatchError(
|
| 254 |
"regex pattern ({pattern}) had zero matches".format(pattern=pattern),
|
| 255 |
)
|
| 256 |
+
fn_name, fn_arg = results.groups()
|
| 257 |
+
return fn_name, int(fn_arg)
|
| 258 |
|
| 259 |
|
| 260 |
def get_signature(js: str, ciphered_signature: str) -> str:
|
|
|
|
| 279 |
|
| 280 |
for js_func in transform_plan:
|
| 281 |
name, argument = parse_function(js_func)
|
| 282 |
+
signature = transform_map[name](signature, argument)
|
| 283 |
logger.debug("applied transform function\n")
|
| 284 |
# pprint.pformat(
|
| 285 |
# {
|
tests/test_cipher.py
CHANGED
|
@@ -8,3 +8,29 @@ from pytube.exceptions import RegexMatchError
|
|
| 8 |
def test_map_functions():
|
| 9 |
with pytest.raises(RegexMatchError):
|
| 10 |
cipher.map_functions("asdf")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
def test_map_functions():
|
| 9 |
with pytest.raises(RegexMatchError):
|
| 10 |
cipher.map_functions("asdf")
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def test_get_initial_function_name_with_no_match_should_error():
|
| 14 |
+
with pytest.raises(RegexMatchError):
|
| 15 |
+
cipher.get_initial_function_name("asdf")
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def test_get_transform_object_with_no_match_should_error():
|
| 19 |
+
with pytest.raises(RegexMatchError):
|
| 20 |
+
cipher.get_transform_object("asdf", var="lt")
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
def test_parse_function_with_match():
|
| 24 |
+
fn_name, fn_arg = cipher.parse_function("DE.AJ(a,15)")
|
| 25 |
+
assert fn_name == "AJ"
|
| 26 |
+
assert fn_arg == 15
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def test_parse_function_with_no_match_should_error():
|
| 30 |
+
with pytest.raises(RegexMatchError):
|
| 31 |
+
cipher.parse_function("asdf")
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
def test_reverse():
|
| 35 |
+
reversed_array = cipher.reverse([1, 2, 3, 4], None)
|
| 36 |
+
assert reversed_array == [4, 3, 2, 1]
|
tests/test_extract.py
CHANGED
|
@@ -1,5 +1,9 @@
|
|
| 1 |
# -*- coding: utf-8 -*-
|
| 2 |
"""Unit tests for the :module:`extract <extract>` module."""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
from pytube import extract
|
| 4 |
|
| 5 |
|
|
@@ -61,3 +65,19 @@ def test_get_vid_desc(cipher_signature):
|
|
| 61 |
"http://weibo.com/psyoppa"
|
| 62 |
)
|
| 63 |
assert extract.get_vid_descr(cipher_signature.watch_html) == expected
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
# -*- coding: utf-8 -*-
|
| 2 |
"""Unit tests for the :module:`extract <extract>` module."""
|
| 3 |
+
import pytest
|
| 4 |
+
|
| 5 |
+
from pytube.exceptions import RegexMatchError
|
| 6 |
+
|
| 7 |
from pytube import extract
|
| 8 |
|
| 9 |
|
|
|
|
| 65 |
"http://weibo.com/psyoppa"
|
| 66 |
)
|
| 67 |
assert extract.get_vid_descr(cipher_signature.watch_html) == expected
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
def test_eurl():
|
| 71 |
+
url = extract.eurl("videoid")
|
| 72 |
+
assert url == "https://youtube.googleapis.com/v/videoid"
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
def test_mime_type_codec():
|
| 76 |
+
mime_type, mime_subtype = extract.mime_type_codec('audio/webm; codecs="opus"')
|
| 77 |
+
assert mime_type == "audio/webm"
|
| 78 |
+
assert mime_subtype == ["opus"]
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
def test_mime_type_codec_with_no_match_should_error():
|
| 82 |
+
with pytest.raises(RegexMatchError):
|
| 83 |
+
mime_type, mime_subtype = extract.mime_type_codec("audio/webm")
|