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

View File

@ -0,0 +1,23 @@
#!/usr/bin/env python3
# coding=utf-8
import praw
import pytest
from bulkredditdownloader.resource import Resource
from bulkredditdownloader.site_downloaders.youtube import Youtube
@pytest.mark.online
@pytest.mark.reddit
@pytest.mark.parametrize(('test_submission_id', 'expected_hash'), (
('ltnoqp', '468136300a106c67f1463a7011a6db4a'),
))
def test_find_resources(test_submission_id: str, expected_hash: str, reddit_instance: praw.Reddit):
test_submission = reddit_instance.submission(id=test_submission_id)
downloader = Youtube(test_submission)
resources = downloader.find_resources()
assert len(resources) == 1
assert isinstance(resources[0], Resource)
resources[0].download()
assert resources[0].hash.hexdigest() == expected_hash