2022-04-26 04:09:09 +12:00
|
|
|
#!/usr/bin/env python3
|
2022-12-20 12:32:37 +13:00
|
|
|
# -*- coding: utf-8 -*-
|
2022-04-26 04:09:09 +12:00
|
|
|
|
|
|
|
import logging
|
2022-12-29 04:00:43 +13:00
|
|
|
from typing import Optional
|
2022-04-26 04:09:09 +12:00
|
|
|
|
|
|
|
from praw.models import Submission
|
|
|
|
|
2022-12-29 04:00:43 +13:00
|
|
|
from bdfr.exceptions import NotADownloadableLinkError
|
2022-04-26 04:09:09 +12:00
|
|
|
from bdfr.resource import Resource
|
|
|
|
from bdfr.site_authenticator import SiteAuthenticator
|
2022-07-15 17:12:39 +12:00
|
|
|
from bdfr.site_downloaders.youtube import Youtube
|
2022-04-26 04:09:09 +12:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2022-07-15 17:12:39 +12:00
|
|
|
class VReddit(Youtube):
|
2022-04-26 04:09:09 +12:00
|
|
|
def __init__(self, post: Submission):
|
|
|
|
super().__init__(post)
|
|
|
|
|
|
|
|
def find_resources(self, authenticator: Optional[SiteAuthenticator] = None) -> list[Resource]:
|
|
|
|
ytdl_options = {
|
2022-12-03 18:11:17 +13:00
|
|
|
"playlistend": 1,
|
|
|
|
"nooverwrites": True,
|
2022-04-26 04:09:09 +12:00
|
|
|
}
|
|
|
|
download_function = self._download_video(ytdl_options)
|
2022-12-03 18:11:17 +13:00
|
|
|
extension = self.get_video_attributes(self.post.url)["ext"]
|
2022-04-26 04:09:09 +12:00
|
|
|
res = Resource(self.post, self.post.url, download_function, extension)
|
|
|
|
return [res]
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_video_attributes(url: str) -> dict:
|
2022-07-15 17:12:39 +12:00
|
|
|
result = VReddit.get_video_data(url)
|
2022-12-03 18:11:17 +13:00
|
|
|
if "ext" in result:
|
2022-04-26 04:09:09 +12:00
|
|
|
return result
|
|
|
|
else:
|
|
|
|
try:
|
2022-07-15 17:12:39 +12:00
|
|
|
result = result["entries"][0]
|
2022-04-26 04:09:09 +12:00
|
|
|
return result
|
|
|
|
except Exception as e:
|
|
|
|
logger.exception(e)
|
2022-12-03 18:11:17 +13:00
|
|
|
raise NotADownloadableLinkError(f"Video info extraction failed for {url}")
|