Use actual sync for bookmarks.
authorRadek Czajka <rczajka@rczajka.pl>
Wed, 10 Dec 2025 12:50:45 +0000 (13:50 +0100)
committerRadek Czajka <rczajka@rczajka.pl>
Wed, 10 Dec 2025 12:50:45 +0000 (13:50 +0100)
requirements/requirements.txt
src/bookmarks/models.py
src/catalogue/api/views.py
src/catalogue/models/book.py
src/catalogue/templates/catalogue/book_text.html

index 396184d..5f7496e 100644 (file)
@@ -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
index 8747ffb..7325e2d 100644 (file)
@@ -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
index 7457b6f..0f3a95f 100644 (file)
@@ -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()
         ])
 
     
index 10ba5b2..ee3aecb 100644 (file)
@@ -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")
 
index 18e4bcc..e06f0f2 100644 (file)
     <div id="player-bar">
       {% include 'catalogue/snippets/jplayer_reader.html' %}
     </div>
-    <script type="application/json" id="smil">
-     {{ book.get_sync|safe }}
-    </script>
+    {{ book.get_sync|json_script:"smil" }}
   {% endif %}