Creating functions to unify finding/making video directories.

This commit is contained in:
Roland 2022-01-16 10:17:06 -07:00
parent a70e049823
commit 89e3edb157
No known key found for this signature in database
GPG key ID: 9D6DBD3ACF11AB93
3 changed files with 27 additions and 11 deletions

View file

@ -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'

View file

@ -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):

View file

@ -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(