#!/bin/bash """ COMPLETE BOOK SCANNER - Orchestrates persistent session chunks to scan entire book Uses proven working persistent session approach """ TOTAL_PAGES=226 CHUNK_SIZE=25 # Conservative chunk size for reliability PROGRESS_FILE="scan_progress.json" echo "📚 COMPLETE KINDLE BOOK SCANNER" echo "===============================" echo "Total pages: $TOTAL_PAGES" echo "Chunk size: $CHUNK_SIZE pages" echo "" # Function to get last completed page get_last_page() { if [ -f "$PROGRESS_FILE" ]; then python3 -c " import json try: with open('$PROGRESS_FILE', 'r') as f: data = json.load(f) print(data.get('last_completed_page', 0)) except: print(0) " else echo 0 fi } # Check if session state exists if [ ! -f "kindle_session_state.json" ]; then echo "❌ No session state found. Initializing..." python3 persistent_scanner.py --init if [ $? -ne 0 ]; then echo "❌ Session initialization failed. Exiting." exit 1 fi echo "" fi # Main scanning loop chunk_number=1 total_chunks=$(( (TOTAL_PAGES + CHUNK_SIZE - 1) / CHUNK_SIZE )) echo "🚀 Starting complete book scan..." echo "" while true; do last_completed=$(get_last_page) next_start=$((last_completed + 1)) if [ "$next_start" -gt "$TOTAL_PAGES" ]; then echo "🏁 SCANNING COMPLETE!" echo "✅ All $TOTAL_PAGES pages have been scanned" break fi next_end=$((next_start + CHUNK_SIZE - 1)) if [ "$next_end" -gt "$TOTAL_PAGES" ]; then next_end=$TOTAL_PAGES fi echo "📦 CHUNK $chunk_number/$total_chunks" echo " Pages: $next_start to $next_end" echo " Progress: $last_completed/$TOTAL_PAGES completed ($(( last_completed * 100 / TOTAL_PAGES ))%)" echo "" # Run the persistent scanner python3 persistent_scanner.py --start-page "$next_start" --chunk-size "$CHUNK_SIZE" # Check if chunk completed successfully new_last_completed=$(get_last_page) if [ "$new_last_completed" -le "$last_completed" ]; then echo "❌ ERROR: Chunk failed or made no progress" echo " Last completed before: $last_completed" echo " Last completed after: $new_last_completed" echo "" echo "🔄 Retrying chunk in 10 seconds..." sleep 10 else echo "✅ Chunk completed successfully" echo " Scanned pages: $next_start to $new_last_completed" echo "" chunk_number=$((chunk_number + 1)) # Brief pause between chunks echo "⏳ Waiting 3 seconds before next chunk..." sleep 3 fi done echo "" echo "📊 FINAL SUMMARY" echo "================" final_count=$(get_last_page) echo "Total pages scanned: $final_count/$TOTAL_PAGES" echo "Files location: ./scanned_pages/" echo "Progress file: $PROGRESS_FILE" # Count actual files file_count=$(ls scanned_pages/page_*.png 2>/dev/null | wc -l) echo "Screenshot files: $file_count" if [ "$final_count" -eq "$TOTAL_PAGES" ]; then echo "" echo "🎉 SUCCESS: Complete book scan finished!" echo "📖 All $TOTAL_PAGES pages captured successfully" echo "💾 Ready for OCR processing and translation" # Show file size summary echo "" echo "📁 File size summary:" if [ -d "scanned_pages" ]; then total_size=$(du -sh scanned_pages | cut -f1) echo " Total size: $total_size" echo " Average per page: $(du -sk scanned_pages | awk -v pages=$file_count '{printf "%.1fKB", $1/pages}')" fi else echo "" echo "⚠️ Partial completion: $final_count/$TOTAL_PAGES pages" echo "You can resume by running this script again." fi echo "" echo "🎯 SCAN COMPLETED - Check scanned_pages/ directory for results"