From ddf90122103df88aba9d7ad1635a1bd6624e68a6 Mon Sep 17 00:00:00 2001 From: tcely Date: Thu, 1 May 2025 08:42:56 -0400 Subject: [PATCH 1/7] Handle `AssertionError` for timestamp --- tubesync/sync/tasks.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/tubesync/sync/tasks.py b/tubesync/sync/tasks.py index 81dd4b5d..ed628958 100644 --- a/tubesync/sync/tasks.py +++ b/tubesync/sync/tasks.py @@ -330,9 +330,14 @@ def index_source_task(source_id): media.duration = float(video.get(fields('duration', media), None) or 0) or None media.title = str(video.get(fields('title', media), ''))[:200] timestamp = video.get(fields('timestamp', media), None) - published_dt = media.metadata_published(timestamp) - if published_dt is not None: - media.published = published_dt + if timestamp is not None: + try: + published_dt = media.metadata_published(timestamp) + except AssertionError: + pass + else: + if published_dt: + media.published = published_dt try: media.save() except IntegrityError as e: From f856caa0245fdef50a2f921280f215bf9b94e67d Mon Sep 17 00:00:00 2001 From: tcely Date: Thu, 1 May 2025 09:21:21 -0400 Subject: [PATCH 2/7] Remove assertion --- tubesync/sync/models.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tubesync/sync/models.py b/tubesync/sync/models.py index eec9d7ca..3b1f66a9 100644 --- a/tubesync/sync/models.py +++ b/tubesync/sync/models.py @@ -1312,7 +1312,6 @@ class Media(models.Model): return self.get_metadata_first_value(('fulltitle', 'title',), '') def ts_to_dt(self, /, timestamp): - assert timestamp is not None try: timestamp_float = float(timestamp) except Exception as e: From 0b37aa3d9f88a4c99e872eac1643a122fa143e31 Mon Sep 17 00:00:00 2001 From: tcely Date: Thu, 1 May 2025 09:26:14 -0400 Subject: [PATCH 3/7] Call `Media.ts_to_dt` function instead --- tubesync/sync/tasks.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tubesync/sync/tasks.py b/tubesync/sync/tasks.py index ed628958..79f3b375 100644 --- a/tubesync/sync/tasks.py +++ b/tubesync/sync/tasks.py @@ -332,7 +332,7 @@ def index_source_task(source_id): timestamp = video.get(fields('timestamp', media), None) if timestamp is not None: try: - published_dt = media.metadata_published(timestamp) + published_dt = media.ts_to_dt(timestamp) except AssertionError: pass else: From 39be70454509605c8ea65f7cd5d019cdc8b2d6d0 Mon Sep 17 00:00:00 2001 From: tcely Date: Thu, 1 May 2025 09:44:27 -0400 Subject: [PATCH 4/7] Switch to `Media.ts_to_dt` function --- tubesync/sync/tasks.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/tubesync/sync/tasks.py b/tubesync/sync/tasks.py index 79f3b375..39280059 100644 --- a/tubesync/sync/tasks.py +++ b/tubesync/sync/tasks.py @@ -505,9 +505,17 @@ def download_media_metadata(media_id): # Media must have a valid upload date if upload_date: media.published = timezone.make_aware(upload_date) - published = media.metadata_published() - if published: - media.published = published + timestamp = media.get_metadata_first_value( + ('release_timestamp', 'timestamp',), + arg_dict=response, + ) + try: + published_dt = media.ts_to_dt(timestamp) + except AssertionError: + pass + else: + if published_dt: + media.published = published_dt # Store title in DB so it's fast to access if media.metadata_title: From c52fd14cd0c206ac750d03930bc75071d7bfe6bb Mon Sep 17 00:00:00 2001 From: tcely Date: Thu, 1 May 2025 09:46:48 -0400 Subject: [PATCH 5/7] Remove `Media.metadata_published` function --- tubesync/sync/models.py | 7 ------- 1 file changed, 7 deletions(-) diff --git a/tubesync/sync/models.py b/tubesync/sync/models.py index 3b1f66a9..05eaf854 100644 --- a/tubesync/sync/models.py +++ b/tubesync/sync/models.py @@ -1321,13 +1321,6 @@ class Media(models.Model): return self.posix_epoch + timedelta(seconds=timestamp_float) return None - def metadata_published(self, timestamp=None): - if timestamp is None: - timestamp = self.get_metadata_first_value( - ('release_timestamp', 'timestamp',) - ) - return self.ts_to_dt(timestamp) - @property def slugtitle(self): replaced = self.title.replace('_', '-').replace('&', 'and').replace('+', 'and') From 19a2d03c5b7c436c31ff951e96d52deb50573c96 Mon Sep 17 00:00:00 2001 From: tcely Date: Thu, 1 May 2025 09:50:49 -0400 Subject: [PATCH 6/7] Remove an unneeded `if` --- tubesync/sync/tasks.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/tubesync/sync/tasks.py b/tubesync/sync/tasks.py index 39280059..5e727e6d 100644 --- a/tubesync/sync/tasks.py +++ b/tubesync/sync/tasks.py @@ -330,14 +330,13 @@ def index_source_task(source_id): media.duration = float(video.get(fields('duration', media), None) or 0) or None media.title = str(video.get(fields('title', media), ''))[:200] timestamp = video.get(fields('timestamp', media), None) - if timestamp is not None: - try: - published_dt = media.ts_to_dt(timestamp) - except AssertionError: - pass - else: - if published_dt: - media.published = published_dt + try: + published_dt = media.ts_to_dt(timestamp) + except AssertionError: + pass + else: + if published_dt: + media.published = published_dt try: media.save() except IntegrityError as e: From f850502ad05a12184dd186c491ad3c443f951d94 Mon Sep 17 00:00:00 2001 From: tcely Date: Thu, 1 May 2025 10:24:44 -0400 Subject: [PATCH 7/7] Narrow the exceptions --- tubesync/sync/models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tubesync/sync/models.py b/tubesync/sync/models.py index 05eaf854..a5e3d675 100644 --- a/tubesync/sync/models.py +++ b/tubesync/sync/models.py @@ -1314,7 +1314,7 @@ class Media(models.Model): def ts_to_dt(self, /, timestamp): try: timestamp_float = float(timestamp) - except Exception as e: + except (TypeError, ValueError,) as e: log.warn(f'Could not compute published from timestamp for: {self.source} / {self} with "{e}"') pass else: