1
0
Fork 0
mirror of synced 2024-05-18 19:22:38 +12:00

bug(youtube.dl): Fix crash on zero downloads #375

This commit is contained in:
Ali Parlakci 2021-05-15 14:35:16 +03:00 committed by Serene
parent aea30d2b44
commit c7a5ec4376
2 changed files with 22 additions and 3 deletions

View file

@ -8,7 +8,7 @@ from typing import Optional
import youtube_dl
from praw.models import Submission
from bdfr.exceptions import SiteDownloaderError
from bdfr.exceptions import (NotADownloadableLinkError, SiteDownloaderError)
from bdfr.resource import Resource
from bdfr.site_authenticator import SiteAuthenticator
from bdfr.site_downloaders.base_downloader import BaseDownloader
@ -43,7 +43,12 @@ class Youtube(BaseDownloader):
except youtube_dl.DownloadError as 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
with open(downloaded_file, 'rb') as file:
content = file.read()

View file

@ -5,6 +5,7 @@ from unittest.mock import MagicMock
import pytest
from bdfr.exceptions import NotADownloadableLinkError
from bdfr.resource import Resource
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=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.url = test_url
downloader = Youtube(test_submission)
@ -24,3 +25,16 @@ def test_find_resources(test_url: str, expected_hash: str):
assert isinstance(resources[0], Resource)
resources[0].download(120)
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()