Base VReddit class off of Youtube class

This commit is contained in:
Serene-Arc
2022-07-15 15:12:39 +10:00
parent 7d4eb47643
commit 9277903308
2 changed files with 10 additions and 38 deletions

View File

@@ -11,12 +11,12 @@ from praw.models import Submission
from bdfr.exceptions import NotADownloadableLinkError, 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.youtube import Youtube
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
class VReddit(BaseDownloader): class VReddit(Youtube):
def __init__(self, post: Submission): def __init__(self, post: Submission):
super().__init__(post) super().__init__(post)
@@ -30,47 +30,14 @@ class VReddit(BaseDownloader):
res = Resource(self.post, self.post.url, download_function, extension) res = Resource(self.post, self.post.url, download_function, extension)
return [res] return [res]
def _download_video(self, ytdl_options: dict) -> Callable:
yt_logger = logging.getLogger('youtube-dl')
yt_logger.setLevel(logging.CRITICAL)
ytdl_options['quiet'] = True
ytdl_options['logger'] = yt_logger
def download(_: dict) -> bytes:
with tempfile.TemporaryDirectory() as temp_dir:
download_path = Path(temp_dir).resolve()
ytdl_options['outtmpl'] = str(download_path) + '/' + 'test.%(ext)s'
try:
with yt_dlp.YoutubeDL(ytdl_options) as ydl:
ydl.download([self.post.url])
except yt_dlp.DownloadError as e:
raise SiteDownloaderError(f'Vreddit download failed: {e}')
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}")
with open(downloaded_file, 'rb') as file:
content = file.read()
return content
return download
@staticmethod @staticmethod
def get_video_attributes(url: str) -> dict: def get_video_attributes(url: str) -> dict:
yt_logger = logging.getLogger('youtube-dl') result = VReddit.get_video_data(url)
yt_logger.setLevel(logging.CRITICAL)
with yt_dlp.YoutubeDL({'logger': yt_logger, }) as ydl:
try:
result = ydl.extract_info(url, download=False)
except Exception as e:
logger.exception(e)
raise NotADownloadableLinkError(f'Video info extraction failed for {url}')
if 'ext' in result: if 'ext' in result:
return result return result
else: else:
try: try:
result = (result["entries"][0]) result = result["entries"][0]
return result return result
except Exception as e: except Exception as e:
logger.exception(e) logger.exception(e)

View File

@@ -58,7 +58,7 @@ class Youtube(BaseDownloader):
return download return download
@staticmethod @staticmethod
def get_video_attributes(url: str) -> dict: def get_video_data(url: str) -> dict:
yt_logger = logging.getLogger('youtube-dl') yt_logger = logging.getLogger('youtube-dl')
yt_logger.setLevel(logging.CRITICAL) yt_logger.setLevel(logging.CRITICAL)
with yt_dlp.YoutubeDL({'logger': yt_logger, }) as ydl: with yt_dlp.YoutubeDL({'logger': yt_logger, }) as ydl:
@@ -67,6 +67,11 @@ class Youtube(BaseDownloader):
except Exception as e: except Exception as e:
logger.exception(e) logger.exception(e)
raise NotADownloadableLinkError(f'Video info extraction failed for {url}') raise NotADownloadableLinkError(f'Video info extraction failed for {url}')
return result
@staticmethod
def get_video_attributes(url: str) -> dict:
result = Youtube.get_video_data(url)
if 'ext' in result: if 'ext' in result:
return result return result
else: else: