From d08ac1730868531e81bf918e0362935e988428ca Mon Sep 17 00:00:00 2001 From: Radek Czajka Date: Wed, 10 Dec 2025 13:50:45 +0100 Subject: [PATCH] Use actual sync for bookmarks. --- requirements/requirements.txt | 2 +- src/bookmarks/models.py | 9 +++----- src/catalogue/api/views.py | 2 +- src/catalogue/models/book.py | 21 +++++++++++++++---- .../templates/catalogue/book_text.html | 4 +--- 5 files changed, 23 insertions(+), 15 deletions(-) diff --git a/requirements/requirements.txt b/requirements/requirements.txt index 396184d30..5f7496ec6 100644 --- a/requirements/requirements.txt +++ b/requirements/requirements.txt @@ -42,7 +42,7 @@ mutagen==1.47 sorl-thumbnail==12.10.0 # home-brewed & dependencies -librarian==24.5.10 +librarian==25.12 # celery tasks celery[redis]==5.4.0 diff --git a/src/bookmarks/models.py b/src/bookmarks/models.py index 8747ffbd3..7325e2d32 100644 --- a/src/bookmarks/models.py +++ b/src/bookmarks/models.py @@ -34,16 +34,13 @@ class Bookmark(Syncable, models.Model): audio_l = self.book.get_audio_length() except: audio_l = 60 + if self.anchor: self.mode = 'text' - if audio_l: - self.audio_timestamp = audio_l * .4 + self.audio_timestamp = self.book.sync_elid(self.anchor) if self.audio_timestamp: self.mode = 'audio' - if self.audio_timestamp > audio_l: - self.audio_timestamp = audio_l - if audio_l: - self.anchor = 'f20' + self.anchor = self.book.sync_ts(self.audio_timestamp) return super().save(*args, **kwargs) @classmethod diff --git a/src/catalogue/api/views.py b/src/catalogue/api/views.py index 7457b6f1b..0f3a95fa2 100644 --- a/src/catalogue/api/views.py +++ b/src/catalogue/api/views.py @@ -259,7 +259,7 @@ class BookSyncView(RetrieveAPIView): instance = self.get_object() return Response([ {"id": i, "timestamp": ts} - for (i, ts) in json.loads(instance.get_sync()) + for (i, ts) in instance.get_sync() ]) diff --git a/src/catalogue/models/book.py b/src/catalogue/models/book.py index 10ba5b23b..ee3aecb02 100644 --- a/src/catalogue/models/book.py +++ b/src/catalogue/models/book.py @@ -471,10 +471,9 @@ class Book(models.Model): None, ContentFile(sync) ) - def get_sync(self): if not self.has_sync_file(): - return '[]' + return [] with self.get_media('sync').first().file.open('r') as f: sync = f.read().split('\n') offset = float(sync[0]) @@ -484,8 +483,22 @@ class Book(models.Model): continue start, end, elid = line.split() items.append([elid, float(start) + offset]) - return json.dumps(items) - + return items + + def sync_ts(self, ts): + elid = None + for cur_id, t in self.get_sync(): + if ts >= t: + elid = cur_id + else: + break + return elid + + def sync_elid(self, elid): + for cur_id, t in self.get_sync(): + if cur_id == elid: + return t + def has_audio_epub_file(self): return self.has_media("audio.epub") diff --git a/src/catalogue/templates/catalogue/book_text.html b/src/catalogue/templates/catalogue/book_text.html index 18e4bcc43..e06f0f2a3 100644 --- a/src/catalogue/templates/catalogue/book_text.html +++ b/src/catalogue/templates/catalogue/book_text.html @@ -322,9 +322,7 @@
{% include 'catalogue/snippets/jplayer_reader.html' %}
- + {{ book.get_sync|json_script:"smil" }} {% endif %} -- 2.20.1