diff --git a/modules/videos/lib.py b/modules/videos/lib.py index ee524f61..ce4e75c7 100644 --- a/modules/videos/lib.py +++ b/modules/videos/lib.py @@ -11,7 +11,7 @@ from sqlalchemy import or_ from sqlalchemy.orm import Session from yt_dlp import YoutubeDL -from wrolpi import before_startup +from wrolpi import before_startup, dates from wrolpi.captions import extract_captions from wrolpi.common import ConfigFile, get_media_directory, sanitize_link, register_refresh_cleanup, limit_concurrent from wrolpi.dates import Seconds @@ -80,7 +80,7 @@ def validate_video(video: Video, channel_generate_poster: bool): """ info_json_path = video.info_json_path json_data_missing = bool(video.file_group.title) and bool(video.duration) and bool(video.view_count) \ - and bool(video.url) and bool(video.upload_date) + and bool(video.url) if info_json_path and json_data_missing is False: # These properties can be found in the info json. video_info_json = process_video_info_json(video) @@ -101,9 +101,8 @@ def validate_video(video: Video, channel_generate_poster: bool): # These are the least trusted, so anything already on the video should be trusted. _, upload_date, source_id, title = parse_video_file_name(video_path) upload_date = upload_date or video.upload_date - if upload_date: - upload_date = datetime.datetime.strptime(upload_date, '%Y%M%d') - upload_date = upload_date.replace(tzinfo=datetime.timezone.utc) + if upload_date and isinstance(upload_date, str): + upload_date = dates.strpdate(upload_date) video.file_group.title = video.file_group.title or html.unescape(title) video.upload_date = upload_date video.source_id = video.source_id or source_id diff --git a/modules/videos/test/test_lib.py b/modules/videos/test/test_lib.py index 976246f7..23320822 100644 --- a/modules/videos/test/test_lib.py +++ b/modules/videos/test/test_lib.py @@ -69,7 +69,7 @@ async def test_video_factory(test_session, video_factory, channel_factory): def test_validate_video(test_session, test_directory, video_factory, image_bytes_factory, video_file): """A video poster will be generated only if the channel permits.""" - video_file = video_file.rename(test_directory / 'Channel Name_20000101_1234567890_The Title.mp4') + video_file = video_file.rename(test_directory / 'Channel Name_20050607_1234567890_The Title.mp4') vid1 = video_factory(with_video_file=video_file) # Clear source_id so source_id in the file will be extracted. vid1.source_id = None @@ -81,7 +81,7 @@ def test_validate_video(test_session, test_directory, video_factory, image_bytes assert vid1.poster_path.is_file(), 'Poster path does not exist' assert vid1.video_path.stem == vid1.poster_path.stem assert vid1.poster_path.suffix == '.jpg' - assert vid1.upload_date == datetime.datetime(2000, 1, 1, 0, 1, tzinfo=datetime.timezone.utc) + assert vid1.upload_date == datetime.datetime(2005, 6, 7, 0, 0, tzinfo=datetime.timezone.utc) assert vid1.source_id == '1234567890' assert vid1.file_group.title == 'The Title' diff --git a/wrolpi/dates.py b/wrolpi/dates.py index 5b76dfc3..d44caeb6 100644 --- a/wrolpi/dates.py +++ b/wrolpi/dates.py @@ -48,6 +48,13 @@ def strptime(dt: str) -> datetime: return datetime.fromisoformat(dt).astimezone(pytz.UTC) +def strpdate(dt: str) -> datetime: + if '-' in dt: + return datetime.strptime(dt, '%Y-%m-%d').replace(tzinfo=pytz.UTC) + + return datetime.strptime(dt, '%Y%m%d').replace(tzinfo=pytz.UTC) + + def strftime_ms(dt: datetime) -> str: return dt.strftime(DATETIME_FORMAT_MS) diff --git a/wrolpi/test/test_dates.py b/wrolpi/test/test_dates.py index f6ad9ebf..b7408daf 100644 --- a/wrolpi/test/test_dates.py +++ b/wrolpi/test/test_dates.py @@ -79,3 +79,13 @@ def test_timedelta_to_timestamp(td, expected): ]) def test_strptime(dt, expected): assert dates.strptime(dt) == expected + + +@pytest.mark.parametrize('dt,expected', [ + ('20010101', datetime(2001, 1, 1, tzinfo=pytz.UTC)), + ('20050607', datetime(2005, 6, 7, tzinfo=pytz.UTC)), + ('2006-07-08', datetime(2006, 7, 8, tzinfo=pytz.UTC)), + ('2007-8-9', datetime(2007, 8, 9, tzinfo=pytz.UTC)), +]) +def test_strpdate(dt, expected): + assert dates.strpdate(dt) == expected