1
0
Fork 0
mirror of synced 2024-06-02 18:34:37 +12:00
bulk-downloader-for-reddit/bdfr/site_downloaders/vreddit.py

43 lines
1.3 KiB
Python
Raw Permalink Normal View History

2022-04-26 04:09:09 +12:00
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
2022-04-26 04:09:09 +12:00
import logging
from typing import Optional
2022-04-26 04:09:09 +12:00
from praw.models import Submission
from bdfr.exceptions import NotADownloadableLinkError
2022-04-26 04:09:09 +12:00
from bdfr.resource import Resource
from bdfr.site_authenticator import SiteAuthenticator
from bdfr.site_downloaders.youtube import Youtube
2022-04-26 04:09:09 +12:00
logger = logging.getLogger(__name__)
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:
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:
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}")