Skip to content

Commit cc6c475

Browse files
committed
Location or Page
1 parent 8974f57 commit cc6c475

File tree

2 files changed

+27
-28
lines changed

2 files changed

+27
-28
lines changed

server/resources/book_open_resource.py

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,7 @@
1515
from server.middleware.profile_middleware import ensure_user_profile_loaded
1616
from server.middleware.request_deduplication_middleware import deduplicate_request
1717
from server.middleware.response_handler import handle_automator_response
18-
from server.utils.ocr_utils import (
19-
KindleOCR,
20-
cycle_page_indicator_if_needed,
21-
is_base64_requested,
22-
is_ocr_requested,
23-
parse_page_indicators,
24-
process_screenshot_with_regions,
25-
)
18+
from server.utils.ocr_utils import is_base64_requested, is_ocr_requested
2619
from server.utils.request_utils import get_sindarin_email
2720
from views.core.app_state import AppState
2821

@@ -235,28 +228,29 @@ def capture_book_state(already_open=False):
235228
screenshot_path = os.path.join(automator.screenshots_dir, f"{screenshot_id}.png")
236229
automator.driver.save_screenshot(screenshot_path)
237230

238-
# Get OCR text and page indicators
239-
with open(screenshot_path, "rb") as img_file:
240-
image_data = img_file.read()
231+
# Import process_screenshot_response
232+
from server.utils.ocr_utils import process_screenshot_response
241233

242-
# Process screenshot with regions to extract both main text and page indicators
243-
ocr_results = process_screenshot_with_regions(image_data)
234+
# Use process_screenshot_response to get both OCR text and page indicators
235+
ocr_data = process_screenshot_response(
236+
screenshot_id, screenshot_path, use_base64=False, perform_ocr=True
237+
)
244238

245-
# Add main text if available
246-
if ocr_results.get("main_text"):
247-
response_data["ocr_text"] = ocr_results["main_text"]
239+
# Add OCR text if extracted
240+
if "ocr_text" in ocr_data:
241+
response_data["ocr_text"] = ocr_data["ocr_text"]
248242

249-
# Add page indicators if extracted
250-
page_indicator_text = ocr_results.get("page_indicator_text")
251-
percentage_text = ocr_results.get("percentage_text")
243+
# Update progress if page indicators were extracted
244+
if "progress" in ocr_data and ocr_data["progress"]:
245+
# Merge the OCR-extracted progress with existing progress
246+
if response_data.get("progress"):
247+
response_data["progress"].update(ocr_data["progress"])
248+
else:
249+
response_data["progress"] = ocr_data["progress"]
252250

253-
if page_indicator_text or percentage_text:
254-
# Use the cycle function which will tap if needed for time-based indicators
255-
page_indicators = cycle_page_indicator_if_needed(
256-
automator.driver, page_indicator_text, percentage_text
257-
)
258-
if page_indicators:
259-
response_data["progress"].update(page_indicators)
251+
# Add any OCR error if present
252+
if "ocr_error" in ocr_data:
253+
response_data["ocr_error"] = ocr_data["ocr_error"]
260254

261255
# Delete the temporary screenshot
262256
try:

tests/test_01_api_integration.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,7 @@ def test_open_random_book(self):
598598
[
599599
progress.get("page") is not None,
600600
progress.get("location") is not None,
601+
progress.get("current_location") is not None,
601602
progress.get("percentage") is not None,
602603
]
603604
)
@@ -608,6 +609,8 @@ def test_open_random_book(self):
608609
print(f"Extracted page: {progress['page']}")
609610
if progress.get("location"):
610611
print(f"Extracted location: {progress['location']}")
612+
if progress.get("current_location"):
613+
print(f"Extracted current_location: {progress['current_location']}")
611614
if progress.get("percentage") is not None:
612615
print(f"Extracted percentage: {progress['percentage']}%")
613616

@@ -656,8 +659,10 @@ def test_navigate_preview(self):
656659

657660
# Check that progress contains expected fields
658661
assert "percentage" in progress, f"Progress missing percentage field: {progress}"
659-
assert "current_page" in progress, f"Progress missing current_page field: {progress}"
660-
assert "total_pages" in progress, f"Progress missing total_pages field: {progress}"
662+
# Accept either page or location information
663+
has_page_info = ("current_page" in progress and "total_pages" in progress)
664+
has_location_info = ("current_location" in progress and "total_locations" in progress)
665+
assert has_page_info or has_location_info, f"Progress missing page/location fields: {progress}"
661666

662667
# At least one of these should have a non-null value from OCR
663668
has_valid_data = any(

0 commit comments

Comments
 (0)