mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-11-26 01:46:18 -05:00
Download info can be saved during the download process and can be used by the Downloader.
This commit is contained in:
parent
f830fe3dcc
commit
f5ce952da0
8 changed files with 153 additions and 90 deletions
|
|
@ -1,4 +1,5 @@
|
|||
from abc import ABC
|
||||
from typing import Tuple
|
||||
|
||||
from wrolpi.common import logger
|
||||
from wrolpi.downloader import Downloader, Download
|
||||
|
|
@ -13,11 +14,11 @@ logger = logger.getChild(__name__)
|
|||
|
||||
class ArchiveDownloader(Downloader, ABC):
|
||||
@classmethod
|
||||
def valid_url(cls, url: str) -> bool:
|
||||
def valid_url(cls, url: str) -> Tuple[bool, None]:
|
||||
"""
|
||||
Archiver will attempt to archive anything, so it should be last!
|
||||
"""
|
||||
return True
|
||||
return True, None
|
||||
|
||||
def do_download(self, download: Download):
|
||||
if download.attempts > 3:
|
||||
|
|
@ -28,4 +29,4 @@ class ArchiveDownloader(Downloader, ABC):
|
|||
|
||||
|
||||
# Archive downloader is the last downloader which should be used.
|
||||
archive_downloader = ArchiveDownloader(priority=100)
|
||||
archive_downloader = ArchiveDownloader('archive', priority=100)
|
||||
|
|
|
|||
|
|
@ -2,12 +2,14 @@ import pathlib
|
|||
import shutil
|
||||
from uuid import uuid4
|
||||
|
||||
import mock
|
||||
import pytest
|
||||
|
||||
from modules.videos.channel.lib import spread_channel_downloads
|
||||
from modules.videos.downloader import VideoDownloader, ChannelDownloader
|
||||
from modules.videos.models import Channel, Video
|
||||
from wrolpi.common import sanitize_link
|
||||
from wrolpi.downloader import DownloadFrequency
|
||||
from wrolpi.downloader import DownloadFrequency, DownloadManager
|
||||
from wrolpi.vars import PROJECT_DIR
|
||||
|
||||
|
||||
|
|
@ -105,3 +107,19 @@ def video_factory(test_session, test_directory):
|
|||
return video
|
||||
|
||||
return _
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def video_download_manager(test_download_manager) -> DownloadManager:
|
||||
"""
|
||||
Get a DownloadManager ready to download Videos and Channels.
|
||||
"""
|
||||
channel_downloader = ChannelDownloader('video_channel')
|
||||
video_downloader = VideoDownloader('video', priority=40)
|
||||
|
||||
test_download_manager.register_downloader(channel_downloader)
|
||||
test_download_manager.register_downloader(video_downloader)
|
||||
|
||||
with mock.patch('modules.videos.downloader.YDL.extract_info') as mock_extract_info: # prevent real downloads
|
||||
mock_extract_info.side_effect = Exception('You must patch modules.videos.downloader.YDL.extract_info!')
|
||||
yield test_download_manager
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ import json
|
|||
import pathlib
|
||||
import re
|
||||
from abc import ABC
|
||||
from typing import Tuple, List
|
||||
from typing import Tuple, List, Optional
|
||||
|
||||
from sqlalchemy.orm.exc import NoResultFound
|
||||
from yt_dlp import YoutubeDL
|
||||
|
|
@ -49,12 +49,12 @@ class ChannelDownloader(Downloader, ABC):
|
|||
"""
|
||||
|
||||
@classmethod
|
||||
def valid_url(cls, url) -> bool:
|
||||
def valid_url(cls, url) -> tuple[bool, None]:
|
||||
for ie in ChannelIEs:
|
||||
if ie.suitable(url):
|
||||
return True
|
||||
return True, None
|
||||
logger.debug(f'{cls.__name__} not suitable for {url}')
|
||||
return False
|
||||
return False, None
|
||||
|
||||
def do_download(self, download: Download):
|
||||
"""
|
||||
|
|
@ -92,34 +92,30 @@ class VideoDownloader(Downloader, ABC):
|
|||
"""
|
||||
|
||||
@classmethod
|
||||
def valid_url(cls, url) -> bool:
|
||||
def valid_url(cls, url) -> tuple[bool, Optional[dict]]:
|
||||
"""
|
||||
Match against all Youtube-DL Info Extractors, except those that match a Channel.
|
||||
"""
|
||||
for ie in YDL._ies.values():
|
||||
if ie.suitable(url) and not ChannelDownloader.valid_url(url):
|
||||
if ie.suitable(url) and not ChannelDownloader.valid_url(url)[0]:
|
||||
try:
|
||||
YDL.extract_info(url, download=False, process=False)
|
||||
return True
|
||||
info = YDL.extract_info(url, download=False, process=False)
|
||||
return True, info
|
||||
except UnsupportedError:
|
||||
logger.debug(f'Video downloader extract_info failed for {url}')
|
||||
return False
|
||||
return False, None
|
||||
except DownloadError:
|
||||
logger.debug(f'Video downloader extract_info failed for {url}')
|
||||
return False
|
||||
return False, None
|
||||
logger.debug(f'{cls.__name__} not suitable for {url}')
|
||||
return False
|
||||
return False, None
|
||||
|
||||
def do_download(self, download: Download):
|
||||
if download.attempts >= 10:
|
||||
raise UnrecoverableDownloadError('Max download attempts reached')
|
||||
|
||||
url = download.url
|
||||
try:
|
||||
info = YDL.extract_info(url, download=False, process=False)
|
||||
except UnsupportedError as e:
|
||||
# Video wasn't really valid... probably the GenericIE.
|
||||
raise UnrecoverableDownloadError() from e
|
||||
info = download.info
|
||||
|
||||
channel_name = info.get('channel')
|
||||
channel_id = info.get('channel_id')
|
||||
|
|
@ -209,9 +205,9 @@ class VideoDownloader(Downloader, ABC):
|
|||
return final_filename, entry
|
||||
|
||||
|
||||
channel_downloader = ChannelDownloader()
|
||||
channel_downloader = ChannelDownloader('video_channel')
|
||||
# Videos may match the ChannelDownloader, give it a higher priority.
|
||||
video_downloader = VideoDownloader(40)
|
||||
video_downloader = VideoDownloader('video', 40)
|
||||
|
||||
|
||||
def get_or_create_channel(source_id: str = None, link: str = None, url: str = None, name: str = None) -> Channel:
|
||||
|
|
|
|||
|
|
@ -94,7 +94,7 @@ example_channel_json = {
|
|||
'ie_key': 'Youtube',
|
||||
'title': 'video 1 title',
|
||||
'uploader': None,
|
||||
'url': 'video 1 url',
|
||||
'url': 'video_1_url',
|
||||
'view_count': 58504},
|
||||
{'_type': 'url',
|
||||
'description': None,
|
||||
|
|
@ -103,7 +103,7 @@ example_channel_json = {
|
|||
'ie_key': 'Youtube',
|
||||
'title': 'video 2 title',
|
||||
'uploader': None,
|
||||
'url': 'video 2 url',
|
||||
'url': 'video_2_url',
|
||||
'view_count': 1413},
|
||||
],
|
||||
'extractor': 'youtube:tab',
|
||||
|
|
@ -119,8 +119,8 @@ class TestVideosDownloaders(TestAPI):
|
|||
|
||||
def setUp(self) -> None:
|
||||
super().setUp()
|
||||
self.vd = VideoDownloader()
|
||||
self.cd = ChannelDownloader()
|
||||
self.vd = VideoDownloader('video')
|
||||
self.cd = ChannelDownloader('channel')
|
||||
self.mgr = DownloadManager()
|
||||
self.mgr.register_downloader(self.vd)
|
||||
self.mgr.register_downloader(self.cd)
|
||||
|
|
@ -130,32 +130,33 @@ class TestVideosDownloaders(TestAPI):
|
|||
|
||||
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'))
|
||||
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'))
|
||||
self.assertFalse(self.vd.valid_url('https://www.youtube.com/c/LearningSelfReliance'))
|
||||
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'))
|
||||
self.assertFalse(self.cd.valid_url('https://youtube.com'))
|
||||
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'))
|
||||
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'))
|
||||
self.assertTrue(self.cd.valid_url('https://www.youtube.com/c/LearningSelfReliance'))
|
||||
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'))
|
||||
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'), self.cd)
|
||||
self.assertEqual(self.mgr.get_downloader('https://www.youtube.com/c/LearningSelfReliance'), self.cd)
|
||||
self.assertEqual(self.mgr.get_downloader('https://www.youtube.com/watch?v=31jPEBiAC3c'), self.vd)
|
||||
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):
|
||||
|
|
@ -269,7 +270,7 @@ class TestVideosDownloaders(TestAPI):
|
|||
_, session = get_db_context()
|
||||
with mock.patch('modules.videos.downloader.VideoDownloader.valid_url') as mock_valid_url, \
|
||||
mock.patch('modules.videos.downloader.YDL') as mock_ydl:
|
||||
mock_valid_url.return_value = True
|
||||
mock_valid_url.return_value = (True, None)
|
||||
mock_ydl.extract_info.side_effect = UnsupportedError('oops')
|
||||
self.mgr.create_download('url', session)
|
||||
|
||||
|
|
@ -360,3 +361,13 @@ class TestVideosDownloaders(TestAPI):
|
|||
|
||||
# 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()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue