示例#1
0
        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'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
    def _download_video(self, ytdl_options: dict) -> Resource:
        ytdl_options['quiet'] = True
        with tempfile.TemporaryDirectory() as temp_dir:
            download_path = Path(temp_dir).resolve()
            ytdl_options['outtmpl'] = str(download_path) + '/' + 'test.%(ext)s'
            try:
                with youtube_dl.YoutubeDL(ytdl_options) as ydl:
                    ydl.download([self.post.url])
            except youtube_dl.DownloadError as e:
                raise SiteDownloaderError(f'Youtube download failed: {e}')

            downloaded_file = list(download_path.iterdir())[0]
            extension = downloaded_file.suffix
            with open(downloaded_file, 'rb') as file:
                content = file.read()
        out = Resource(self.post, self.post.url, extension)
        out.content = content
        out.create_hash()
        return out
from bdfr.exceptions import SiteDownloaderError
from bdfr.resource import Resource
from bdfr.site_authenticator import SiteAuthenticator
from bdfr.site_downloaders.youtube import Youtube

logger = logging.getLogger(__name__)


class PornHub(Youtube):
    def __init__(self, post: Submission):
        super().__init__(post)

    def find_resources(self, authenticator: Optional[SiteAuthenticator] = None) -> list[Resource]:
        ytdl_options = {
            'format': 'best',
            'nooverwrites': True,
        }
        if video_attributes := super().get_video_attributes(self.post.url):
            extension = video_attributes['ext']
        else:
            raise SiteDownloaderError()

        out = Resource(
            self.post,
            self.post.url,
            super()._download_video(ytdl_options),
            extension,
        )
        return [out]