mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-11-26 18:06:17 -05:00
418 lines
17 KiB
Python
418 lines
17 KiB
Python
import pathlib
|
|
from copy import copy
|
|
from unittest import mock
|
|
|
|
import pytest
|
|
from yt_dlp.utils import UnsupportedError
|
|
|
|
from modules.videos.channel.lib import download_channel
|
|
from modules.videos.downloader import find_all_missing_videos, VideoDownloader, \
|
|
ChannelDownloader, get_or_create_channel
|
|
from modules.videos.models import Channel, Video
|
|
from modules.videos.test.common import create_channel_structure
|
|
from wrolpi.db import get_db_session, get_db_context
|
|
from wrolpi.downloader import DownloadManager, Download
|
|
from wrolpi.errors import InvalidDownload
|
|
from wrolpi.test.common import wrap_test_db, TestAPI
|
|
from wrolpi.test.test_downloader import HTTPDownloader
|
|
|
|
|
|
class FakeYDL:
|
|
|
|
@staticmethod
|
|
def extract_info(*a, **kw):
|
|
return {
|
|
'entries': [
|
|
{'id': 1},
|
|
],
|
|
}
|
|
|
|
|
|
class TestDownloader(TestAPI):
|
|
@wrap_test_db
|
|
@create_channel_structure(
|
|
{
|
|
'channel1': ['vid1.mp4'],
|
|
'channel2': ['vid2.mp4']
|
|
}
|
|
)
|
|
def test_find_all_missing_videos(self, tempdir):
|
|
with get_db_session(commit=True) as session:
|
|
channel1, channel2 = session.query(Channel).order_by(Channel.id).all()
|
|
channel1.url = channel2.url = 'some url'
|
|
channel1.info_json = {'entries': [{'id': 'foo'}]}
|
|
|
|
# No missing videos
|
|
self.assertEqual([], list(find_all_missing_videos()))
|
|
|
|
# Create a video that has no video file.
|
|
with get_db_session(commit=True) as session:
|
|
video = Video(title='needs to be downloaded', channel_id=channel1.id, source_id='foo')
|
|
session.add(video)
|
|
|
|
missing = list(find_all_missing_videos())
|
|
self.assertEqual(len(missing), 1)
|
|
|
|
# The video returned is the one we faked.
|
|
id_, source_id, entry = missing[0]
|
|
# Two videos were created for this test already.
|
|
self.assertEqual(id_, 3)
|
|
# The fake entry we added is regurgitated back.
|
|
self.assertEqual(entry, channel1.info_json['entries'][0])
|
|
|
|
|
|
example_video_json = {
|
|
'age_limit': 0,
|
|
'average_rating': 4.6923075,
|
|
'categories': ['Howto & Style'],
|
|
'channel': 'channel name',
|
|
'channel_id': 'channel id',
|
|
'channel_url': 'channel url',
|
|
'duration': 635,
|
|
'extractor': 'youtube',
|
|
'extractor_key': 'Youtube',
|
|
'id': 'some long id',
|
|
'is_live': None,
|
|
'like_count': 24,
|
|
'subtitles': {},
|
|
'title': "The video's title",
|
|
'upload_date': '20190707',
|
|
'uploader': 'uploader name',
|
|
'uploader_id': 'uploader id',
|
|
'uploader_url': 'uploader url',
|
|
'view_count': 406,
|
|
'webpage_url': 'webpage url',
|
|
'webpage_url_basename': 'watch',
|
|
}
|
|
|
|
example_channel_json = {
|
|
'_type': 'playlist',
|
|
'entries': [
|
|
{'_type': 'url',
|
|
'description': None,
|
|
'duration': None,
|
|
'id': 'video 1 id',
|
|
'ie_key': 'Youtube',
|
|
'title': 'video 1 title',
|
|
'uploader': None,
|
|
'url': 'video_1_url',
|
|
'view_count': 58504},
|
|
{'_type': 'url',
|
|
'description': None,
|
|
'duration': None,
|
|
'id': 'video 2 id',
|
|
'ie_key': 'Youtube',
|
|
'title': 'video 2 title',
|
|
'uploader': None,
|
|
'url': 'video_2_url',
|
|
'view_count': 1413},
|
|
],
|
|
'extractor': 'youtube:tab',
|
|
'extractor_key': 'YoutubeTab',
|
|
'id': 'some id',
|
|
'title': 'channel title',
|
|
'webpage_url': 'channel url',
|
|
'webpage_url_basename': 'videos',
|
|
}
|
|
|
|
|
|
class TestVideosDownloaders(TestAPI):
|
|
|
|
def setUp(self) -> None:
|
|
super().setUp()
|
|
self.vd = VideoDownloader()
|
|
self.cd = ChannelDownloader()
|
|
self.mgr = DownloadManager()
|
|
self.mgr.register_downloader(self.vd)
|
|
self.mgr.register_downloader(self.cd)
|
|
|
|
self.videos_dir = pathlib.Path(self.tmp_dir.name) / 'videos'
|
|
self.videos_dir.mkdir()
|
|
|
|
def test_video_valid_url(self):
|
|
# A specific video can be downloaded.
|
|
self.assertTrue(self.vd.valid_url('https://www.youtube.com/watch?v=31jPEBiAC3c')[0])
|
|
|
|
# A channel cannot be downloaded.
|
|
self.assertFalse(self.vd.valid_url('https://www.youtube.com/c/LearningSelfReliance/videos')[0])
|
|
self.assertFalse(self.vd.valid_url('https://www.youtube.com/c/LearningSelfReliance')[0])
|
|
|
|
def test_channel_valid_url(self):
|
|
# An entire domain cannot be downloaded.
|
|
self.assertFalse(self.cd.valid_url('https://example.com')[0])
|
|
self.assertFalse(self.cd.valid_url('https://youtube.com')[0])
|
|
|
|
# Cannot download a single video.
|
|
self.assertFalse(self.cd.valid_url('https://www.youtube.com/watch?v=31jPEBiAC3c')[0])
|
|
|
|
# Can download entire channels.
|
|
self.assertTrue(self.cd.valid_url('https://www.youtube.com/c/LearningSelfReliance/videos')[0])
|
|
self.assertTrue(self.cd.valid_url('https://www.youtube.com/c/LearningSelfReliance')[0])
|
|
|
|
# Can download entire playlists.
|
|
self.assertTrue(
|
|
self.cd.valid_url('https://www.youtube.com/playlist?list=PLCdlMQeP-TbG12nkBCt0E96yr3EfwcH4E')[0])
|
|
|
|
def test_get_downloader(self):
|
|
# The correct Downloader is gotten.
|
|
self.assertEqual(self.mgr.get_downloader('https://www.youtube.com/c/LearningSelfReliance/videos')[0], self.cd)
|
|
self.assertEqual(self.mgr.get_downloader('https://www.youtube.com/c/LearningSelfReliance')[0], self.cd)
|
|
self.assertEqual(self.mgr.get_downloader('https://www.youtube.com/watch?v=31jPEBiAC3c')[0], self.vd)
|
|
|
|
@staticmethod
|
|
def _make_video_files(channel_dir):
|
|
video_path = channel_dir / 'video.mp4'
|
|
poster_path = channel_dir / 'video.jpg'
|
|
description_path = channel_dir / 'video.description'
|
|
info_json_path = channel_dir / 'video.info.json'
|
|
video_path.touch()
|
|
poster_path.touch()
|
|
description_path.touch()
|
|
info_json_path.touch()
|
|
return video_path, poster_path, description_path, info_json_path
|
|
|
|
@wrap_test_db
|
|
def test_video_download(self):
|
|
channel_dir = self.videos_dir / 'channel name'
|
|
channel_dir.mkdir(parents=True)
|
|
|
|
# Video paths
|
|
video_path, poster_path, description_path, info_json_path = self._make_video_files(channel_dir)
|
|
|
|
with get_db_session() as session:
|
|
url = 'https://www.youtube.com/watch?v=31jPEBiAC3c'
|
|
with mock.patch('modules.videos.downloader.YDL.extract_info') as mock_extract_info, \
|
|
mock.patch('modules.videos.downloader.VideoDownloader.prepare_filename') as mock_prepare_filename, \
|
|
mock.patch('modules.videos.downloader.VideoDownloader.process_runner') as mock_process_runner:
|
|
mock_extract_info.return_value = example_video_json
|
|
mock_prepare_filename.return_value = (video_path, {'id': 'foo'})
|
|
mock_process_runner.return_value = 0
|
|
|
|
self.mgr.create_download(url, session)
|
|
|
|
mock_process_runner.assert_called_once()
|
|
video_url, _, out_dir = mock_process_runner.call_args[0]
|
|
|
|
download: Download = session.query(Download).one()
|
|
self.assertEqual(video_url, download.url)
|
|
channel: Channel = session.query(Channel).one()
|
|
self.assertEqual(channel.directory, channel_dir)
|
|
|
|
video: Video = session.query(Video).one()
|
|
self.assertEqual(video.video_path, video_path)
|
|
self.assertEqual(video.description_path, description_path)
|
|
self.assertEqual(video.poster_path, poster_path)
|
|
self.assertEqual(video.info_json_path, info_json_path)
|
|
self.assertTrue(video.video_path.path.is_absolute())
|
|
self.assertTrue(video.poster_path.path.is_absolute())
|
|
self.assertTrue(video.description_path.path.is_absolute())
|
|
self.assertTrue(video.info_json_path.path.is_absolute())
|
|
|
|
@wrap_test_db
|
|
def test_video_download_no_channel(self):
|
|
_, session = get_db_context()
|
|
|
|
channel_dir = self.videos_dir / 'NO CHANNEL'
|
|
channel_dir.mkdir(parents=True)
|
|
|
|
# Video paths
|
|
video_path, _, _, _ = self._make_video_files(channel_dir)
|
|
|
|
# Video has no channel
|
|
info_json = copy(example_video_json)
|
|
del info_json['channel']
|
|
del info_json['channel_id']
|
|
del info_json['channel_url']
|
|
|
|
url = 'https://www.youtube.com/watch?v=31jPEBiAC3c'
|
|
with mock.patch('modules.videos.downloader.YDL.extract_info') as mock_extract_info, \
|
|
mock.patch('modules.videos.downloader.VideoDownloader.prepare_filename') as mock_prepare_filename, \
|
|
mock.patch('modules.videos.downloader.VideoDownloader.process_runner') as mock_process_runner:
|
|
mock_extract_info.return_value = info_json
|
|
mock_prepare_filename.return_value = (video_path, {'id': 'foo'})
|
|
mock_process_runner.return_value = 0
|
|
|
|
self.mgr.create_download(url, session)
|
|
|
|
mock_process_runner.assert_called_once()
|
|
|
|
video: Video = session.query(Video).one()
|
|
self.assertEqual(str(video.video_path.path), f'{channel_dir}/video.mp4')
|
|
|
|
@wrap_test_db
|
|
def test_channel_download(self):
|
|
_, session = get_db_context()
|
|
|
|
url = 'https://www.youtube.com/c/LearningSelfReliance/videos'
|
|
session.add(Channel(url=url, link='foo'))
|
|
|
|
channel_dir = self.videos_dir / 'NO CHANNEL'
|
|
channel_dir.mkdir(parents=True)
|
|
video_path, _, _, _ = self._make_video_files(channel_dir)
|
|
|
|
with mock.patch('modules.videos.downloader.YDL.extract_info') as mock_extract_info, \
|
|
mock.patch('modules.videos.downloader.VideoDownloader.prepare_filename') as mock_prepare_filename, \
|
|
mock.patch('modules.videos.downloader.VideoDownloader.process_runner') as mock_process_runner:
|
|
mock_extract_info.return_value = example_channel_json
|
|
mock_prepare_filename.return_value = (video_path, {'id': 'foo'})
|
|
mock_process_runner.return_value = 0
|
|
self.mgr.create_download(url, session)
|
|
|
|
# Two videos are in the example channel.
|
|
downloads = self.mgr.get_new_downloads(session)
|
|
expected = ['https://youtube.com/watch?v=HQ_62YwcA80', 'https://youtube.com/watch?v=4gSz6W4Gv-o']
|
|
for download, expected in zip(downloads, expected):
|
|
self.assertEqual(download.url, expected)
|
|
# Download is skipped for test.
|
|
self.assertEqual(download.status, 'new')
|
|
|
|
@wrap_test_db
|
|
def test_invalid_download_url(self):
|
|
"""
|
|
An invalid url should not be attempted again.
|
|
"""
|
|
_, session = get_db_context()
|
|
with mock.patch('modules.videos.downloader.VideoDownloader.valid_url') as mock_valid_url, \
|
|
mock.patch('wrolpi.downloader.DownloadManager.get_downloader') as mock_get_downloader, \
|
|
mock.patch('modules.videos.downloader.YDL') as mock_ydl:
|
|
mock_get_downloader.return_value = (VideoDownloader, {'channel': 'foo', 'id': 'some id'})
|
|
mock_valid_url.return_value = (True, None)
|
|
mock_ydl.extract_info.side_effect = UnsupportedError('oops')
|
|
self.mgr.create_download('url', session)
|
|
|
|
with get_db_session() as session:
|
|
download = self.mgr.get_downloads(session)[0]
|
|
assert download.status == 'failed'
|
|
|
|
@wrap_test_db
|
|
def test_get_or_create_channel(self):
|
|
"""
|
|
A Channel may need to be created for an arbitrary download. Attempt to use an existing Channel if we can
|
|
match it.
|
|
"""
|
|
with get_db_session(commit=True) as session:
|
|
c1 = Channel(name='foo', link='foo', source_id='foo', url='foo')
|
|
c2 = Channel(name='bar', link='bar', source_id='bar')
|
|
c3 = Channel(name='baz', link='baz', source_id='baz', url='baz')
|
|
c4 = Channel(name='qux', link='qux')
|
|
session.add_all([c1, c2, c3, c4])
|
|
|
|
# All existing channels should be used.
|
|
tests = [
|
|
(dict(source_id='foo'), c1),
|
|
(dict(link='foo'), c1),
|
|
(dict(url='foo'), c1),
|
|
(dict(url='foo', source_id='bar'), c2), # source_id is preferred.
|
|
(dict(name='foo', source_id='bar'), c2),
|
|
(dict(source_id='bar'), c2),
|
|
(dict(source_id='baz'), c3),
|
|
(dict(name='qux'), c4),
|
|
(dict(link='qux'), c4),
|
|
]
|
|
for kwargs, expected in tests:
|
|
channel = get_or_create_channel(**kwargs)
|
|
self.assertEqual(expected.id, channel.id, f'Expected {expected} got {channel}')
|
|
|
|
# A new channel is created. It will not be automatically downloaded.
|
|
channel = get_or_create_channel(source_id='quux', name='quux', url='quux')
|
|
self.assertEqual(channel.id, 5)
|
|
self.assertEqual(channel.source_id, 'quux')
|
|
self.assertEqual(channel.name, 'quux')
|
|
self.assertEqual(channel.link, 'quux')
|
|
self.assertEqual(channel.url, 'quux')
|
|
self.assertIsNone(channel.download_frequency)
|
|
|
|
# New channel can be retrieved.
|
|
self.assertEqual(get_or_create_channel(source_id='quux'), channel)
|
|
|
|
|
|
def test_download_info_save(test_session, video_download_manager):
|
|
"""
|
|
An info dict from Downloader.valid_url is saved to the Downloader and can be used by the Downloader later.
|
|
"""
|
|
with mock.patch('modules.videos.downloader.YDL.extract_info') as mock_extract_info:
|
|
mock_extract_info.return_value = {'some': 'info'}
|
|
video_download_manager.create_download('https://www.youtube.com/watch?v=HQ_62YwcA80', test_session)
|
|
mock_extract_info.assert_called_once()
|
|
|
|
|
|
def test_selected_downloader(test_session, video_download_manager):
|
|
"""
|
|
A user can specify which Downloader to use, no other should ever be used (even if the Download fails).
|
|
"""
|
|
# The test HTTP Downloader will claim to be able to handle any HTTP URL; will pretend to have downloaded anything.
|
|
http_downloader = HTTPDownloader()
|
|
mock_do_download = http_downloader.do_download = mock.MagicMock()
|
|
mock_http_valid_url = http_downloader.valid_url = mock.MagicMock()
|
|
mock_http_valid_url.return_value = (True, None)
|
|
video_download_manager.register_downloader(http_downloader)
|
|
|
|
def check_attempts(attempts):
|
|
assert test_session.query(Download).one().attempts == attempts
|
|
|
|
# DownloadManager will attempt to choose the Downloader.
|
|
with mock.patch('modules.videos.downloader.YDL.extract_info') as mock_extract_info:
|
|
mock_extract_info.side_effect = UnsupportedError('not this one')
|
|
video_download_manager.create_downloads(['https://example.com'], test_session)
|
|
mock_http_valid_url.assert_called_once()
|
|
mock_extract_info.assert_called_once()
|
|
mock_do_download.assert_called_once()
|
|
|
|
check_attempts(1)
|
|
|
|
mock_http_valid_url.reset_mock()
|
|
mock_do_download.reset_mock()
|
|
|
|
# DownloadManager will only use the HTTPDownloader.
|
|
with mock.patch('modules.videos.downloader.YDL.extract_info') as mock_extract_info:
|
|
video_download_manager.create_downloads(['https://example.com'], test_session, downloader='http')
|
|
# DownloadManager does not try to find the Download, it trusts the "downloader" param.
|
|
mock_extract_info.assert_not_called()
|
|
mock_http_valid_url.assert_not_called()
|
|
# DownloadManager called do_download.
|
|
mock_do_download.assert_called_once()
|
|
|
|
check_attempts(2)
|
|
|
|
mock_http_valid_url.reset_mock()
|
|
mock_do_download.reset_mock()
|
|
|
|
# DownloadManager will only use the HTTPDownloader, even if the download fails.
|
|
with mock.patch('modules.videos.downloader.YDL.extract_info') as mock_extract_info:
|
|
mock_do_download.side_effect = Exception('oh no!')
|
|
video_download_manager.create_downloads(['https://example.com'], test_session, downloader='http')
|
|
# DownloadManager does not try to find the Download, it trusts the "downloader" param.
|
|
mock_extract_info.assert_not_called()
|
|
mock_http_valid_url.assert_not_called()
|
|
# DownloadManager called do_download.
|
|
mock_do_download.assert_called_once()
|
|
|
|
check_attempts(3)
|
|
|
|
|
|
def test_channel_downloader_hidden(video_download_manager):
|
|
"""
|
|
ChannelDownloader should not be presented to the User.
|
|
"""
|
|
downloaders = video_download_manager.list_downloaders()
|
|
assert [i.__json__() for i in downloaders] == [(dict(name='video', pretty_name='Videos')), ]
|
|
|
|
|
|
def test_bad_downloader(test_session, video_download_manager):
|
|
"""
|
|
Attempting to use an unknown downloader should raise an error.
|
|
"""
|
|
with pytest.raises(InvalidDownload):
|
|
video_download_manager.create_download('https://example.com', test_session, 'bad downloader')
|
|
|
|
|
|
def test_channel_download_no_download(test_session, video_download_manager, simple_channel):
|
|
"""
|
|
A Channel without an existing Download cannot be downloaded. This is to avoid the situation where a single
|
|
video has been downloaded in a Channel, but the User has not requested downloading of all videos in the channel.
|
|
"""
|
|
# simple_channel has no download record.
|
|
assert video_download_manager.get_downloads(test_session) == []
|
|
|
|
with pytest.raises(InvalidDownload):
|
|
download_channel(simple_channel.link)
|