1
0
Fork 0
mirror of synced 2024-09-29 08:41:56 +13:00
bulk-downloader-for-reddit/bulkredditdownloader/site_downloaders/youtube.py

38 lines
1.1 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
import logging
2021-02-07 01:29:13 +13:00
import pathlib
2021-02-11 12:10:40 +13:00
import tempfile
import youtube_dl
2021-02-11 12:10:40 +13:00
from praw.models import Submission
2021-02-11 12:10:40 +13:00
from bulkredditdownloader.resource import Resource
2021-02-07 20:08:24 +13:00
from bulkredditdownloader.site_downloaders.base_downloader import BaseDownloader
logger = logging.getLogger(__name__)
2021-02-07 14:33:19 +13:00
class Youtube(BaseDownloader):
2021-02-11 12:10:40 +13:00
def __init__(self, directory: pathlib.Path, post: Submission):
2021-02-07 14:33:19 +13:00
super().__init__(directory, post)
def download(self):
2021-02-11 12:10:40 +13:00
return self._download_video()
def _download_video(self) -> Resource:
with tempfile.TemporaryDirectory() as temp_dir:
ydl_opts = {
"format": "best",
"outtmpl": str(temp_dir / "test.%(ext)s"),
"playlistend": 1,
"nooverwrites": True,
"quiet": True
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
ydl.download([self.post.url])
with open(temp_dir / 'test.mp4', 'rb') as file:
content = file.read()
return Resource(self.post, self.post.url, content)