From 641fb368eb7b794c5093a500daf71a5703945d08 Mon Sep 17 00:00:00 2001 From: Roland Date: Tue, 23 Dec 2025 18:49:33 -0700 Subject: [PATCH] Adding upload_year and upload_month to the video file format option. --- app/src/components/Videos.js | 2 ++ modules/videos/downloader.py | 34 +++++++++++++++++-- modules/videos/test/test_api.py | 14 ++++++++ modules/videos/test/test_downloader.py | 45 ++++++++++++++++++++++++++ 4 files changed, 92 insertions(+), 3 deletions(-) diff --git a/app/src/components/Videos.js b/app/src/components/Videos.js index e8380ad0..02f31e14 100644 --- a/app/src/components/Videos.js +++ b/app/src/components/Videos.js @@ -254,6 +254,8 @@ function VideoFileNameForm({form}) {
  • %(title)s - Video title
  • %(uploader)s - Channel/uploader name
  • %(upload_date)s - Upload date (YYYYMMDD)
  • +
  • %(upload_year)s - Upload year (YYYY)
  • +
  • %(upload_month)s - Upload month (01-12)
  • %(id)s - Video ID
  • %(ext)s - File extension
  • %(channel)s - Channel name
  • diff --git a/modules/videos/downloader.py b/modules/videos/downloader.py index 0e620147..9a1b26c3 100755 --- a/modules/videos/downloader.py +++ b/modules/videos/downloader.py @@ -115,6 +115,29 @@ def prepare_filename(entry: dict, ydl: YoutubeDL = YDL) -> str: return f'{dir_name}/{file_name}' +def convert_wrolpi_filename_format(filename_format: str) -> str: + """Convert WROLPi-specific filename variables to yt-dlp syntax. + + WROLPi provides user-friendly variables that map to yt-dlp's strftime syntax: + %(upload_year)s -> %(upload_date>%Y)s + %(upload_month)s -> %(upload_date>%m)s + + This allows users to organize videos into year/month subdirectories. + + Example: + '%(upload_year)s/%(upload_month)s/%(title)s.%(ext)s' + -> '%(upload_date>%Y)s/%(upload_date>%m)s/%(title)s.%(ext)s' + """ + conversions = { + '%(upload_year)s': '%(upload_date>%Y)s', + '%(upload_month)s': '%(upload_date>%m)s', + } + result = filename_format + for wrolpi_var, ytdlp_var in conversions.items(): + result = result.replace(wrolpi_var, ytdlp_var) + return result + + def preview_filename(filename_format: str) -> str: """Return an example video file name formatted using the provided filename format. @@ -123,8 +146,11 @@ def preview_filename(filename_format: str) -> str: if not filename_format.endswith('.%(ext)s'): raise RuntimeError('Filename must end with .%(ext)s') + # Convert WROLPi-specific variables to yt-dlp syntax + converted_format = convert_wrolpi_filename_format(filename_format) + options = get_videos_downloader_config().yt_dlp_options - options['outtmpl'] = filename_format + options['outtmpl'] = converted_format ydl = YoutubeDL(copy.deepcopy(options)) entry = dict( uploader='WROLPi', @@ -135,7 +161,7 @@ def preview_filename(filename_format: str) -> str: ext='mp4', description='A description of the video', ) - filename = prepare_filename(entry, ydl=ydl)[1:] + filename = prepare_filename(entry, ydl=ydl).lstrip('/') return filename @@ -663,8 +689,10 @@ class VideoDownloader(Downloader, ABC): # YoutubeDL expects specific options, add onto the default options config = get_videos_downloader_config() options = config.yt_dlp_options + # Convert WROLPi-specific variables to yt-dlp syntax + converted_format = convert_wrolpi_filename_format(config.file_name_format) # yt-dlp expects the absolute path. - options['outtmpl'] = f'{out_dir}/{config.file_name_format}' + options['outtmpl'] = f'{out_dir}/{converted_format}' options['merge_output_format'] = video_format # options['remuxvideo'] = video_format options['format'] = video_resolutions diff --git a/modules/videos/test/test_api.py b/modules/videos/test/test_api.py index cade2363..0dd5cfc7 100644 --- a/modules/videos/test/test_api.py +++ b/modules/videos/test/test_api.py @@ -368,6 +368,20 @@ async def test_video_file_format(async_client, test_session, fake_now): assert response.json['error'] == 'Filename must end with .%(ext)s' +@pytest.mark.asyncio +async def test_video_file_format_with_upload_year(async_client, test_session, fake_now): + """Video file format API handles upload_year and upload_month variables.""" + body = dict(video_file_format='%(upload_year)s/%(upload_month)s/%(title)s.%(ext)s') + request, response = await async_client.post('/api/videos/file_format', json=body) + assert response.status_code == HTTPStatus.OK + assert response.json['preview'] == '2000/01/The title of the video.mp4' + + body = dict(video_file_format='%(upload_year)s/%(uploader)s_%(upload_date)s_%(title)s.%(ext)s') + request, response = await async_client.post('/api/videos/file_format', json=body) + assert response.status_code == HTTPStatus.OK + assert response.json['preview'] == '2000/WROLPi_20000101_The title of the video.mp4' + + @pytest.mark.asyncio async def test_video_upload_file_tracking(test_session, async_client, video_factory, await_switches, make_multipart_form, diff --git a/modules/videos/test/test_downloader.py b/modules/videos/test/test_downloader.py index 10ee9d33..fa0185b1 100644 --- a/modules/videos/test/test_downloader.py +++ b/modules/videos/test/test_downloader.py @@ -528,6 +528,51 @@ def test_preview_filename(test_directory, fake_now): preview_filename('%(upload_date)s_%(title)s.asdf') +def test_convert_wrolpi_filename_format(): + """WROLPi filename variables are converted to yt-dlp syntax.""" + from modules.videos.downloader import convert_wrolpi_filename_format + + # Test upload_year conversion + assert convert_wrolpi_filename_format('%(upload_year)s/%(title)s.%(ext)s') \ + == '%(upload_date>%Y)s/%(title)s.%(ext)s' + + # Test upload_month conversion + assert convert_wrolpi_filename_format('%(upload_month)s/%(title)s.%(ext)s') \ + == '%(upload_date>%m)s/%(title)s.%(ext)s' + + # Test both together + assert convert_wrolpi_filename_format('%(upload_year)s/%(upload_month)s/%(title)s.%(ext)s') \ + == '%(upload_date>%Y)s/%(upload_date>%m)s/%(title)s.%(ext)s' + + # Test with other variables unchanged + format_str = '%(upload_year)s/%(uploader)s_%(upload_date)s_%(id)s_%(title)s.%(ext)s' + expected = '%(upload_date>%Y)s/%(uploader)s_%(upload_date)s_%(id)s_%(title)s.%(ext)s' + assert convert_wrolpi_filename_format(format_str) == expected + + # Test format without WROLPi variables unchanged + original = '%(uploader)s_%(upload_date)s_%(id)s_%(title)s.%(ext)s' + assert convert_wrolpi_filename_format(original) == original + + +def test_preview_filename_with_upload_year(test_directory, fake_now): + """preview_filename works with upload_year and upload_month variables.""" + # Year only + assert preview_filename('%(upload_year)s/%(title)s.%(ext)s') \ + == '2000/The title of the video.mp4' + + # Month only + assert preview_filename('%(upload_month)s/%(title)s.%(ext)s') \ + == '01/The title of the video.mp4' + + # Year and month together + assert preview_filename('%(upload_year)s/%(upload_month)s/%(title)s.%(ext)s') \ + == '2000/01/The title of the video.mp4' + + # Combined with traditional format + assert preview_filename('%(upload_year)s/%(uploader)s_%(upload_date)s_%(title)s.%(ext)s') \ + == '2000/WROLPi_20000101_The title of the video.mp4' + + def test_normalize_video_file_names(test_directory, video_download_manager): """VideoDownloader renames caption files that do not share the video file's stem.""" downloader = VideoDownloader()