Skip to content

Commit 02c8b97

Browse files
authored
feat(kosync): push KOReader reading progress to Hardcover after status update
* Push reading progress from koreader to Hardcover * Remove commented out code * Revert debug logging changes to hardcover client
1 parent 852bd99 commit 02c8b97

2 files changed

Lines changed: 26 additions & 10 deletions

File tree

cps/kobo.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -975,7 +975,7 @@ def HandleStateRequest(book_uuid):
975975
ub.session.rollback()
976976
abort(400, description="Malformed request data is missing 'ReadingStates' key")
977977

978-
push_reading_state_to_hardcover(book, request_bookmark)
978+
push_reading_state_to_hardcover(current_user, book, request_bookmark['ProgressPercent'])
979979

980980
ub.session.merge(kobo_reading_state)
981981
ub.session_commit()
@@ -985,15 +985,15 @@ def HandleStateRequest(book_uuid):
985985
})
986986

987987

988-
def push_reading_state_to_hardcover(book: db.Books, request_bookmark: dict):
988+
def push_reading_state_to_hardcover(user, book: db.Books, progress_percentage: int):
989989
"""
990990
Sync reading progress to Hardcover if enabled for the user and book is not blacklisted.
991991
992992
Most exceptions are caught and logged so that issues with Hardcover do not prevent
993993
the Kobo from clearing its reading state sync queue.
994994
995995
:param book: The book for which to sync reading progress.
996-
:param request_bookmark: The bookmark data from the Kobo request.
996+
:param progress_percentage: Reading progress percentage.
997997
:return: None
998998
"""
999999

@@ -1009,16 +1009,16 @@ def push_reading_state_to_hardcover(book: db.Books, request_bookmark: dict):
10091009
return
10101010

10111011
try:
1012-
hardcoverClient = hardcover.HardcoverClient(current_user.hardcover_token)
1012+
hardcoverClient = hardcover.HardcoverClient(user.hardcover_token)
10131013
except hardcover.MissingHardcoverToken:
1014-
log.info(f"User {current_user.name} has no Hardcover token, not syncing reading progress to Hardcover")
1014+
log.info(f"User {user.name} has no Hardcover token, not syncing reading progress to Hardcover")
10151015
return
10161016
except Exception as e:
1017-
log.error(f"Failed to create Hardcover client for user {current_user.name}: {e}")
1017+
log.error(f"Failed to create Hardcover client for user {user.name}: {e}")
10181018
return
10191019

10201020
try:
1021-
hardcoverClient.update_reading_progress(book.identifiers, request_bookmark["ProgressPercent"])
1021+
hardcoverClient.update_reading_progress(book.identifiers, progress_percentage)
10221022
except Exception as e:
10231023
log.error(f"Failed to update reading progress for book {book.id} in Hardcover: {e}")
10241024

cps/progress_syncing/protocols/kosync.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@
4040
from datetime import datetime, timezone
4141
from typing import Dict, Optional, Any, Tuple
4242

43+
from ...services import SyncToken as SyncToken, hardcover
44+
from ...kobo import push_reading_state_to_hardcover
45+
4346
from flask import Blueprint, request, jsonify
4447
from flask_babel import gettext as _
4548
from werkzeug.security import check_password_hash
@@ -190,7 +193,7 @@ def authenticate_user() -> Optional[ub.User]:
190193
if login_result:
191194
log.info(f"authenticate_user: Successfully authenticated user via LDAP: {user.name}")
192195
return user
193-
196+
194197
# Log LDAP failure but continue to local check (fallback)
195198
# We use debug level here because failure is expected if the user is using a local password
196199
if error:
@@ -326,7 +329,7 @@ def enrich_response_with_book_info(response_data: Dict[str, Any], document_check
326329
return response_data, book_id, book_format, book_title, checksum_version
327330

328331

329-
def update_book_read_status(user_id: int, book_id: int, percentage: float) -> None:
332+
def update_book_read_status(user, book_id: int, percentage: float) -> None:
330333
"""
331334
Update the user's ReadBook status based on reading progress percentage.
332335
@@ -360,6 +363,8 @@ def update_book_read_status(user_id: int, book_id: int, percentage: float) -> No
360363
else:
361364
new_status = ub.ReadBook.STATUS_UNREAD
362365

366+
user_id = user.id
367+
363368
log.debug(f"update_book_read_status: user {user_id}, book {book_id}, "
364369
f"percentage {percentage:.2f}% -> status {new_status}")
365370

@@ -717,10 +722,21 @@ def update_progress():
717722
# This is done AFTER kosync_progress is committed, so sync location is always safe
718723
if book_id:
719724
try:
720-
update_book_read_status(user.id, book_id, percentage_float)
725+
update_book_read_status(user, book_id, percentage_float)
721726
ub.session.commit()
722727
log.info(f"Updated ReadBook status: user={user.id}, book={book_id} "
723728
f"({book_title}), status based on {percentage_float:.1f}%")
729+
730+
# Push to Hardcover
731+
from ... import calibre_db
732+
book = calibre_db.get_book(book_id)
733+
734+
if user is not None:
735+
log.debug(f"Going to sync book {book_id} to Hardcover.")
736+
push_reading_state_to_hardcover(user, book, int(percentage_float))
737+
else:
738+
log.debug(f"Book {book_id} not syncing to Hardcover, no matched user.")
739+
724740
except SQLAlchemyError as e:
725741
log.error(f"Failed to update ReadBook status for book {book_id}: {e}")
726742
# Rollback only affects the failed ReadBook update

0 commit comments

Comments
 (0)