|
1
|
|
|
import os |
|
2
|
|
|
from glob import glob |
|
3
|
|
|
|
|
4
|
|
|
import pytest |
|
5
|
|
|
|
|
6
|
|
|
from music_album_creation.downloading import ( |
|
7
|
|
|
CertificateVerificationError, |
|
8
|
|
|
CMDYoutubeDownloader, |
|
9
|
|
|
) |
|
10
|
|
|
from music_album_creation.web_parsing import video_title |
|
11
|
|
|
|
|
12
|
|
|
NON_EXISTANT_YOUTUBE_URL = 'https://www.youtube.com/watch?v=alpharegavgavasdfsh' |
|
13
|
|
|
INVALID_URL = 'gav' |
|
14
|
|
|
duration = '3:43' |
|
15
|
|
|
duration_in_seconds = 223 |
|
16
|
|
|
|
|
17
|
|
|
|
|
18
|
|
|
@pytest.fixture(scope='module') |
|
19
|
|
|
def download(): |
|
20
|
|
|
youtue = CMDYoutubeDownloader() |
|
21
|
|
|
return lambda url, target_directory, times, suppress_certificate_validation: youtue.download_trials( |
|
22
|
|
|
url, |
|
23
|
|
|
target_directory, |
|
24
|
|
|
times=times, |
|
25
|
|
|
suppress_certificate_validation=suppress_certificate_validation, |
|
26
|
|
|
delay=0.8, |
|
27
|
|
|
) |
|
28
|
|
|
|
|
29
|
|
|
|
|
30
|
|
|
@pytest.mark.network_bound |
|
31
|
|
|
@pytest.mark.parametrize( |
|
32
|
|
|
'url, target_file', |
|
33
|
|
|
[ |
|
34
|
|
|
( |
|
35
|
|
|
'https://www.youtube.com/watch?v=Q3dvbM6Pias', |
|
36
|
|
|
'Rage Against The Machine - Testify (Official HD Video).mp4', |
|
37
|
|
|
), |
|
38
|
|
|
('https://www.youtube.com/watch?v=bj1JRuyYeco', '20 Second Timer (Minimal).webm'), |
|
39
|
|
|
], |
|
40
|
|
|
) |
|
41
|
|
|
def test_downloading_valid_youtube_url(url, target_file, tmp_path_factory): |
|
42
|
|
|
from pathlib import Path |
|
43
|
|
|
|
|
44
|
|
|
from music_album_creation.downloading import CMDYoutubeDownloader |
|
45
|
|
|
|
|
46
|
|
|
target_directory = tmp_path_factory.mktemp("youtubedownloads") |
|
47
|
|
|
target_directory = str(target_directory) |
|
48
|
|
|
|
|
49
|
|
|
expected_file_name = target_file |
|
50
|
|
|
|
|
51
|
|
|
youtube = CMDYoutubeDownloader() |
|
52
|
|
|
downloaded_file = youtube.download_trials( |
|
53
|
|
|
url, |
|
54
|
|
|
target_directory, |
|
55
|
|
|
times=5, |
|
56
|
|
|
delay=0.8, |
|
57
|
|
|
) |
|
58
|
|
|
|
|
59
|
|
|
downloaded_path: Path = Path(str(downloaded_file)) |
|
60
|
|
|
|
|
61
|
|
|
# THEN File has been downloaded |
|
62
|
|
|
assert downloaded_path.exists() |
|
63
|
|
|
assert downloaded_path.is_file() |
|
64
|
|
|
|
|
65
|
|
|
# AND FILE has the expected name |
|
66
|
|
|
assert downloaded_path.name == expected_file_name |
|
67
|
|
|
assert downloaded_path.suffix == '.' + expected_file_name.split('.')[-1] |
|
68
|
|
|
|
|
69
|
|
|
|
|
70
|
|
|
# Test that expected Exceptions are fired up |
|
71
|
|
|
|
|
72
|
|
|
|
|
73
|
|
|
@pytest.mark.network_bound |
|
74
|
|
|
def test_downloading_false_youtube_url(tmp_path_factory): |
|
75
|
|
|
from pytube.exceptions import VideoUnavailable |
|
76
|
|
|
|
|
77
|
|
|
from music_album_creation.downloading import CMDYoutubeDownloader |
|
78
|
|
|
|
|
79
|
|
|
youtube = CMDYoutubeDownloader() |
|
80
|
|
|
|
|
81
|
|
|
with pytest.raises(VideoUnavailable): |
|
82
|
|
|
youtube.download_trials( |
|
83
|
|
|
NON_EXISTANT_YOUTUBE_URL, |
|
84
|
|
|
str(tmp_path_factory.mktemp("unit-test")), |
|
85
|
|
|
times=3, |
|
86
|
|
|
delay=0.8, |
|
87
|
|
|
) |
|
88
|
|
|
|
|
89
|
|
|
|
|
90
|
|
|
@pytest.mark.network_bound |
|
91
|
|
|
def test_downloading_invalid_url(download, tmp_path_factory): |
|
92
|
|
|
from pytube.exceptions import RegexMatchError |
|
93
|
|
|
|
|
94
|
|
|
from music_album_creation.downloading import CMDYoutubeDownloader |
|
95
|
|
|
|
|
96
|
|
|
youtube = CMDYoutubeDownloader() |
|
97
|
|
|
|
|
98
|
|
|
with pytest.raises(RegexMatchError): |
|
99
|
|
|
youtube.download_trials( |
|
100
|
|
|
INVALID_URL, |
|
101
|
|
|
str(tmp_path_factory.mktemp("unit-test")), |
|
102
|
|
|
times=1, |
|
103
|
|
|
delay=0.8, |
|
104
|
|
|
) |
|
105
|
|
|
|