bug(youtube.dl): Fix crash on zero downloads #375
This commit is contained in:
parent
aea30d2b44
commit
c7a5ec4376
2 changed files with 22 additions and 3 deletions
|
@ -8,7 +8,7 @@ from typing import Optional
|
||||||
import youtube_dl
|
import youtube_dl
|
||||||
from praw.models import Submission
|
from praw.models import Submission
|
||||||
|
|
||||||
from bdfr.exceptions import SiteDownloaderError
|
from bdfr.exceptions import (NotADownloadableLinkError, SiteDownloaderError)
|
||||||
from bdfr.resource import Resource
|
from bdfr.resource import Resource
|
||||||
from bdfr.site_authenticator import SiteAuthenticator
|
from bdfr.site_authenticator import SiteAuthenticator
|
||||||
from bdfr.site_downloaders.base_downloader import BaseDownloader
|
from bdfr.site_downloaders.base_downloader import BaseDownloader
|
||||||
|
@ -43,7 +43,12 @@ class Youtube(BaseDownloader):
|
||||||
except youtube_dl.DownloadError as e:
|
except youtube_dl.DownloadError as e:
|
||||||
raise SiteDownloaderError(f'Youtube download failed: {e}')
|
raise SiteDownloaderError(f'Youtube download failed: {e}')
|
||||||
|
|
||||||
downloaded_file = list(download_path.iterdir())[0]
|
downloaded_file = None
|
||||||
|
downloaded_files = list(download_path.iterdir())
|
||||||
|
if len(downloaded_files) > 0:
|
||||||
|
downloaded_file = downloaded_files[0]
|
||||||
|
else:
|
||||||
|
raise NotADownloadableLinkError(f"No media exists in the URL {self.post.url}")
|
||||||
extension = downloaded_file.suffix
|
extension = downloaded_file.suffix
|
||||||
with open(downloaded_file, 'rb') as file:
|
with open(downloaded_file, 'rb') as file:
|
||||||
content = file.read()
|
content = file.read()
|
||||||
|
|
|
@ -5,6 +5,7 @@ from unittest.mock import MagicMock
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
|
from bdfr.exceptions import NotADownloadableLinkError
|
||||||
from bdfr.resource import Resource
|
from bdfr.resource import Resource
|
||||||
from bdfr.site_downloaders.youtube import Youtube
|
from bdfr.site_downloaders.youtube import Youtube
|
||||||
|
|
||||||
|
@ -15,7 +16,7 @@ from bdfr.site_downloaders.youtube import Youtube
|
||||||
('https://www.youtube.com/watch?v=uSm2VDgRIUs', 'f70b704b4b78b9bb5cd032bfc26e4971'),
|
('https://www.youtube.com/watch?v=uSm2VDgRIUs', 'f70b704b4b78b9bb5cd032bfc26e4971'),
|
||||||
('https://www.youtube.com/watch?v=m-tKnjFwleU', '30314930d853afff8ebc7d8c36a5b833'),
|
('https://www.youtube.com/watch?v=m-tKnjFwleU', '30314930d853afff8ebc7d8c36a5b833'),
|
||||||
))
|
))
|
||||||
def test_find_resources(test_url: str, expected_hash: str):
|
def test_find_resources_good(test_url: str, expected_hash: str):
|
||||||
test_submission = MagicMock()
|
test_submission = MagicMock()
|
||||||
test_submission.url = test_url
|
test_submission.url = test_url
|
||||||
downloader = Youtube(test_submission)
|
downloader = Youtube(test_submission)
|
||||||
|
@ -24,3 +25,16 @@ def test_find_resources(test_url: str, expected_hash: str):
|
||||||
assert isinstance(resources[0], Resource)
|
assert isinstance(resources[0], Resource)
|
||||||
resources[0].download(120)
|
resources[0].download(120)
|
||||||
assert resources[0].hash.hexdigest() == expected_hash
|
assert resources[0].hash.hexdigest() == expected_hash
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.online
|
||||||
|
@pytest.mark.slow
|
||||||
|
@pytest.mark.parametrize(('test_url'), (
|
||||||
|
('https://www.polygon.com/disney-plus/2020/5/14/21249881/gargoyles-animated-series-disney-plus-greg-weisman-interview-oj-simpson-goliath-chronicles'),
|
||||||
|
))
|
||||||
|
def test_find_resources_bad(test_url: str):
|
||||||
|
test_submission = MagicMock()
|
||||||
|
test_submission.url = test_url
|
||||||
|
downloader = Youtube(test_submission)
|
||||||
|
with pytest.raises(NotADownloadableLinkError):
|
||||||
|
downloader.find_resources()
|
||||||
|
|
Loading…
Reference in a new issue