Videos can be sorted by their modification date.

This commit is contained in:
Roland 2022-01-25 08:51:55 -07:00
parent c264354f23
commit 0315def174
No known key found for this signature in database
GPG key ID: 9D6DBD3ACF11AB93
9 changed files with 101 additions and 17 deletions

View file

@ -10,6 +10,7 @@ from sqlalchemy.orm import Session
from wrolpi.common import logger, chunks, get_config
from wrolpi.common import save_settings_config
from wrolpi.dates import from_timestamp
from wrolpi.db import get_db_curs, get_db_session, optional_session
from wrolpi.media_path import MediaPath
from wrolpi.vars import PYTEST
@ -188,8 +189,12 @@ def validate_video(video: Video, channel_generate_poster: bool):
video.duration = get_video_duration(video_path)
if not video.caption and video.caption_path:
video.caption = get_video_captions(video)
if not video.size:
video.size = video_path.stat().st_size
if not video.size or not video.modification_datetime:
stat = video_path.stat()
video.size = video.size or stat.st_size
video.modification_datetime = video.modification_datetime or from_timestamp(stat.st_mtime)
if not video.poster_path:
# Video poster is not found, lets check near the video file.
for ext in ('.jpg', '.jpeg', '.webp', '.png'):

View file

@ -38,6 +38,7 @@ class Video(ModelHelper, Base):
censored = Column(Boolean, default=False)
duration = Column(Integer)
favorite = Column(TZDateTime)
modification_datetime = Column(TZDateTime)
size = Column(Integer)
source_id = Column(String)
title = Column(String)
@ -237,6 +238,7 @@ class Video(ModelHelper, Base):
caption_path=self.caption_path,
channel=self.channel,
channel_id=self.channel_id,
modification_datetime=self.modification_datetime,
duration=self.duration,
favorite=self.favorite,
id=self.id,

View file

@ -9,7 +9,7 @@ import sqlalchemy
from modules.videos.channel import lib
from modules.videos.common import apply_info_json
from modules.videos.lib import validate_videos, parse_video_file_name
from modules.videos.lib import validate_videos, parse_video_file_name, upsert_video
from modules.videos.models import Channel, Video
from modules.videos.video.lib import video_search
from wrolpi.dates import local_timezone
@ -267,3 +267,15 @@ def test_validate_does_not_generate_when_disabled(test_session, channel_factory,
assert not vid1.poster_path
assert vid2.poster_path
assert str(vid3.poster_path).endswith('webp')
def test_video_modification_datetime(test_session, simple_channel, video_file):
"""
A Video has a modification_datetime which represents the video file's mtime.
"""
expected = local_timezone(datetime(2000, 1, 1, 0, 0, 0))
with mock.patch('modules.videos.lib.from_timestamp') as mock_from_timestamp:
mock_from_timestamp.return_value = expected
video = upsert_video(test_session, video_file, simple_channel)
test_session.commit()
assert video.modification_datetime == expected

View file

@ -55,6 +55,8 @@ VIDEO_ORDERS = {
'-viewed': 'viewed DESC',
'view_count': 'view_count ASC',
'-view_count': 'view_count DESC',
'modification_datetime': 'modification_datetime ASC',
'-modification_datetime': 'modification_datetime DESC',
}
NO_NULL_ORDERS = {
'viewed': '\nAND viewed IS NOT NULL',
@ -65,6 +67,8 @@ NO_NULL_ORDERS = {
'-size': '\nAND size IS NOT NULL',
'view_count': '\nAND view_count IS NOT NULL',
'-view_count': '\nAND view_count IS NOT NULL',
'modification_datetime': '\nAND modification_datetime IS NOT NULL',
'-modification_datetime': '\nAND modification_datetime IS NOT NULL',
}
DEFAULT_VIDEO_ORDER = 'rank'
VIDEO_QUERY_LIMIT = 20