From 9d1a0ecd55f08a4bf255030045c1307c3d716157 Mon Sep 17 00:00:00 2001 From: Roland Date: Thu, 30 Dec 2021 19:00:53 -0700 Subject: [PATCH] A Channel without an existing Download cannot be downloaded. --- app/src/api.js | 25 +++++++++++++++++-------- modules/videos/channel/lib.py | 9 +++++++-- modules/videos/test/test_downloader.py | 14 +++++++++++++- 3 files changed, 37 insertions(+), 11 deletions(-) diff --git a/app/src/api.js b/app/src/api.js index a4f07dc3..105ff34f 100644 --- a/app/src/api.js +++ b/app/src/api.js @@ -36,7 +36,7 @@ async function apiCall(url, method, body, ms = 10000) { if (response.status === 403 && (await response.json())['code'] === 17) { toast({ type: 'error', - header: 'WROL Mode is enabled!', + title: 'WROL Mode is enabled!', description: 'This functionality is not enabled while WROL Mode is enabled!', time: 5000, }); @@ -47,14 +47,14 @@ async function apiCall(url, method, body, ms = 10000) { if (e.message === 'promise timeout') { toast({ type: 'error', - header: 'Server failed to respond!', + title: 'Server failed to respond!', description: 'Timeout while waiting for server response. See server logs.', time: 5000, }); } else { toast({ type: 'error', - header: 'Error!', + title: 'Error!', description: 'See client logs', time: 5000, }); @@ -138,7 +138,7 @@ export async function deleteVideo(video_id) { if (data.code === 17) { toast({ type: 'warning', - header: 'WROL Mode Enabled', + title: 'WROL Mode Enabled', description: 'This cannot be done while WROL Mode is enabled.', time: 5000, }); @@ -147,7 +147,7 @@ export async function deleteVideo(video_id) { if (response.status !== 204) { toast({ type: 'error', - header: 'Unexpected server response', + title: 'Unexpected server response', description: 'Failed to delete video. See server logs.', time: 5000, }); @@ -209,7 +209,16 @@ export async function download() { export async function downloadChannel(link) { let url = `${VIDEOS_API}/download/${link}`; - await fetch(url, {method: 'POST'}); + let response = await apiPost(url); + let json = await response.json(); + if (response.status === 400 && json['code'] === 30) { + toast({ + type: 'error', + title: 'Cannot Download!', + description: 'This channel does not have a download record. Create one!', + time: 5000, + }); + } } export async function refreshChannel(link) { @@ -223,7 +232,7 @@ export async function encryptOTP(otp, plaintext) { if (response.status !== 200) { toast({ type: 'error', - header: 'Error!', + title: 'Error!', description: 'Failed to encrypt OTP', time: 5000, }); @@ -238,7 +247,7 @@ export async function decryptOTP(otp, ciphertext) { if (response.status !== 200) { toast({ type: 'error', - header: 'Error!', + title: 'Error!', description: 'Failed to decrypt OTP', time: 5000, }); diff --git a/modules/videos/channel/lib.py b/modules/videos/channel/lib.py index 2d2396c4..12ab3edf 100644 --- a/modules/videos/channel/lib.py +++ b/modules/videos/channel/lib.py @@ -8,7 +8,7 @@ from wrolpi.common import sanitize_link, run_after, get_relative_to_media_direct from wrolpi.dates import today from wrolpi.db import get_db_session, get_db_curs from wrolpi.downloader import download_manager, Download -from wrolpi.errors import UnknownChannel, UnknownDirectory, APIError, ValidationError +from wrolpi.errors import UnknownChannel, UnknownDirectory, APIError, ValidationError, InvalidDownload from ..common import check_for_channel_conflicts from ..lib import save_channels_config from ..models import Channel @@ -243,7 +243,12 @@ def delete_channel(link): def download_channel(link: str): + """ + Create a Download record for a Channel's entire catalog. + """ 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: + if not download_manager.get_download(session, channel.url): + raise InvalidDownload(f'Channel {channel.name} does not have a download!') + download_manager.create_download(channel.url, session, downloader='video_channel') diff --git a/modules/videos/test/test_downloader.py b/modules/videos/test/test_downloader.py index e7bd6c53..52477c7d 100644 --- a/modules/videos/test/test_downloader.py +++ b/modules/videos/test/test_downloader.py @@ -6,7 +6,7 @@ from unittest import mock import pytest from yt_dlp.utils import UnsupportedError -from modules.videos.channel.lib import spread_channel_downloads +from modules.videos.channel.lib import spread_channel_downloads, download_channel from modules.videos.downloader import find_all_missing_videos, VideoDownloader, \ ChannelDownloader, get_or_create_channel from modules.videos.models import Channel, Video @@ -444,3 +444,15 @@ def test_bad_downloader(test_session, video_download_manager): """ 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)