From 89e3edb1578fa6888b2a39960d16c8a916c62fc9 Mon Sep 17 00:00:00 2001 From: Roland Date: Sun, 16 Jan 2022 10:17:06 -0700 Subject: [PATCH] Creating functions to unify finding/making video directories. --- modules/videos/channel/test/test_api.py | 3 +-- modules/videos/common.py | 22 +++++++++++++++++++++- modules/videos/downloader.py | 13 +++++-------- 3 files changed, 27 insertions(+), 11 deletions(-) diff --git a/modules/videos/channel/test/test_api.py b/modules/videos/channel/test/test_api.py index b859e62a..a610bb92 100644 --- a/modules/videos/channel/test/test_api.py +++ b/modules/videos/channel/test/test_api.py @@ -11,6 +11,7 @@ import pytest from modules.videos.api import refresh_queue from modules.videos.channel.lib import spread_channel_downloads, delete_channel +from modules.videos.common import get_no_channel_directory from modules.videos.lib import upsert_video from modules.videos.models import Channel, Video from modules.videos.test.common import create_channel_structure @@ -139,9 +140,7 @@ class TestVideoAPI(TestAPI): session.commit() # The special NO CHANNEL directory. - from modules.videos.downloader import get_no_channel_directory no_channel_path = get_no_channel_directory() - no_channel_path.mkdir() vid3 = no_channel_path / 'some video.mp4' vid3.touch() no_channel_subdir = no_channel_path / 'subdir' diff --git a/modules/videos/common.py b/modules/videos/common.py index b558628d..6dcdcec8 100644 --- a/modules/videos/common.py +++ b/modules/videos/common.py @@ -56,6 +56,26 @@ class ConfigError(Exception): pass +def get_videos_directory() -> pathlib.Path: + """ + Get the "videos" directory in the media directory. Make it if it does not exist. + """ + directory = get_media_directory() / 'videos' + if not directory.is_dir(): + directory.mkdir(parents=True) + return directory + + +def get_no_channel_directory() -> pathlib.Path: + """ + Get the "NO CHANNEL" directory in the videos directory. Make it if it does not exist. + """ + directory = get_videos_directory() / 'NO CHANNEL' + if not directory.is_dir(): + directory.mkdir(parents=True) + return directory + + @before_startup def import_videos_config(): """Import channel settings to the DB. Existing channels will be updated.""" @@ -291,7 +311,7 @@ def get_matching_directories(path: Union[str, Path]) -> List[str]: path = str(path) ignored_directories = { - str(get_media_directory() / 'videos/NO CHANNEL'), + str(get_no_channel_directory()), } if os.path.isdir(path): diff --git a/modules/videos/downloader.py b/modules/videos/downloader.py index 4d1f9537..8b8138d8 100755 --- a/modules/videos/downloader.py +++ b/modules/videos/downloader.py @@ -11,15 +11,15 @@ from yt_dlp import YoutubeDL from yt_dlp.extractor import YoutubeTabIE from yt_dlp.utils import UnsupportedError, DownloadError -from wrolpi.common import logger, sanitize_link, extract_domain, \ - get_media_directory +from wrolpi.common import logger, sanitize_link, extract_domain from wrolpi.dates import now from wrolpi.db import get_db_session, get_db_curs from wrolpi.downloader import Downloader, Download from wrolpi.errors import UnknownChannel, ChannelURLEmpty, UnrecoverableDownloadError from wrolpi.vars import PYTEST from .channel.lib import create_channel, get_channel -from .common import load_downloader_config, apply_info_json, get_channel_source_id +from .common import load_downloader_config, apply_info_json, get_channel_source_id, get_no_channel_directory, \ + get_videos_directory from .lib import upsert_video, refresh_channel_videos from .models import Video, Channel from .video_url_resolver import video_url_resolver @@ -40,10 +40,6 @@ PREFERRED_VIDEO_EXTENSION = 'mp4' PREFERRED_VIDEO_FORMAT = 'best[height=720],22,720p,mp4-480p,mp4-360p,mp4-240p' -def get_no_channel_directory(): - return get_media_directory() / 'videos/NO CHANNEL' - - class ChannelDownloader(Downloader, ABC): """ Handling downloading of all Videos in a Channel. @@ -261,7 +257,8 @@ def get_or_create_channel(source_id: str = None, link: str = None, url: str = No except UnknownChannel: pass - channel_directory = get_media_directory() / f'videos/{name}' + # Channel does not exist. Create one in the video directory. + channel_directory = get_videos_directory() / name if not channel_directory.is_dir(): channel_directory.mkdir(parents=True) data = dict(