From df849ffb421787cac8441170ac2aa0ba8ec8705d Mon Sep 17 00:00:00 2001 From: Roland Date: Mon, 26 Feb 2024 13:40:39 -0700 Subject: [PATCH] Ordering video/archive results when no published_datetime exists. Adding Video.location property which returns the App location of the video. --- modules/archive/lib.py | 4 ++-- modules/videos/downloader.py | 13 ++----------- modules/videos/models.py | 10 ++++++++++ modules/videos/video/lib.py | 5 +++-- 4 files changed, 17 insertions(+), 15 deletions(-) diff --git a/modules/archive/lib.py b/modules/archive/lib.py index a0290eff..d1bd993d 100644 --- a/modules/archive/lib.py +++ b/modules/archive/lib.py @@ -418,8 +418,8 @@ def get_domains(): ARCHIVE_ORDERS = { - 'published_datetime': (date := 'fg.published_datetime ASC, fg.download_datetime ASC'), - '-published_datetime': (_date := 'fg.published_datetime DESC NULLS LAST, fg.download_datetime DESC NULLS LAST'), + 'published_datetime': (date := 'COALESCE(fg.published_datetime, fg.download_datetime) ASC'), + '-published_datetime': (_date := 'COALESCE(fg.published_datetime, fg.download_datetime) DESC NULLS LAST'), 'published_modified_datetime': f'fg.published_modified_datetime ASC NULLS LAST, {date}', '-published_modified_datetime': f'fg.published_modified_datetime DESC, {_date}', 'download_datetime': 'fg.download_datetime ASC', diff --git a/modules/videos/downloader.py b/modules/videos/downloader.py index 7851bdcb..2785fb0d 100755 --- a/modules/videos/downloader.py +++ b/modules/videos/downloader.py @@ -209,14 +209,9 @@ class VideoDownloader(Downloader, ABC): url = normalize_video_url(download.url) # Video may have been downloaded previously, get its location for error reporting. - location = None with get_db_session() as session: videos = session.query(Video).join(FileGroup).filter_by(url=url).all() - video = videos[0] if videos else None - if video and video.channel_id: - location = f'/videos/channel/{video.channel_id}/video/{video.id}' - elif video: - location = f'/videos/video/{video.id}' + location = videos[0].location if videos else None try: download.info_json = download.info_json or extract_info(url) @@ -391,6 +386,7 @@ class VideoDownloader(Downloader, ABC): if name not in existing_names: video.add_tag(name) need_commit = True + location = video.location if need_commit: session.commit() @@ -439,11 +435,6 @@ class VideoDownloader(Downloader, ABC): error = str(traceback.format_exc()) return DownloadResult(success=False, error=error, location=location) - if channel_id: - location = f'/videos/channel/{channel_id}/video/{video_id}' - else: - location = f'/videos/video/{video_id}' - logger.debug(f'Downloaded video {location=}') logger.info(f'Successfully downloaded video {url} {video}') diff --git a/modules/videos/models.py b/modules/videos/models.py index 1b708d21..dc38f219 100644 --- a/modules/videos/models.py +++ b/modules/videos/models.py @@ -467,6 +467,16 @@ class Video(ModelHelper, Base): return True + @property + def location(self) -> str: + """The href of the video in the App.""" + if not self.id: + raise RuntimeError('Video id not set. Flush to DB.') + + if self.channel_id: + return f'/videos/channel/{self.channel_id}/video/{self.id}' + return f'/videos/video/{self.id}' + class Channel(ModelHelper, Base): __tablename__ = 'channel' diff --git a/modules/videos/video/lib.py b/modules/videos/video/lib.py index 2796d30f..bef46ed5 100644 --- a/modules/videos/video/lib.py +++ b/modules/videos/video/lib.py @@ -30,8 +30,9 @@ def get_video_for_app(video_id: int) -> Tuple[dict, Optional[dict], Optional[dic VIDEO_ORDERS = { - 'published_datetime': 'fg.published_datetime ASC, LOWER(fg.primary_path) ASC', - '-published_datetime': 'fg.published_datetime DESC NULLS LAST, LOWER(fg.primary_path) ASC', + 'published_datetime': 'COALESCE(fg.published_datetime, fg.download_datetime) ASC, LOWER(fg.primary_path) ASC', + '-published_datetime': 'COALESCE(fg.published_datetime, fg.download_datetime) DESC NULLS LAST,' + ' LOWER(fg.primary_path) ASC', 'rank': '2 DESC, LOWER(fg.primary_path) DESC', '-rank': '2 ASC, LOWER(fg.primary_path) ASC', 'size': 'fg.size ASC, LOWER(fg.primary_path) ASC',