2021-02-07 17:46:20 +13:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import logging
|
2021-02-11 12:10:40 +13:00
|
|
|
import tempfile
|
2021-02-28 17:52:20 +13:00
|
|
|
from pathlib import Path
|
2021-07-27 15:39:49 +12:00
|
|
|
from typing import Callable, Optional
|
2020-06-02 00:05:02 +12:00
|
|
|
|
2021-10-02 15:23:13 +13:00
|
|
|
import yt_dlp
|
2021-02-11 12:10:40 +13:00
|
|
|
from praw.models import Submission
|
2020-06-02 00:05:02 +12:00
|
|
|
|
2021-07-27 15:39:49 +12:00
|
|
|
from bdfr.exceptions import NotADownloadableLinkError, SiteDownloaderError
|
2021-04-12 19:58:32 +12:00
|
|
|
from bdfr.resource import Resource
|
|
|
|
from bdfr.site_authenticator import SiteAuthenticator
|
|
|
|
from bdfr.site_downloaders.base_downloader import BaseDownloader
|
2021-02-07 17:46:20 +13:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
2021-02-06 21:35:50 +13:00
|
|
|
|
2020-06-02 00:05:02 +12:00
|
|
|
|
2021-02-07 14:33:19 +13:00
|
|
|
class Youtube(BaseDownloader):
|
2021-02-15 18:12:27 +13:00
|
|
|
def __init__(self, post: Submission):
|
|
|
|
super().__init__(post)
|
2020-06-02 00:05:02 +12:00
|
|
|
|
2021-02-26 21:57:05 +13:00
|
|
|
def find_resources(self, authenticator: Optional[SiteAuthenticator] = None) -> list[Resource]:
|
2021-03-01 17:50:31 +13:00
|
|
|
ytdl_options = {
|
2021-04-05 17:57:21 +12:00
|
|
|
'format': 'best',
|
|
|
|
'playlistend': 1,
|
|
|
|
'nooverwrites': True,
|
2021-03-01 17:50:31 +13:00
|
|
|
}
|
2021-07-27 15:39:49 +12:00
|
|
|
download_function = self._download_video(ytdl_options)
|
2021-11-21 14:48:29 +13:00
|
|
|
extension = self.get_video_attributes(self.post.url)['ext']
|
2021-07-27 15:39:49 +12:00
|
|
|
res = Resource(self.post, self.post.url, download_function, extension)
|
|
|
|
return [res]
|
2021-03-01 17:50:31 +13:00
|
|
|
|
2021-07-27 15:39:49 +12:00
|
|
|
def _download_video(self, ytdl_options: dict) -> Callable:
|
2021-05-02 21:49:32 +12:00
|
|
|
yt_logger = logging.getLogger('youtube-dl')
|
|
|
|
yt_logger.setLevel(logging.CRITICAL)
|
2021-03-01 17:50:31 +13:00
|
|
|
ytdl_options['quiet'] = True
|
2021-05-02 21:49:32 +12:00
|
|
|
ytdl_options['logger'] = yt_logger
|
2021-07-27 15:39:49 +12:00
|
|
|
|
2021-07-29 21:10:10 +12:00
|
|
|
def download(_: dict) -> bytes:
|
2021-07-27 15:39:49 +12:00
|
|
|
with tempfile.TemporaryDirectory() as temp_dir:
|
|
|
|
download_path = Path(temp_dir).resolve()
|
|
|
|
ytdl_options['outtmpl'] = str(download_path) + '/' + 'test.%(ext)s'
|
|
|
|
try:
|
2021-10-02 15:23:13 +13:00
|
|
|
with yt_dlp.YoutubeDL(ytdl_options) as ydl:
|
2021-07-27 15:39:49 +12:00
|
|
|
ydl.download([self.post.url])
|
2021-10-02 15:23:13 +13:00
|
|
|
except yt_dlp.DownloadError as e:
|
2021-07-27 15:39:49 +12:00
|
|
|
raise SiteDownloaderError(f'Youtube 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
|
|
|
|
def get_video_attributes(url: str) -> dict:
|
|
|
|
yt_logger = logging.getLogger('youtube-dl')
|
|
|
|
yt_logger.setLevel(logging.CRITICAL)
|
2021-10-02 15:23:13 +13:00
|
|
|
with yt_dlp.YoutubeDL({'logger': yt_logger, }) as ydl:
|
2021-03-13 15:01:30 +13:00
|
|
|
try:
|
2021-07-27 15:39:49 +12:00
|
|
|
result = ydl.extract_info(url, download=False)
|
|
|
|
except Exception as e:
|
|
|
|
logger.exception(e)
|
2021-11-21 14:48:29 +13:00
|
|
|
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}')
|