From caefb591dd28c1801ed9ab580e1aa82f6790585e Mon Sep 17 00:00:00 2001 From: Serene-Arc Date: Sun, 28 Feb 2021 14:52:20 +1000 Subject: [PATCH] Add tests for Youtube --- .../site_downloaders/youtube.py | 13 +++++++---- .../tests/downloaders/test_youtube.py | 23 +++++++++++++++++++ 2 files changed, 32 insertions(+), 4 deletions(-) create mode 100644 bulkredditdownloader/tests/downloaders/test_youtube.py diff --git a/bulkredditdownloader/site_downloaders/youtube.py b/bulkredditdownloader/site_downloaders/youtube.py index 369835f..ff770a3 100644 --- a/bulkredditdownloader/site_downloaders/youtube.py +++ b/bulkredditdownloader/site_downloaders/youtube.py @@ -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 diff --git a/bulkredditdownloader/tests/downloaders/test_youtube.py b/bulkredditdownloader/tests/downloaders/test_youtube.py new file mode 100644 index 0000000..0bbe10d --- /dev/null +++ b/bulkredditdownloader/tests/downloaders/test_youtube.py @@ -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