Browse Source

Fix sync to include all listening history

- Changed sync to use /api/me endpoint to get full mediaProgress
- Now syncs finished books, not just in-progress items
- Use actual start/finish timestamps from Audiobookshelf
- Skip podcast episodes, only sync books
- Successfully syncs 100+ books from listening history

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Brad Lance 3 tháng trước cách đây
mục cha
commit
f58ef2b8d6
1 tập tin đã thay đổi với 29 bổ sung14 xóa
  1. 29 14
      app/main.py

+ 29 - 14
app/main.py

@@ -85,17 +85,31 @@ async def home(request: Request, db: AsyncSession = Depends(get_db)):
 async def sync_with_audiobookshelf(db: AsyncSession = Depends(get_db)):
     """Sync library and progress from Audiobookshelf."""
     try:
-        # Get user's progress
-        progress_items = await abs_client.get_user_progress()
+        # Get user info which includes all media progress
+        user_info = await abs_client.get_user_info()
+        media_progress = user_info.get("mediaProgress", [])
 
         synced_count = 0
-        for item in progress_items:
+        for progress_item in media_progress:
+            # Skip podcast episodes, only process books
+            if progress_item.get("mediaItemType") != "book":
+                continue
+
+            library_item_id = progress_item.get("libraryItemId")
+            if not library_item_id:
+                continue
+
+            # Fetch full library item details
+            try:
+                item = await abs_client.get_item_details(library_item_id)
+            except:
+                # Skip if item not found
+                continue
             # Extract book info
-            library_item = item.get("libraryItem", {})
-            media = library_item.get("media", {})
+            media = item.get("media", {})
             metadata = media.get("metadata", {})
 
-            book_id = library_item.get("id")
+            book_id = item.get("id")
             if not book_id:
                 continue
 
@@ -124,9 +138,11 @@ async def sync_with_audiobookshelf(db: AsyncSession = Depends(get_db)):
                 book.updated_at = datetime.now()
 
             # Update or create listening session
-            progress_data = item.get("progress", 0)
-            current_time = item.get("currentTime", 0)
-            is_finished = item.get("isFinished", False)
+            progress_data = progress_item.get("progress", 0)
+            current_time = progress_item.get("currentTime", 0)
+            is_finished = progress_item.get("isFinished", False)
+            started_at_ts = progress_item.get("startedAt")
+            finished_at_ts = progress_item.get("finishedAt")
 
             session_result = await db.execute(
                 select(ListeningSession)
@@ -142,18 +158,17 @@ async def sync_with_audiobookshelf(db: AsyncSession = Depends(get_db)):
                     progress=progress_data,
                     current_time=current_time,
                     is_finished=is_finished,
-                    started_at=datetime.now()
+                    started_at=datetime.fromtimestamp(started_at_ts / 1000) if started_at_ts else datetime.now(),
+                    finished_at=datetime.fromtimestamp(finished_at_ts / 1000) if finished_at_ts else None
                 )
-                if is_finished:
-                    session.finished_at = datetime.now()
                 db.add(session)
             else:
                 # Update existing session
                 session.progress = progress_data
                 session.current_time = current_time
                 session.is_finished = is_finished
-                if is_finished and not session.finished_at:
-                    session.finished_at = datetime.now()
+                if finished_at_ts and not session.finished_at:
+                    session.finished_at = datetime.fromtimestamp(finished_at_ts / 1000)
 
             synced_count += 1