mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-11-28 19:06:19 -05:00
Downloader can be specified by the user.
This commit is contained in:
parent
4a169ba43c
commit
047b507119
12 changed files with 171 additions and 53 deletions
|
|
@ -14,6 +14,7 @@ logger = logger.getChild(__name__)
|
|||
|
||||
class ArchiveDownloader(Downloader, ABC):
|
||||
name = 'archive'
|
||||
pretty_name = 'Archiver'
|
||||
|
||||
def __repr__(self):
|
||||
return f'<ArchiveDownloader name={self.name}>'
|
||||
|
|
|
|||
|
|
@ -244,5 +244,6 @@ def delete_channel(link):
|
|||
|
||||
def download_channel(link: str):
|
||||
channel = get_channel(link=link, return_dict=False)
|
||||
# TODO do not download the channel if it does not already have Download record.
|
||||
with get_db_session(commit=True) as session:
|
||||
download_manager.create_download(channel.url, session)
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ import re
|
|||
from abc import ABC
|
||||
from typing import Tuple, List, Optional
|
||||
|
||||
from sqlalchemy.orm import Session
|
||||
from sqlalchemy.orm.exc import NoResultFound
|
||||
from yt_dlp import YoutubeDL
|
||||
from yt_dlp.extractor import YoutubeTabIE
|
||||
|
|
@ -48,6 +49,7 @@ class ChannelDownloader(Downloader, ABC):
|
|||
Handling downloading of all Videos in a Channel.
|
||||
"""
|
||||
name = 'video_channel'
|
||||
pretty_name = 'Video Channel'
|
||||
|
||||
def __repr__(self):
|
||||
return f'<ChannelDownloader name={self.name}>'
|
||||
|
|
@ -95,6 +97,7 @@ class VideoDownloader(Downloader, ABC):
|
|||
Download a single video. Store the video in it's channel's directory, otherwise store it in `videos/NO CHANNEL`.
|
||||
"""
|
||||
name = 'video'
|
||||
pretty_name = 'Videos'
|
||||
|
||||
def __repr__(self):
|
||||
return f'<VideoDownloader name={self.name}>'
|
||||
|
|
@ -125,6 +128,15 @@ class VideoDownloader(Downloader, ABC):
|
|||
url = download.url
|
||||
info = download.info_json
|
||||
|
||||
if not info:
|
||||
# Info was not fetched by the DownloadManager, lets get it.
|
||||
valid, info = self.valid_url(url)
|
||||
if not valid:
|
||||
raise UnrecoverableDownloadError(f'{self} cannot download {url}')
|
||||
session = Session.object_session(download)
|
||||
download.info_json = info
|
||||
session.commit()
|
||||
|
||||
channel_name = info.get('channel')
|
||||
channel_id = info.get('channel_id')
|
||||
channel = None
|
||||
|
|
|
|||
|
|
@ -255,9 +255,6 @@ class Channel(ModelHelper, Base):
|
|||
info_json = Column(JSON)
|
||||
info_date = Column(Date)
|
||||
|
||||
# Will be filled later by directory path relative to the media directory.
|
||||
_directory = None
|
||||
|
||||
videos: InstrumentedList = relationship('Video', primaryjoin='Channel.id==Video.channel_id')
|
||||
|
||||
def __repr__(self):
|
||||
|
|
|
|||
|
|
@ -14,6 +14,7 @@ from wrolpi.dates import local_timezone
|
|||
from wrolpi.db import get_db_session, get_db_context
|
||||
from wrolpi.downloader import DownloadManager, Download, DownloadFrequency
|
||||
from wrolpi.test.common import wrap_test_db, TestAPI
|
||||
from wrolpi.test.test_downloader import HTTPDownloader
|
||||
|
||||
|
||||
class FakeYDL:
|
||||
|
|
@ -371,3 +372,57 @@ def test_download_info_save(test_session, video_download_manager):
|
|||
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)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue