Add tests for Youtube

This commit is contained in:
Serene-Arc
2021-02-28 14:52:20 +10:00
committed by Ali Parlakci
parent be68d4eb1c
commit caefb591dd
2 changed files with 32 additions and 4 deletions

View File

@@ -2,13 +2,14 @@
import logging
import tempfile
from pathlib import Path
from typing import Optional
import youtube_dl
from praw.models import Submission
from bulkredditdownloader.site_authenticator import SiteAuthenticator
from bulkredditdownloader.resource import Resource
from bulkredditdownloader.site_authenticator import SiteAuthenticator
from bulkredditdownloader.site_downloaders.base_downloader import BaseDownloader
logger = logging.getLogger(__name__)
@@ -23,9 +24,10 @@ class Youtube(BaseDownloader):
def _download_video(self) -> Resource:
with tempfile.TemporaryDirectory() as temp_dir:
download_path = Path(temp_dir).resolve()
ydl_opts = {
"format": "best",
"outtmpl": str(temp_dir / "test.%(ext)s"),
"outtmpl": str(download_path) + '/' + 'test.%(ext)s',
"playlistend": 1,
"nooverwrites": True,
"quiet": True
@@ -33,8 +35,11 @@ class Youtube(BaseDownloader):
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
ydl.download([self.post.url])
with open(temp_dir / 'test.mp4', 'rb') as file:
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)
out = Resource(self.post, self.post.url, extension)
out.content = content
out.create_hash()
return out