From ce0ae66a9ec19ea738a55a7603d2c9487878bad1 Mon Sep 17 00:00:00 2001 From: Roland Date: Tue, 12 Mar 2024 11:06:30 -0600 Subject: [PATCH] Storing video download_datetime. --- app/src/components/Videos.js | 1 + modules/videos/conftest.py | 2 +- modules/videos/lib.py | 8 ++++++-- modules/videos/test/test_lib.py | 3 ++- modules/videos/video/lib.py | 5 +++-- wrolpi/common.py | 2 +- 6 files changed, 14 insertions(+), 7 deletions(-) diff --git a/app/src/components/Videos.js b/app/src/components/Videos.js index 0d9fa4ea..9684f4da 100644 --- a/app/src/components/Videos.js +++ b/app/src/components/Videos.js @@ -98,6 +98,7 @@ function VideosPage() { {value: 'size', text: 'Size'}, {value: 'view_count', text: 'Views'}, {value: 'viewed', text: 'Recently Viewed', short: 'R.Viewed'}, + {value: 'download_datetime', text: 'Download Date', short: 'D.Date'}, ] if (searchStr) { diff --git a/modules/videos/conftest.py b/modules/videos/conftest.py index cc7a6a13..60fd3ad3 100644 --- a/modules/videos/conftest.py +++ b/modules/videos/conftest.py @@ -113,7 +113,7 @@ def video_factory(test_session, test_directory): info_json_path = None if with_info_json: # Use the provided object as the json. - with_info_json = {'duration': 5} if with_info_json is True else with_info_json + with_info_json = {'duration': 5, 'epoch': 12345678} if with_info_json is True else with_info_json info_json_path = path.with_suffix('.info.json') info_json_path.write_text(json.dumps(with_info_json)) diff --git a/modules/videos/lib.py b/modules/videos/lib.py index 7f7c2900..ef43c2dd 100644 --- a/modules/videos/lib.py +++ b/modules/videos/lib.py @@ -14,7 +14,7 @@ from yt_dlp import YoutubeDL from wrolpi import before_startup, dates from wrolpi.captions import extract_captions from wrolpi.common import ConfigFile, get_media_directory, register_refresh_cleanup, limit_concurrent -from wrolpi.dates import Seconds +from wrolpi.dates import Seconds, from_timestamp from wrolpi.db import get_db_curs, get_db_session, optional_session from wrolpi.errors import UnknownDirectory from wrolpi.vars import PYTEST @@ -34,6 +34,7 @@ class VideoInfoJSON(object): title = None url = None view_count = None + epoch = None def process_video_info_json(video: Video) -> VideoInfoJSON: @@ -49,6 +50,8 @@ def process_video_info_json(video: Video) -> VideoInfoJSON: video_info_json.view_count = info_json.get('view_count') or None video_info_json.url = info_json.get('webpage_url') or info_json.get('url') or None video_info_json.channel_source_id = info_json.get('channel_id') or None + video_info_json.epoch = info_json.get('epoch') + video_info_json.epoch = int(video_info_json.epoch) if video_info_json.epoch else None return video_info_json @@ -78,13 +81,14 @@ 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.file_group.length) and bool(video.view_count) \ - and bool(video.file_group.url) + and bool(video.file_group.url) and bool(video.file_group.download_datetime) 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) video.file_group.title = video_info_json.title video.file_group.length = video_info_json.duration video.file_group.url = video_info_json.url + video.file_group.download_datetime = from_timestamp(video_info_json.epoch) if video_info_json.epoch else None # View count will probably be overwritten by more recent data when this Video's Channel is # updated. video.view_count = video.view_count or video_info_json.view_count diff --git a/modules/videos/test/test_lib.py b/modules/videos/test/test_lib.py index 12fc550a..6e4743ad 100644 --- a/modules/videos/test/test_lib.py +++ b/modules/videos/test/test_lib.py @@ -67,7 +67,7 @@ async def test_video_factory(test_session, video_factory, channel_factory): def test_validate_video(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_20050607_1234567890_The Title.mp4') - vid1 = video_factory(with_video_file=video_file) + vid1 = video_factory(with_video_file=video_file, with_info_json=True) # Clear source_id so source_id in the file will be extracted. vid1.source_id = None assert not vid1.poster_path @@ -80,6 +80,7 @@ def test_validate_video(test_directory, video_factory, image_bytes_factory, vide assert vid1.poster_path.suffix == '.jpg' # File name date is assumed to be local timezone. assert vid1.file_group.published_datetime and vid1.file_group.published_datetime.year == 2005 + assert vid1.file_group.download_datetime assert vid1.source_id == '1234567890' assert vid1.file_group.title == 'The Title' diff --git a/modules/videos/video/lib.py b/modules/videos/video/lib.py index 9ea3a059..f9a1fd04 100644 --- a/modules/videos/video/lib.py +++ b/modules/videos/video/lib.py @@ -52,6 +52,8 @@ VIDEO_ORDERS = { '-viewed': 'fg.viewed DESC', 'view_count': 'v.view_count ASC', '-view_count': 'v.view_count DESC', + 'download_datetime': 'fg.download_datetime ASC, LOWER(fg.primary_path) ASC', + '-download_datetime': 'fg.download_datetime DESC NULLS LAST, LOWER(fg.primary_path) ASC', } NO_NULL_ORDERS = { 'viewed': 'fg.viewed IS NOT NULL', @@ -63,8 +65,7 @@ NO_NULL_ORDERS = { 'view_count': 'v.view_count IS NOT NULL', '-view_count': 'v.view_count IS NOT NULL', } -JOIN_ORDERS = ('published_datetime', '-published_datetime', 'viewed', '-viewed', 'view_count', '-view_count', - 'length', '-length') +JOIN_ORDERS = ('view_count', '-view_count') DEFAULT_VIDEO_ORDER = 'rank' VIDEO_QUERY_LIMIT = 24 diff --git a/wrolpi/common.py b/wrolpi/common.py index 3aba4c37..5466533b 100644 --- a/wrolpi/common.py +++ b/wrolpi/common.py @@ -1699,6 +1699,6 @@ def get_title_from_html(html: str, url: str = None) -> str: """ soup = get_html_soup(html) try: - return soup.title.string + return soup.title.string.strip() except Exception: # noqa logger.debug(f'Unable to extract title {url}')