File size: 17,295 Bytes
b8d2d59 e573655 d68bda8 cd0bfbf a92dd5e 1e9e88c 4258da6 a92dd5e 85bb2e5 4258da6 b8d2d59 4258da6 1a404f0 272841b 1a404f0 7a1d0be 4258da6 7a1d0be 4258da6 7a1d0be 4258da6 766d18b 7a1d0be 4258da6 7a1d0be 4258da6 96801e7 4258da6 766d18b 4258da6 7a1d0be 4258da6 7a1d0be 4258da6 7a1d0be 4258da6 7a1d0be 4258da6 7ad4dc3 4258da6 7a1d0be 4258da6 7ad4dc3 4258da6 1e829ab 1e9e88c 7a1d0be 82321d6 8adc165 7a1d0be 4258da6 7ad4dc3 4258da6 7a1d0be 84a96f3 c16d7a9 7ad4dc3 c16d7a9 7ad4dc3 c16d7a9 9f6f947 84a96f3 f3408d8 84a96f3 f3408d8 84a96f3 b8d2d59 1319b15 b8d2d59 e573655 b8d2d59 1319b15 cf801d3 1319b15 b8d2d59 1319b15 b8d2d59 e573655 cf801d3 e573655 b8d2d59 9f6f947 766d18b b8d2d59 ee0deb6 b8d2d59 766d18b b8d2d59 766d18b b8d2d59 9f6f947 b8d2d59 1319b15 ee0deb6 1319b15 b8d2d59 9f6f947 b8d2d59 1319b15 b8d2d59 9f6f947 b8d2d59 5340b13 b8d2d59 f1b9623 9f6f947 f1b9623 ee0deb6 f1b9623 9f6f947 0bdab6b 81d2efb 0bdab6b 81d2efb 1e829ab 0bdab6b 89105b1 0bdab6b 81d2efb 0bdab6b 81d2efb 0bdab6b 81d2efb b8bc882 31753be 85bb2e5 b8bc882 31753be 9f6f947 ea25aa7 9f6f947 ea25aa7 ee0deb6 ea25aa7 9f6f947 ea25aa7 9f6f947 ea25aa7 9f6f947 b22a991 9f6f947 ee0deb6 9f6f947 b22a991 ee0deb6 b22a991 272841b b22a991 ee0deb6 b22a991 9f6f947 c1037d1 272841b c1037d1 1a404f0 272841b ee0deb6 1a404f0 ba0f647 272841b ba0f647 272841b ba0f647 272841b ee0deb6 ba0f647 9a0374e ba0f647 91d675f a485633 91d675f 96577d7 91d675f d68bda8 91d675f d68bda8 91d675f d68bda8 96577d7 91d675f 9f6f947 96577d7 b8bc882 96577d7 b8bc882 ee0deb6 b8bc882 96577d7 b8bc882 96577d7 b8bc882 96577d7 b8bc882 31753be 96577d7 b8bc882 31753be c664757 0d5b7aa c664757 9f6f947 d4361d3 ee0deb6 272841b ee0deb6 d4361d3 ee0deb6 272841b ee0deb6 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 |
import argparse
import logging
import os
from unittest import mock
from unittest.mock import MagicMock, patch
import pytest
from pytube import Caption, CaptionQuery, cli, StreamQuery
from pytube.exceptions import PytubeError
parse_args = cli._parse_args
@mock.patch("pytube.cli._parse_args")
def test_main_invalid_url(_parse_args): # noqa: PT019
parser = argparse.ArgumentParser()
args = parse_args(parser, ["crikey",],)
_parse_args.return_value = args
with pytest.raises(SystemExit):
cli.main()
@mock.patch("pytube.cli.display_streams")
@mock.patch("pytube.cli.YouTube")
def test_download_when_itag_not_found(youtube, display_streams):
# Given
youtube.streams = mock.Mock()
youtube.streams.get_by_itag.return_value = None
# When
with pytest.raises(SystemExit):
cli.download_by_itag(youtube, 123)
# Then
youtube.streams.get_by_itag.assert_called_with(123)
display_streams.assert_called_with(youtube)
@mock.patch("pytube.cli.YouTube")
@mock.patch("pytube.Stream")
def test_download_when_itag_is_found(youtube, stream):
stream.itag = 123
stream.exists_at_path.return_value = False
youtube.streams = StreamQuery([stream])
with patch.object(
youtube.streams, "get_by_itag", wraps=youtube.streams.get_by_itag
) as wrapped_itag:
cli.download_by_itag(youtube, 123)
wrapped_itag.assert_called_with(123)
youtube.register_on_progress_callback.assert_called_with(cli.on_progress)
stream.download.assert_called()
@mock.patch("pytube.cli.YouTube")
@mock.patch("pytube.Stream")
def test_display_stream(youtube, stream):
# Given
stream.itag = 123
stream.__repr__ = MagicMock(return_value="")
youtube.streams = StreamQuery([stream])
# When
cli.display_streams(youtube)
# Then
stream.__repr__.assert_called()
@mock.patch("pytube.cli._print_available_captions")
@mock.patch("pytube.cli.YouTube")
def test_download_caption_with_none(youtube, print_available):
# Given
caption = Caption(
{"url": "url1", "name": {"simpleText": "name1"}, "languageCode": "en", "vssId": ".en"}
)
youtube.captions = CaptionQuery([caption])
# When
cli.download_caption(youtube, None)
# Then
print_available.assert_called_with(youtube.captions)
@mock.patch("pytube.cli.YouTube")
def test_download_caption_with_language_found(youtube):
youtube.title = "video title"
caption = Caption(
{"url": "url1", "name": {"simpleText": "name1"}, "languageCode": "en", "vssId": ".en"}
)
caption.download = MagicMock(return_value="file_path")
youtube.captions = CaptionQuery([caption])
cli.download_caption(youtube, "en")
caption.download.assert_called_with(title="video title", output_path=None)
@mock.patch("pytube.cli._print_available_captions")
@mock.patch("pytube.cli.YouTube")
def test_download_caption_with_lang_not_found(youtube, print_available):
# Given
caption = Caption(
{"url": "url1", "name": {"simpleText": "name1"}, "languageCode": "en", "vssId": ".en"}
)
youtube.captions = CaptionQuery([caption])
# When
cli.download_caption(youtube, "blah")
# Then
print_available.assert_called_with(youtube.captions)
def test_print_available_captions(capsys):
# Given
caption1 = Caption(
{"url": "url1", "name": {"simpleText": "name1"}, "languageCode": "en", "vssId": ".en"}
)
caption2 = Caption(
{"url": "url2", "name": {"simpleText": "name2"}, "languageCode": "fr", "vssId": ".fr"}
)
query = CaptionQuery([caption1, caption2])
# When
cli._print_available_captions(query)
# Then
captured = capsys.readouterr()
assert captured.out == "Available caption codes are: en, fr\n"
def test_display_progress_bar(capsys):
cli.display_progress_bar(bytes_received=25, filesize=100, scale=0.55)
out, _ = capsys.readouterr()
assert "25.0%" in out
@mock.patch("pytube.Stream")
def test_on_progress(stream):
stream.filesize = 10
cli.display_progress_bar = MagicMock()
cli.on_progress(stream, "", 7)
cli.display_progress_bar.assert_called_once_with(3, 10)
def test_parse_args_falsey():
parser = argparse.ArgumentParser()
args = cli._parse_args(parser, ["http://youtube.com/watch?v=9bZkp7q19f0"])
assert args.url == "http://youtube.com/watch?v=9bZkp7q19f0"
assert args.build_playback_report is False
assert args.itag is None
assert args.list is False
assert args.verbose is False
def test_parse_args_truthy():
parser = argparse.ArgumentParser()
args = cli._parse_args(
parser,
[
"http://youtube.com/watch?v=9bZkp7q19f0",
"--build-playback-report",
"-c",
"en",
"-l",
"--itag=10",
"-vvv",
],
)
assert args.url == "http://youtube.com/watch?v=9bZkp7q19f0"
assert args.build_playback_report is True
assert args.itag == 10
assert args.list is True
assert args.verbose is True
@mock.patch("pytube.cli.setup_logger", return_value=None)
def test_main_logging_setup(setup_logger):
# Given
parser = argparse.ArgumentParser()
args = parse_args(parser, ["http://fakeurl", "-v"])
cli._parse_args = MagicMock(return_value=args)
# When
with pytest.raises(SystemExit):
cli.main()
# Then
setup_logger.assert_called_with(logging.DEBUG, log_filename=None)
@mock.patch("pytube.cli.YouTube", return_value=None)
def test_main_download_by_itag(youtube):
parser = argparse.ArgumentParser()
args = parse_args(
parser, ["http://youtube.com/watch?v=9bZkp7q19f0", "--itag=10"]
)
cli._parse_args = MagicMock(return_value=args)
cli.download_by_itag = MagicMock()
cli.main()
youtube.assert_called()
cli.download_by_itag.assert_called()
@mock.patch("pytube.cli.YouTube", return_value=None)
def test_main_build_playback_report(youtube):
parser = argparse.ArgumentParser()
args = parse_args(
parser,
["http://youtube.com/watch?v=9bZkp7q19f0", "--build-playback-report"],
)
cli._parse_args = MagicMock(return_value=args)
cli.build_playback_report = MagicMock()
cli.main()
youtube.assert_called()
cli.build_playback_report.assert_called()
@mock.patch("pytube.cli.YouTube", return_value=None)
def test_main_display_streams(youtube):
parser = argparse.ArgumentParser()
args = parse_args(parser, ["http://youtube.com/watch?v=9bZkp7q19f0", "-l"])
cli._parse_args = MagicMock(return_value=args)
cli.display_streams = MagicMock()
cli.main()
youtube.assert_called()
cli.display_streams.assert_called()
@mock.patch("pytube.cli.YouTube", return_value=None)
def test_main_download_caption(youtube):
parser = argparse.ArgumentParser()
args = parse_args(parser, ["http://youtube.com/watch?v=9bZkp7q19f0", "-c", "en"])
cli._parse_args = MagicMock(return_value=args)
cli.download_caption = MagicMock()
cli.main()
youtube.assert_called()
cli.download_caption.assert_called()
@mock.patch("pytube.cli.YouTube", return_value=None)
@mock.patch("pytube.cli.download_by_resolution")
def test_download_by_resolution_flag(youtube, download_by_resolution):
parser = argparse.ArgumentParser()
args = parse_args(
parser, ["http://youtube.com/watch?v=9bZkp7q19f0", "-r", "320p"]
)
cli._parse_args = MagicMock(return_value=args)
cli.main()
youtube.assert_called()
download_by_resolution.assert_called()
@mock.patch("pytube.cli.YouTube")
@mock.patch("pytube.cli.Playlist")
@mock.patch("pytube.cli._perform_args_on_youtube")
def test_download_with_playlist(perform_args_on_youtube, playlist, youtube):
# Given
cli.safe_filename = MagicMock(return_value="safe_title")
parser = argparse.ArgumentParser()
args = parse_args(parser, ["https://www.youtube.com/playlist?list=PLyn"])
cli._parse_args = MagicMock(return_value=args)
videos = [youtube]
playlist_instance = playlist.return_value
playlist_instance.videos = videos
# When
cli.main()
# Then
playlist.assert_called()
perform_args_on_youtube.assert_called_with(youtube, args)
@mock.patch("pytube.cli.YouTube")
@mock.patch("pytube.cli.Playlist")
@mock.patch("pytube.cli._perform_args_on_youtube")
def test_download_with_playlist_video_error(
perform_args_on_youtube, playlist, youtube, capsys
):
# Given
cli.safe_filename = MagicMock(return_value="safe_title")
parser = argparse.ArgumentParser()
args = parse_args(parser, ["https://www.youtube.com/playlist?list=PLyn"])
cli._parse_args = MagicMock(return_value=args)
videos = [youtube]
playlist_instance = playlist.return_value
playlist_instance.videos = videos
perform_args_on_youtube.side_effect = PytubeError()
# When
cli.main()
# Then
playlist.assert_called()
captured = capsys.readouterr()
assert "There was an error with video" in captured.out
@mock.patch("pytube.cli.YouTube")
@mock.patch("pytube.StreamQuery")
@mock.patch("pytube.Stream")
@mock.patch("pytube.cli._download")
def test_download_by_resolution(download, stream, stream_query, youtube):
# Given
stream_query.get_by_resolution.return_value = stream
youtube.streams = stream_query
# When
cli.download_by_resolution(
youtube=youtube, resolution="320p", target="test_target"
)
# Then
download.assert_called_with(stream, target="test_target")
@mock.patch("pytube.cli.YouTube")
@mock.patch("pytube.StreamQuery")
@mock.patch("pytube.cli._download")
def test_download_by_resolution_not_exists(download, stream_query, youtube):
stream_query.get_by_resolution.return_value = None
youtube.streams = stream_query
with pytest.raises(SystemExit):
cli.download_by_resolution(
youtube=youtube, resolution="DOESNT EXIST", target="test_target"
)
download.assert_not_called()
@mock.patch("pytube.Stream")
def test_download_stream_file_exists(stream, capsys):
# Given
stream.exists_at_path.return_value = True
# When
cli._download(stream=stream)
# Then
captured = capsys.readouterr()
assert "Already downloaded at" in captured.out
stream.download.assert_not_called()
@mock.patch("pytube.cli.YouTube")
@mock.patch("pytube.cli.ffmpeg_process")
def test_perform_args_should_ffmpeg_process(ffmpeg_process, youtube):
# Given
parser = argparse.ArgumentParser()
args = parse_args(
parser, ["http://youtube.com/watch?v=9bZkp7q19f0", "-f", "best"]
)
cli._parse_args = MagicMock(return_value=args)
# When
cli._perform_args_on_youtube(youtube, args)
# Then
ffmpeg_process.assert_called_with(
youtube=youtube, resolution="best", target=None
)
@mock.patch("pytube.cli.YouTube")
@mock.patch("pytube.cli._ffmpeg_downloader")
def test_ffmpeg_process_best_should_download( # noqa: PT019
_ffmpeg_downloader, youtube
):
# Given
target = "/target"
streams = MagicMock()
youtube.streams = streams
video_stream = MagicMock()
streams.filter.return_value.order_by.return_value.last.return_value = (
video_stream
)
audio_stream = MagicMock()
streams.get_audio_only.return_value = audio_stream
# When
cli.ffmpeg_process(youtube, "best", target)
# Then
_ffmpeg_downloader.assert_called_with(
audio_stream=audio_stream, video_stream=video_stream, target=target
)
@mock.patch("pytube.cli.YouTube")
@mock.patch("pytube.cli._ffmpeg_downloader")
def test_ffmpeg_process_res_should_download( # noqa: PT019
_ffmpeg_downloader, youtube
):
# Given
target = "/target"
streams = MagicMock()
youtube.streams = streams
video_stream = MagicMock()
streams.filter.return_value.first.return_value = video_stream
audio_stream = MagicMock()
streams.get_audio_only.return_value = audio_stream
# When
cli.ffmpeg_process(youtube, "XYZp", target)
# Then
_ffmpeg_downloader.assert_called_with(
audio_stream=audio_stream, video_stream=video_stream, target=target
)
@mock.patch("pytube.cli.YouTube")
@mock.patch("pytube.cli._ffmpeg_downloader")
def test_ffmpeg_process_res_none_should_not_download( # noqa: PT019
_ffmpeg_downloader, youtube
):
# Given
target = "/target"
streams = MagicMock()
youtube.streams = streams
streams.filter.return_value.first.return_value = None
audio_stream = MagicMock()
streams.get_audio_only.return_value = audio_stream
# When
with pytest.raises(SystemExit):
cli.ffmpeg_process(youtube, "XYZp", target)
# Then
_ffmpeg_downloader.assert_not_called()
@mock.patch("pytube.cli.YouTube")
@mock.patch("pytube.cli._ffmpeg_downloader")
def test_ffmpeg_process_audio_none_should_fallback_download( # noqa: PT019
_ffmpeg_downloader, youtube
):
# Given
target = "/target"
streams = MagicMock()
youtube.streams = streams
stream = MagicMock()
streams.filter.return_value.order_by.return_value.last.return_value = stream
streams.get_audio_only.return_value = None
# When
cli.ffmpeg_process(youtube, "best", target)
# Then
_ffmpeg_downloader.assert_called_with(
audio_stream=stream, video_stream=stream, target=target
)
@mock.patch("pytube.cli.YouTube")
@mock.patch("pytube.cli._ffmpeg_downloader")
def test_ffmpeg_process_audio_fallback_none_should_exit( # noqa: PT019
_ffmpeg_downloader, youtube
):
# Given
target = "/target"
streams = MagicMock()
youtube.streams = streams
stream = MagicMock()
streams.filter.return_value.order_by.return_value.last.side_effect = [
stream,
stream,
None,
]
streams.get_audio_only.return_value = None
# When
with pytest.raises(SystemExit):
cli.ffmpeg_process(youtube, "best", target)
# Then
_ffmpeg_downloader.assert_not_called()
@mock.patch("pytube.cli.os.unlink", return_value=None)
@mock.patch("pytube.cli.subprocess.run", return_value=None)
@mock.patch("pytube.cli._download", return_value=None)
@mock.patch("pytube.cli._unique_name", return_value=None)
def test_ffmpeg_downloader(unique_name, download, run, unlink):
# Given
target = "target"
audio_stream = MagicMock()
video_stream = MagicMock()
video_stream.id = "video_id"
audio_stream.subtype = "audio_subtype"
video_stream.subtype = "video_subtype"
unique_name.side_effect = ["video_name", "audio_name"]
# When
cli._ffmpeg_downloader(
audio_stream=audio_stream, video_stream=video_stream, target=target
)
# Then
download.assert_called()
run.assert_called_with(
[
"ffmpeg",
"-i",
os.path.join("target", "video_name.video_subtype"),
"-i",
os.path.join("target", "audio_name.audio_subtype"),
"-codec",
"copy",
os.path.join("target", "safe_title.video_subtype"),
]
)
unlink.assert_called()
@mock.patch("pytube.cli.download_audio")
@mock.patch("pytube.cli.YouTube.__init__", return_value=None)
def test_download_audio_args(youtube, download_audio):
# Given
parser = argparse.ArgumentParser()
args = parse_args(
parser, ["http://youtube.com/watch?v=9bZkp7q19f0", "-a", "mp4"]
)
cli._parse_args = MagicMock(return_value=args)
# When
cli.main()
# Then
youtube.assert_called()
download_audio.assert_called()
@mock.patch("pytube.cli._download")
@mock.patch("pytube.cli.YouTube")
def test_download_audio(youtube, download):
# Given
youtube_instance = youtube.return_value
audio_stream = MagicMock()
youtube_instance.streams.filter.return_value.order_by.return_value.last.return_value = (
audio_stream
)
# When
cli.download_audio(youtube_instance, "filetype", "target")
# Then
download.assert_called_with(audio_stream, target="target")
@mock.patch("pytube.cli._download")
@mock.patch("pytube.cli.YouTube")
def test_download_audio_none(youtube, download):
# Given
youtube_instance = youtube.return_value
youtube_instance.streams.filter.return_value.order_by.return_value.last.return_value = (
None
)
# When
with pytest.raises(SystemExit):
cli.download_audio(youtube_instance, "filetype", "target")
# Then
download.assert_not_called()
@mock.patch("pytube.cli.YouTube.__init__", return_value=None)
def test_perform_args_on_youtube(youtube):
parser = argparse.ArgumentParser()
args = parse_args(parser, ["http://youtube.com/watch?v=9bZkp7q19f0"])
cli._parse_args = MagicMock(return_value=args)
cli._perform_args_on_youtube = MagicMock()
cli.main()
youtube.assert_called()
cli._perform_args_on_youtube.assert_called()
@mock.patch("pytube.cli.os.path.exists", return_value=False)
def test_unique_name(path_exists):
assert (
cli._unique_name("base", "subtype", "video", "target") == "base_video_0"
)
@mock.patch("pytube.cli.os.path.exists")
def test_unique_name_counter(path_exists):
path_exists.side_effect = [True, False]
assert (
cli._unique_name("base", "subtype", "video", "target") == "base_video_1"
)
|