Add fallback downloader

This commit is contained in:
Serene-Arc
2021-05-02 19:48:25 +10:00
committed by Serene
parent a86a41e6a5
commit a8c2136270
7 changed files with 99 additions and 1 deletions

View File

@@ -9,6 +9,7 @@ from bdfr.exceptions import NotADownloadableLinkError
from bdfr.site_downloaders.base_downloader import BaseDownloader
from bdfr.site_downloaders.direct import Direct
from bdfr.site_downloaders.erome import Erome
from bdfr.site_downloaders.fallback_downloaders.youtubedl_fallback import YoutubeDlFallback
from bdfr.site_downloaders.gallery import Gallery
from bdfr.site_downloaders.gfycat import Gfycat
from bdfr.site_downloaders.imgur import Imgur
@@ -47,6 +48,8 @@ class DownloadFactory:
return Streamable
elif re.match(r'i\.redd\.it.*', sanitised_url):
return Direct
elif YoutubeDlFallback.can_handle_link(sanitised_url):
return YoutubeDlFallback
else:
raise NotADownloadableLinkError(
f'No downloader module exists for url {url}')

View File

@@ -0,0 +1,15 @@
#!/usr/bin/env python3
# coding=utf-8
from abc import ABC, abstractmethod
from bdfr.site_downloaders.base_downloader import BaseDownloader
class BaseFallbackDownloader(BaseDownloader, ABC):
@staticmethod
@abstractmethod
def can_handle_link(url: str) -> bool:
"""Returns whether the fallback downloader can download this link"""
raise NotImplementedError

View File

@@ -0,0 +1,40 @@
#!/usr/bin/env python3
# coding=utf-8
import logging
from typing import Optional
import youtube_dl
from praw.models import Submission
from bdfr.resource import Resource
from bdfr.site_authenticator import SiteAuthenticator
from bdfr.site_downloaders.fallback_downloaders.fallback_downloader import BaseFallbackDownloader
from bdfr.site_downloaders.youtube import Youtube
logger = logging.getLogger(__name__)
class YoutubeDlFallback(BaseFallbackDownloader, Youtube):
def __init__(self, post: Submission):
super(YoutubeDlFallback, self).__init__(post)
def find_resources(self, authenticator: Optional[SiteAuthenticator] = None) -> list[Resource]:
out = super()._download_video({})
return [out]
@staticmethod
def can_handle_link(url: str) -> bool:
yt_logger = logging.getLogger('youtube-dl')
yt_logger.setLevel(logging.CRITICAL)
with youtube_dl.YoutubeDL({
'logger': yt_logger,
}) as ydl:
try:
result = ydl.extract_info(url, download=False)
if result:
return True
except youtube_dl.DownloadError as e:
logger.exception(e)
return False
return False