added test_print_available_captions
Browse files- pytube/query.py +1 -1
- tests/test_captions.py +2 -1
- tests/test_cli.py +16 -0
- tests/test_query.py +4 -4
- tests/test_streams.py +16 -16
pytube/query.py
CHANGED
|
@@ -397,7 +397,7 @@ class CaptionQuery(Mapping):
|
|
| 397 |
return len(self.lang_code_index)
|
| 398 |
|
| 399 |
def __iter__(self):
|
| 400 |
-
return iter(self.lang_code_index)
|
| 401 |
|
| 402 |
def __repr__(self) -> str:
|
| 403 |
return f"{self.lang_code_index}"
|
|
|
|
| 397 |
return len(self.lang_code_index)
|
| 398 |
|
| 399 |
def __iter__(self):
|
| 400 |
+
return iter(self.lang_code_index.values())
|
| 401 |
|
| 402 |
def __repr__(self) -> str:
|
| 403 |
return f"{self.lang_code_index}"
|
tests/test_captions.py
CHANGED
|
@@ -26,7 +26,8 @@ def test_caption_query_sequence():
|
|
| 26 |
assert caption_query["en"] == caption1
|
| 27 |
assert caption_query["fr"] == caption2
|
| 28 |
with pytest.raises(KeyError):
|
| 29 |
-
caption_query["nada"]
|
|
|
|
| 30 |
|
| 31 |
|
| 32 |
def test_caption_query_get_by_language_code_when_exists():
|
|
|
|
| 26 |
assert caption_query["en"] == caption1
|
| 27 |
assert caption_query["fr"] == caption2
|
| 28 |
with pytest.raises(KeyError):
|
| 29 |
+
not_exists = caption_query["nada"]
|
| 30 |
+
assert not_exists is not None # should never reach this
|
| 31 |
|
| 32 |
|
| 33 |
def test_caption_query_get_by_language_code_when_exists():
|
tests/test_cli.py
CHANGED
|
@@ -100,6 +100,22 @@ def test_download_caption_with_lang_not_found(youtube, print_available):
|
|
| 100 |
print_available.assert_called_with(youtube.captions)
|
| 101 |
|
| 102 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
def test_display_progress_bar(capsys):
|
| 104 |
cli.display_progress_bar(bytes_received=25, filesize=100, scale=0.55)
|
| 105 |
out, _ = capsys.readouterr()
|
|
|
|
| 100 |
print_available.assert_called_with(youtube.captions)
|
| 101 |
|
| 102 |
|
| 103 |
+
def test_print_available_captions(capsys):
|
| 104 |
+
# Given
|
| 105 |
+
caption1 = Caption(
|
| 106 |
+
{"url": "url1", "name": {"simpleText": "name1"}, "languageCode": "en"}
|
| 107 |
+
)
|
| 108 |
+
caption2 = Caption(
|
| 109 |
+
{"url": "url2", "name": {"simpleText": "name2"}, "languageCode": "fr"}
|
| 110 |
+
)
|
| 111 |
+
query = CaptionQuery([caption1, caption2])
|
| 112 |
+
# When
|
| 113 |
+
cli._print_available_captions(query)
|
| 114 |
+
# Then
|
| 115 |
+
captured = capsys.readouterr()
|
| 116 |
+
assert captured.out == "Available caption codes are: en, fr\n"
|
| 117 |
+
|
| 118 |
+
|
| 119 |
def test_display_progress_bar(capsys):
|
| 120 |
cli.display_progress_bar(bytes_received=25, filesize=100, scale=0.55)
|
| 121 |
out, _ = capsys.readouterr()
|
tests/test_query.py
CHANGED
|
@@ -44,14 +44,14 @@ def test_get_last(cipher_signature):
|
|
| 44 |
"""Ensure :meth:`~pytube.StreamQuery.last` returns the expected
|
| 45 |
:class:`Stream <Stream>`.
|
| 46 |
"""
|
| 47 |
-
assert cipher_signature.streams.
|
| 48 |
|
| 49 |
|
| 50 |
def test_get_first(cipher_signature):
|
| 51 |
"""Ensure :meth:`~pytube.StreamQuery.first` returns the expected
|
| 52 |
:class:`Stream <Stream>`.
|
| 53 |
"""
|
| 54 |
-
assert cipher_signature.streams.
|
| 55 |
|
| 56 |
|
| 57 |
def test_order_by(cipher_signature):
|
|
@@ -154,10 +154,10 @@ def test_sequence(cipher_signature):
|
|
| 154 |
|
| 155 |
|
| 156 |
def test_otf(cipher_signature):
|
| 157 |
-
non_otf = cipher_signature.streams.otf()
|
| 158 |
assert len(non_otf) == 22
|
| 159 |
|
| 160 |
-
otf = cipher_signature.streams.otf(True)
|
| 161 |
assert len(otf) == 0
|
| 162 |
|
| 163 |
|
|
|
|
| 44 |
"""Ensure :meth:`~pytube.StreamQuery.last` returns the expected
|
| 45 |
:class:`Stream <Stream>`.
|
| 46 |
"""
|
| 47 |
+
assert cipher_signature.streams[-1].itag == 251
|
| 48 |
|
| 49 |
|
| 50 |
def test_get_first(cipher_signature):
|
| 51 |
"""Ensure :meth:`~pytube.StreamQuery.first` returns the expected
|
| 52 |
:class:`Stream <Stream>`.
|
| 53 |
"""
|
| 54 |
+
assert cipher_signature.streams[0].itag == 18
|
| 55 |
|
| 56 |
|
| 57 |
def test_order_by(cipher_signature):
|
|
|
|
| 154 |
|
| 155 |
|
| 156 |
def test_otf(cipher_signature):
|
| 157 |
+
non_otf = cipher_signature.streams.otf()
|
| 158 |
assert len(non_otf) == 22
|
| 159 |
|
| 160 |
+
otf = cipher_signature.streams.otf(True)
|
| 161 |
assert len(otf) == 0
|
| 162 |
|
| 163 |
|
tests/test_streams.py
CHANGED
|
@@ -11,13 +11,13 @@ from pytube import Stream, streams
|
|
| 11 |
def test_filesize(cipher_signature, mocker):
|
| 12 |
mocker.patch.object(request, "head")
|
| 13 |
request.head.return_value = {"content-length": "6796391"}
|
| 14 |
-
assert cipher_signature.streams.
|
| 15 |
|
| 16 |
|
| 17 |
def test_filesize_approx(cipher_signature, mocker):
|
| 18 |
mocker.patch.object(request, "head")
|
| 19 |
request.head.return_value = {"content-length": "123"}
|
| 20 |
-
stream = cipher_signature.streams
|
| 21 |
assert stream.filesize_approx == 22350604
|
| 22 |
stream.bitrate = None
|
| 23 |
assert stream.filesize_approx == 123
|
|
@@ -25,7 +25,7 @@ def test_filesize_approx(cipher_signature, mocker):
|
|
| 25 |
|
| 26 |
def test_default_filename(cipher_signature):
|
| 27 |
expected = "PSY - GANGNAM STYLE(강남스타일) MV.mp4"
|
| 28 |
-
stream = cipher_signature.streams
|
| 29 |
assert stream.default_filename == expected
|
| 30 |
|
| 31 |
|
|
@@ -103,7 +103,7 @@ def test_download(cipher_signature, mocker):
|
|
| 103 |
mocker.patch.object(request, "stream")
|
| 104 |
request.stream.return_value = iter([str(random.getrandbits(8 * 1024))])
|
| 105 |
with mock.patch("pytube.streams.open", mock.mock_open(), create=True):
|
| 106 |
-
stream = cipher_signature.streams
|
| 107 |
stream.download()
|
| 108 |
|
| 109 |
|
|
@@ -114,7 +114,7 @@ def test_download_with_prefix(cipher_signature, mocker):
|
|
| 114 |
request.stream.return_value = iter([str(random.getrandbits(8 * 1024))])
|
| 115 |
streams.target_directory = MagicMock(return_value="/target")
|
| 116 |
with mock.patch("pytube.streams.open", mock.mock_open(), create=True):
|
| 117 |
-
stream = cipher_signature.streams
|
| 118 |
file_path = stream.download(filename_prefix="prefix")
|
| 119 |
assert file_path == "/target/prefixPSY - GANGNAM STYLE(강남스타일) MV.mp4"
|
| 120 |
|
|
@@ -126,7 +126,7 @@ def test_download_with_filename(cipher_signature, mocker):
|
|
| 126 |
request.stream.return_value = iter([str(random.getrandbits(8 * 1024))])
|
| 127 |
streams.target_directory = MagicMock(return_value="/target")
|
| 128 |
with mock.patch("pytube.streams.open", mock.mock_open(), create=True):
|
| 129 |
-
stream = cipher_signature.streams
|
| 130 |
file_path = stream.download(filename="cool name bro")
|
| 131 |
assert file_path == "/target/cool name bro.mp4"
|
| 132 |
|
|
@@ -139,7 +139,7 @@ def test_download_with_existing(cipher_signature, mocker):
|
|
| 139 |
mocker.patch.object(os.path, "isfile")
|
| 140 |
os.path.isfile.return_value = True
|
| 141 |
with mock.patch("pytube.streams.open", mock.mock_open(), create=True):
|
| 142 |
-
stream = cipher_signature.streams
|
| 143 |
mocker.patch.object(os.path, "getsize")
|
| 144 |
os.path.getsize.return_value = stream.filesize
|
| 145 |
file_path = stream.download()
|
|
@@ -156,7 +156,7 @@ def test_download_with_existing_no_skip(cipher_signature, mocker):
|
|
| 156 |
mocker.patch.object(os.path, "isfile")
|
| 157 |
os.path.isfile.return_value = True
|
| 158 |
with mock.patch("pytube.streams.open", mock.mock_open(), create=True):
|
| 159 |
-
stream = cipher_signature.streams
|
| 160 |
mocker.patch.object(os.path, "getsize")
|
| 161 |
os.path.getsize.return_value = stream.filesize
|
| 162 |
file_path = stream.download(skip_existing=False)
|
|
@@ -165,12 +165,12 @@ def test_download_with_existing_no_skip(cipher_signature, mocker):
|
|
| 165 |
|
| 166 |
|
| 167 |
def test_progressive_streams_return_includes_audio_track(cipher_signature):
|
| 168 |
-
stream = cipher_signature.streams.filter(progressive=True)
|
| 169 |
assert stream.includes_audio_track
|
| 170 |
|
| 171 |
|
| 172 |
def test_progressive_streams_return_includes_video_track(cipher_signature):
|
| 173 |
-
stream = cipher_signature.streams.filter(progressive=True)
|
| 174 |
assert stream.includes_video_track
|
| 175 |
|
| 176 |
|
|
@@ -184,7 +184,7 @@ def test_on_progress_hook(cipher_signature, mocker):
|
|
| 184 |
request.stream.return_value = iter([str(random.getrandbits(8 * 1024))])
|
| 185 |
|
| 186 |
with mock.patch("pytube.streams.open", mock.mock_open(), create=True):
|
| 187 |
-
stream = cipher_signature.streams
|
| 188 |
stream.download()
|
| 189 |
assert callback_fn.called
|
| 190 |
args, _ = callback_fn.call_args
|
|
@@ -203,7 +203,7 @@ def test_on_complete_hook(cipher_signature, mocker):
|
|
| 203 |
request.stream.return_value = iter([str(random.getrandbits(8 * 1024))])
|
| 204 |
|
| 205 |
with mock.patch("pytube.streams.open", mock.mock_open(), create=True):
|
| 206 |
-
stream = cipher_signature.streams
|
| 207 |
stream.download()
|
| 208 |
assert callback_fn.called
|
| 209 |
|
|
@@ -233,7 +233,7 @@ def test_thumbnail_when_not_in_details(cipher_signature):
|
|
| 233 |
|
| 234 |
|
| 235 |
def test_repr_for_audio_streams(cipher_signature):
|
| 236 |
-
stream = str(cipher_signature.streams.filter(only_audio=True)
|
| 237 |
expected = (
|
| 238 |
'<Stream: itag="140" mime_type="audio/mp4" abr="128kbps" '
|
| 239 |
'acodec="mp4a.40.2" progressive="False" type="audio">'
|
|
@@ -242,7 +242,7 @@ def test_repr_for_audio_streams(cipher_signature):
|
|
| 242 |
|
| 243 |
|
| 244 |
def test_repr_for_video_streams(cipher_signature):
|
| 245 |
-
stream = str(cipher_signature.streams.filter(only_video=True)
|
| 246 |
expected = (
|
| 247 |
'<Stream: itag="137" mime_type="video/mp4" res="1080p" fps="30fps" '
|
| 248 |
'vcodec="avc1.640028" progressive="False" type="video">'
|
|
@@ -251,7 +251,7 @@ def test_repr_for_video_streams(cipher_signature):
|
|
| 251 |
|
| 252 |
|
| 253 |
def test_repr_for_progressive_streams(cipher_signature):
|
| 254 |
-
stream = str(cipher_signature.streams.filter(progressive=True)
|
| 255 |
expected = (
|
| 256 |
'<Stream: itag="18" mime_type="video/mp4" res="360p" fps="30fps" '
|
| 257 |
'vcodec="avc1.42001E" acodec="mp4a.40.2" progressive="True" type="video">'
|
|
@@ -260,7 +260,7 @@ def test_repr_for_progressive_streams(cipher_signature):
|
|
| 260 |
|
| 261 |
|
| 262 |
def test_repr_for_adaptive_streams(cipher_signature):
|
| 263 |
-
stream = str(cipher_signature.streams.filter(adaptive=True)
|
| 264 |
expected = (
|
| 265 |
'<Stream: itag="137" mime_type="video/mp4" res="1080p" fps="30fps" '
|
| 266 |
'vcodec="avc1.640028" progressive="False" type="video">'
|
|
|
|
| 11 |
def test_filesize(cipher_signature, mocker):
|
| 12 |
mocker.patch.object(request, "head")
|
| 13 |
request.head.return_value = {"content-length": "6796391"}
|
| 14 |
+
assert cipher_signature.streams[0].filesize == 6796391
|
| 15 |
|
| 16 |
|
| 17 |
def test_filesize_approx(cipher_signature, mocker):
|
| 18 |
mocker.patch.object(request, "head")
|
| 19 |
request.head.return_value = {"content-length": "123"}
|
| 20 |
+
stream = cipher_signature.streams[0]
|
| 21 |
assert stream.filesize_approx == 22350604
|
| 22 |
stream.bitrate = None
|
| 23 |
assert stream.filesize_approx == 123
|
|
|
|
| 25 |
|
| 26 |
def test_default_filename(cipher_signature):
|
| 27 |
expected = "PSY - GANGNAM STYLE(강남스타일) MV.mp4"
|
| 28 |
+
stream = cipher_signature.streams[0]
|
| 29 |
assert stream.default_filename == expected
|
| 30 |
|
| 31 |
|
|
|
|
| 103 |
mocker.patch.object(request, "stream")
|
| 104 |
request.stream.return_value = iter([str(random.getrandbits(8 * 1024))])
|
| 105 |
with mock.patch("pytube.streams.open", mock.mock_open(), create=True):
|
| 106 |
+
stream = cipher_signature.streams[0]
|
| 107 |
stream.download()
|
| 108 |
|
| 109 |
|
|
|
|
| 114 |
request.stream.return_value = iter([str(random.getrandbits(8 * 1024))])
|
| 115 |
streams.target_directory = MagicMock(return_value="/target")
|
| 116 |
with mock.patch("pytube.streams.open", mock.mock_open(), create=True):
|
| 117 |
+
stream = cipher_signature.streams[0]
|
| 118 |
file_path = stream.download(filename_prefix="prefix")
|
| 119 |
assert file_path == "/target/prefixPSY - GANGNAM STYLE(강남스타일) MV.mp4"
|
| 120 |
|
|
|
|
| 126 |
request.stream.return_value = iter([str(random.getrandbits(8 * 1024))])
|
| 127 |
streams.target_directory = MagicMock(return_value="/target")
|
| 128 |
with mock.patch("pytube.streams.open", mock.mock_open(), create=True):
|
| 129 |
+
stream = cipher_signature.streams[0]
|
| 130 |
file_path = stream.download(filename="cool name bro")
|
| 131 |
assert file_path == "/target/cool name bro.mp4"
|
| 132 |
|
|
|
|
| 139 |
mocker.patch.object(os.path, "isfile")
|
| 140 |
os.path.isfile.return_value = True
|
| 141 |
with mock.patch("pytube.streams.open", mock.mock_open(), create=True):
|
| 142 |
+
stream = cipher_signature.streams[0]
|
| 143 |
mocker.patch.object(os.path, "getsize")
|
| 144 |
os.path.getsize.return_value = stream.filesize
|
| 145 |
file_path = stream.download()
|
|
|
|
| 156 |
mocker.patch.object(os.path, "isfile")
|
| 157 |
os.path.isfile.return_value = True
|
| 158 |
with mock.patch("pytube.streams.open", mock.mock_open(), create=True):
|
| 159 |
+
stream = cipher_signature.streams[0]
|
| 160 |
mocker.patch.object(os.path, "getsize")
|
| 161 |
os.path.getsize.return_value = stream.filesize
|
| 162 |
file_path = stream.download(skip_existing=False)
|
|
|
|
| 165 |
|
| 166 |
|
| 167 |
def test_progressive_streams_return_includes_audio_track(cipher_signature):
|
| 168 |
+
stream = cipher_signature.streams.filter(progressive=True)[0]
|
| 169 |
assert stream.includes_audio_track
|
| 170 |
|
| 171 |
|
| 172 |
def test_progressive_streams_return_includes_video_track(cipher_signature):
|
| 173 |
+
stream = cipher_signature.streams.filter(progressive=True)[0]
|
| 174 |
assert stream.includes_video_track
|
| 175 |
|
| 176 |
|
|
|
|
| 184 |
request.stream.return_value = iter([str(random.getrandbits(8 * 1024))])
|
| 185 |
|
| 186 |
with mock.patch("pytube.streams.open", mock.mock_open(), create=True):
|
| 187 |
+
stream = cipher_signature.streams[0]
|
| 188 |
stream.download()
|
| 189 |
assert callback_fn.called
|
| 190 |
args, _ = callback_fn.call_args
|
|
|
|
| 203 |
request.stream.return_value = iter([str(random.getrandbits(8 * 1024))])
|
| 204 |
|
| 205 |
with mock.patch("pytube.streams.open", mock.mock_open(), create=True):
|
| 206 |
+
stream = cipher_signature.streams[0]
|
| 207 |
stream.download()
|
| 208 |
assert callback_fn.called
|
| 209 |
|
|
|
|
| 233 |
|
| 234 |
|
| 235 |
def test_repr_for_audio_streams(cipher_signature):
|
| 236 |
+
stream = str(cipher_signature.streams.filter(only_audio=True)[0])
|
| 237 |
expected = (
|
| 238 |
'<Stream: itag="140" mime_type="audio/mp4" abr="128kbps" '
|
| 239 |
'acodec="mp4a.40.2" progressive="False" type="audio">'
|
|
|
|
| 242 |
|
| 243 |
|
| 244 |
def test_repr_for_video_streams(cipher_signature):
|
| 245 |
+
stream = str(cipher_signature.streams.filter(only_video=True)[0])
|
| 246 |
expected = (
|
| 247 |
'<Stream: itag="137" mime_type="video/mp4" res="1080p" fps="30fps" '
|
| 248 |
'vcodec="avc1.640028" progressive="False" type="video">'
|
|
|
|
| 251 |
|
| 252 |
|
| 253 |
def test_repr_for_progressive_streams(cipher_signature):
|
| 254 |
+
stream = str(cipher_signature.streams.filter(progressive=True)[0])
|
| 255 |
expected = (
|
| 256 |
'<Stream: itag="18" mime_type="video/mp4" res="360p" fps="30fps" '
|
| 257 |
'vcodec="avc1.42001E" acodec="mp4a.40.2" progressive="True" type="video">'
|
|
|
|
| 260 |
|
| 261 |
|
| 262 |
def test_repr_for_adaptive_streams(cipher_signature):
|
| 263 |
+
stream = str(cipher_signature.streams.filter(adaptive=True)[0])
|
| 264 |
expected = (
|
| 265 |
'<Stream: itag="137" mime_type="video/mp4" res="1080p" fps="30fps" '
|
| 266 |
'vcodec="avc1.640028" progressive="False" type="video">'
|