test for RegexMatchError
Browse files- pytube/cipher.py +3 -3
- pytube/exceptions.py +2 -6
- tests/test_exceptions.py +10 -2
- tests/test_extract.py +1 -1
pytube/cipher.py
CHANGED
@@ -34,7 +34,7 @@ def get_initial_function_name(js: str) -> str:
|
|
34 |
Function name from regex match
|
35 |
"""
|
36 |
|
37 |
-
|
38 |
r"\b[cs]\s*&&\s*[adf]\.set\([^,]+\s*,\s*encodeURIComponent\s*\(\s*(?P<sig>[a-zA-Z0-9$]+)\(", # noqa: E501
|
39 |
r"\b[a-zA-Z0-9]+\s*&&\s*[a-zA-Z0-9]+\.set\([^,]+\s*,\s*encodeURIComponent\s*\(\s*(?P<sig>[a-zA-Z0-9$]+)\(", # noqa: E501
|
40 |
r'(?P<sig>[a-zA-Z0-9$]+)\s*=\s*function\(\s*a\s*\)\s*{\s*a\s*=\s*a\.split\(\s*""\s*\)', # noqa: E501
|
@@ -49,8 +49,8 @@ def get_initial_function_name(js: str) -> str:
|
|
49 |
]
|
50 |
|
51 |
logger.debug("finding initial function name")
|
52 |
-
for
|
53 |
-
regex = re.compile(
|
54 |
results = regex.search(js)
|
55 |
if results:
|
56 |
logger.debug("finished regex search, matched: {pattern}".format(pattern=p))
|
|
|
34 |
Function name from regex match
|
35 |
"""
|
36 |
|
37 |
+
function_patterns = [
|
38 |
r"\b[cs]\s*&&\s*[adf]\.set\([^,]+\s*,\s*encodeURIComponent\s*\(\s*(?P<sig>[a-zA-Z0-9$]+)\(", # noqa: E501
|
39 |
r"\b[a-zA-Z0-9]+\s*&&\s*[a-zA-Z0-9]+\.set\([^,]+\s*,\s*encodeURIComponent\s*\(\s*(?P<sig>[a-zA-Z0-9$]+)\(", # noqa: E501
|
40 |
r'(?P<sig>[a-zA-Z0-9$]+)\s*=\s*function\(\s*a\s*\)\s*{\s*a\s*=\s*a\.split\(\s*""\s*\)', # noqa: E501
|
|
|
49 |
]
|
50 |
|
51 |
logger.debug("finding initial function name")
|
52 |
+
for pattern in function_patterns:
|
53 |
+
regex = re.compile(pattern)
|
54 |
results = regex.search(js)
|
55 |
if results:
|
56 |
logger.debug("finished regex search, matched: {pattern}".format(pattern=p))
|
pytube/exceptions.py
CHANGED
@@ -1,6 +1,5 @@
|
|
1 |
# -*- coding: utf-8 -*-
|
2 |
"""Library specific exception definitions."""
|
3 |
-
import sys
|
4 |
from typing import Union, Pattern
|
5 |
|
6 |
|
@@ -27,7 +26,7 @@ class RegexMatchError(ExtractError):
|
|
27 |
:param str pattern:
|
28 |
Pattern that failed to match
|
29 |
"""
|
30 |
-
super(
|
31 |
"{caller}: could not find match for {pattern}".format(
|
32 |
caller=caller, pattern=pattern
|
33 |
)
|
@@ -48,11 +47,8 @@ class VideoUnavailable(PytubeError):
|
|
48 |
:param str video_id:
|
49 |
A YouTube video identifier.
|
50 |
"""
|
51 |
-
super(
|
52 |
-
"{video_id} is unavailable".format(video_id=video_id)
|
53 |
-
)
|
54 |
|
55 |
-
self.exc_info = sys.exc_info()
|
56 |
self.video_id = video_id
|
57 |
|
58 |
|
|
|
1 |
# -*- coding: utf-8 -*-
|
2 |
"""Library specific exception definitions."""
|
|
|
3 |
from typing import Union, Pattern
|
4 |
|
5 |
|
|
|
26 |
:param str pattern:
|
27 |
Pattern that failed to match
|
28 |
"""
|
29 |
+
super().__init__(
|
30 |
"{caller}: could not find match for {pattern}".format(
|
31 |
caller=caller, pattern=pattern
|
32 |
)
|
|
|
47 |
:param str video_id:
|
48 |
A YouTube video identifier.
|
49 |
"""
|
50 |
+
super().__init__("{video_id} is unavailable".format(video_id=video_id))
|
|
|
|
|
51 |
|
|
|
52 |
self.video_id = video_id
|
53 |
|
54 |
|
tests/test_exceptions.py
CHANGED
@@ -1,9 +1,17 @@
|
|
1 |
# -*- coding: utf-8 -*-
|
2 |
-
from pytube.exceptions import VideoUnavailable
|
3 |
|
4 |
|
5 |
-
def
|
6 |
try:
|
7 |
raise VideoUnavailable(video_id="YLnZklYFe7E")
|
8 |
except VideoUnavailable as e:
|
9 |
assert e.video_id == "YLnZklYFe7E"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
# -*- coding: utf-8 -*-
|
2 |
+
from pytube.exceptions import VideoUnavailable, RegexMatchError
|
3 |
|
4 |
|
5 |
+
def test_video_unavailable():
|
6 |
try:
|
7 |
raise VideoUnavailable(video_id="YLnZklYFe7E")
|
8 |
except VideoUnavailable as e:
|
9 |
assert e.video_id == "YLnZklYFe7E"
|
10 |
+
assert str(e) == "YLnZklYFe7E is unavailable"
|
11 |
+
|
12 |
+
|
13 |
+
def test_regex_match_error():
|
14 |
+
try:
|
15 |
+
raise RegexMatchError(caller="hello", pattern="*")
|
16 |
+
except RegexMatchError as e:
|
17 |
+
assert str(e) == "hello: could not find match for *"
|
tests/test_extract.py
CHANGED
@@ -80,4 +80,4 @@ def test_mime_type_codec():
|
|
80 |
|
81 |
def test_mime_type_codec_with_no_match_should_error():
|
82 |
with pytest.raises(RegexMatchError):
|
83 |
-
|
|
|
80 |
|
81 |
def test_mime_type_codec_with_no_match_should_error():
|
82 |
with pytest.raises(RegexMatchError):
|
83 |
+
extract.mime_type_codec("audio/webm")
|