Fix bug with youtube class and children

This commit is contained in:
Serene-Arc
2021-11-21 11:48:29 +10:00
parent 53562f4873
commit 17939fe47c
3 changed files with 22 additions and 8 deletions

View File

@@ -6,6 +6,7 @@ from typing import Optional
from praw.models import Submission
from bdfr.exceptions import NotADownloadableLinkError
from bdfr.resource import Resource
from bdfr.site_authenticator import SiteAuthenticator
from bdfr.site_downloaders.fallback_downloaders.fallback_downloader import BaseFallbackDownloader
@@ -29,8 +30,9 @@ class YoutubeDlFallback(BaseFallbackDownloader, Youtube):
@staticmethod
def can_handle_link(url: str) -> bool:
try:
attributes = YoutubeDlFallback.get_video_attributes(url)
except NotADownloadableLinkError:
return False
if attributes:
return True
else:
return False

View File

@@ -27,10 +27,7 @@ class Youtube(BaseDownloader):
'nooverwrites': True,
}
download_function = self._download_video(ytdl_options)
try:
extension = self.get_video_attributes(self.post.url)['ext']
except KeyError:
raise NotADownloadableLinkError(f'Youtube-DL cannot download URL {self.post.url}')
res = Resource(self.post, self.post.url, download_function, extension)
return [res]
@@ -67,6 +64,10 @@ class Youtube(BaseDownloader):
with yt_dlp.YoutubeDL({'logger': yt_logger, }) as ydl:
try:
result = ydl.extract_info(url, download=False)
return result
except Exception as e:
logger.exception(e)
raise NotADownloadableLinkError(f'Video info extraction failed for {url}')
if 'ext' in result:
return result
else:
raise NotADownloadableLinkError(f'Video info extraction failed for {url}')

View File

@@ -4,6 +4,7 @@ from unittest.mock import MagicMock
import pytest
from bdfr.exceptions import NotADownloadableLinkError
from bdfr.resource import Resource
from bdfr.site_downloaders.fallback_downloaders.youtubedl_fallback import YoutubeDlFallback
@@ -13,12 +14,22 @@ from bdfr.site_downloaders.fallback_downloaders.youtubedl_fallback import Youtub
('https://www.reddit.com/r/specializedtools/comments/n2nw5m/bamboo_splitter/', True),
('https://www.youtube.com/watch?v=P19nvJOmqCc', True),
('https://www.example.com/test', False),
('https://milesmatrix.bandcamp.com/album/la-boum/', False),
))
def test_can_handle_link(test_url: str, expected: bool):
result = YoutubeDlFallback.can_handle_link(test_url)
assert result == expected
@pytest.mark.online
@pytest.mark.parametrize('test_url', (
'https://milesmatrix.bandcamp.com/album/la-boum/',
))
def test_info_extraction_bad(test_url: str):
with pytest.raises(NotADownloadableLinkError):
YoutubeDlFallback.get_video_attributes(test_url)
@pytest.mark.online
@pytest.mark.slow
@pytest.mark.parametrize(('test_url', 'expected_hash'), (