1
|
|
|
import os |
2
|
|
|
from glob import glob |
3
|
|
|
import pytest |
4
|
|
|
|
5
|
|
|
from music_album_creation.downloading import CMDYoutubeDownloader, InvalidUrlError, UnavailableVideoError, CertificateVerificationError |
6
|
|
|
from music_album_creation.web_parsing import video_title |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
@pytest.fixture(scope='module') |
10
|
|
|
def download(): |
11
|
|
|
youtue = CMDYoutubeDownloader() |
12
|
|
|
return lambda url, target_directory, times, suppress_certificate_validation: youtue.download_trials(url, target_directory, |
13
|
|
|
times=10, |
14
|
|
|
suppress_certificate_validation=suppress_certificate_validation, |
15
|
|
|
delay=0.8) |
16
|
|
|
@pytest.fixture(scope='module') |
17
|
|
|
def download_trials(): |
18
|
|
|
return 15 |
19
|
|
|
|
20
|
|
|
|
21
|
|
|
|
22
|
|
|
class TestYoutubeDownloader: |
23
|
|
|
NON_EXISTANT_YOUTUBE_URL = 'https://www.youtube.com/watch?v=alpharegavgav' |
24
|
|
|
INVALID_URL = 'gav' |
25
|
|
|
duration = '3:43' |
26
|
|
|
duration_in_seconds = 223 |
27
|
|
|
|
28
|
|
|
def download_trial(self, url, directory, download_callback, nb_trials): |
29
|
|
|
try: |
30
|
|
|
download_callback(url, directory, times=1, suppress_certificate_validation=False) |
31
|
|
|
except CertificateVerificationError: |
32
|
|
|
download_callback(url, directory, times=nb_trials, suppress_certificate_validation=True) |
33
|
|
|
|
34
|
|
|
@pytest.mark.parametrize("url, target_file", [ |
35
|
|
|
('https://www.youtube.com/watch?v=Q3dvbM6Pias', 'Rage Against The Machine - Testify (Official Video).mp3')]) |
36
|
|
|
def test_downloading_valid_youtube_url(self, url, target_file, tmpdir, download, download_trials): |
37
|
|
|
target_directory = str(tmpdir.mkdir('youtubedownloads')) |
38
|
|
|
self.download_trial(url, target_directory, download, download_trials) |
39
|
|
|
assert len(glob('{}/*'.format(target_directory))) == 1 |
40
|
|
|
assert os.path.isfile(os.path.join(target_directory, target_file)) or os.path.isfile(os.path.join(target_directory, '_.mp3')) |
41
|
|
|
|
42
|
|
|
def test_downloading_false_youtube_url(self, download, download_trials): |
43
|
|
|
with pytest.raises(UnavailableVideoError): |
44
|
|
|
self.download_trial(self.NON_EXISTANT_YOUTUBE_URL, '/tmp', download, download_trials) |
45
|
|
|
|
46
|
|
|
def test_downloading_invalid_url(self, download): |
47
|
|
|
with pytest.raises(InvalidUrlError): |
48
|
|
|
download(self.INVALID_URL, '/tmp/', 1, False) |
49
|
|
|
|
50
|
|
|
|
51
|
|
|
@pytest.mark.parametrize('url', 'title', [ |
52
|
|
|
('https://www.youtube.com/watch?v=gkbJLLW6xKo', 'Planet Of Zeus - Faith In Physics (2019) (New Full Album)'), |
53
|
|
|
# pytest.param('a_false_url', marks=pytest.mark.xfail) |
54
|
|
|
]) |
55
|
|
|
def test_backup_youtube_video_title(url, title): |
56
|
|
|
assert video_title(url) == title |
57
|
|
|
|
58
|
|
|
|