vreddit.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #!/usr/bin/env python3
  2. import logging
  3. import tempfile
  4. from pathlib import Path
  5. from typing import Callable, Optional
  6. import yt_dlp
  7. from praw.models import Submission
  8. from bdfr.exceptions import NotADownloadableLinkError, SiteDownloaderError
  9. from bdfr.resource import Resource
  10. from bdfr.site_authenticator import SiteAuthenticator
  11. from bdfr.site_downloaders.youtube import Youtube
  12. logger = logging.getLogger(__name__)
  13. class VReddit(Youtube):
  14. def __init__(self, post: Submission):
  15. super().__init__(post)
  16. def find_resources(self, authenticator: Optional[SiteAuthenticator] = None) -> list[Resource]:
  17. ytdl_options = {
  18. 'playlistend': 1,
  19. 'nooverwrites': True,
  20. }
  21. download_function = self._download_video(ytdl_options)
  22. extension = self.get_video_attributes(self.post.url)['ext']
  23. res = Resource(self.post, self.post.url, download_function, extension)
  24. return [res]
  25. @staticmethod
  26. def get_video_attributes(url: str) -> dict:
  27. result = VReddit.get_video_data(url)
  28. if 'ext' in result:
  29. return result
  30. else:
  31. try:
  32. result = result["entries"][0]
  33. return result
  34. except Exception as e:
  35. logger.exception(e)
  36. raise NotADownloadableLinkError(f'Video info extraction failed for {url}')