import sys import os from pathlib import Path from ebooklib import epub import ebooklib from loguru import logger # Add project root to path sys.path.insert(0, str(Path(__file__).parent)) from src.utils import load_config def compare_epubs(original_path, new_path): print(f"🔍 Comparing EPUBs:\n Original: {original_path}\n New: {new_path}") print("=" * 60) if not os.path.exists(new_path): print(f"❌ New EPUB not found: {new_path}") return try: orig_book = epub.read_epub(original_path) new_book = epub.read_epub(new_path) except Exception as e: print(f"❌ Error reading EPUBs: {e}") return # 1. Metadata Comparison print("\n[1] Metadata Comparison") print("-" * 60) namespaces = ['DC', 'OPF'] for ns in namespaces: orig_meta = orig_book.metadata.get(ns, {}) new_meta = new_book.metadata.get(ns, {}) all_keys = set(orig_meta.keys()) | set(new_meta.keys()) for key in sorted(all_keys): orig_vals = [v[0] for v in orig_meta.get(key, [])] new_vals = [v[0] for v in new_meta.get(key, [])] if orig_vals != new_vals: print(f" ⚠️ {ns}:{key} Changed:") print(f" Orig: {orig_vals}") print(f" New: {new_vals}") else: # print(f" ✅ {ns}:{key} match") pass # Special check for Cover print("\n[2] Cover Image Check") print("-" * 60) # Check via Metadata orig_cover_meta = orig_book.get_metadata('OPF', 'cover') new_cover_meta = new_book.get_metadata('OPF', 'cover') print(f" Original Cover Meta (OPF): {orig_cover_meta}") print(f" New Cover Meta (OPF): {new_cover_meta}") # Check via Manifest Items orig_cover_items = [i for i in orig_book.get_items() if 'cover' in i.get_name().lower() and i.media_type.startswith('image/')] new_cover_items = [i for i in new_book.get_items() if 'cover' in i.get_name().lower() and i.media_type.startswith('image/')] print(f" Original Cover Image Items: {[i.get_name() for i in orig_cover_items]}") print(f" New Cover Image Items: {[i.get_name() for i in new_cover_items]}") # 3. Spine Comparison (Reading Order) print("\n[3] Spine (Reading Order) Comparison") print("-" * 60) orig_spine_ids = [item[0] for item in orig_book.spine] new_spine_ids = [item[0] for item in new_book.spine] print(f" Original Spine Length: {len(orig_spine_ids)}") print(f" New Spine Length: {len(new_spine_ids)}") # Map IDs to Filenames for better readability def get_filename(book, item_id): item = book.get_item_with_id(item_id) return item.get_name() if item else "UNKNOWN" # Compare first few and last few limit = 5 print(f" First {limit} items:") for i in range(min(len(orig_spine_ids), len(new_spine_ids), limit)): f_orig = get_filename(orig_book, orig_spine_ids[i]) f_new = get_filename(new_book, new_spine_ids[i]) status = "✅" if f_orig == f_new else "❌" print(f" {i+1}. {status} Orig: {f_orig} | New: {f_new}") # Check for missing items in spine orig_filenames = set(get_filename(orig_book, i) for i in orig_spine_ids) new_filenames = set(get_filename(new_book, i) for i in new_spine_ids) missing_in_new = orig_filenames - new_filenames if missing_in_new: print(f"\n ⚠️ Missing from New Spine ({len(missing_in_new)}):") for f in list(missing_in_new)[:10]: print(f" - {f}") # 4. Manifest Comparison (All Resources) print("\n[4] Manifest (All Resources) Comparison") print("-" * 60) orig_manifest = {i.get_name() for i in orig_book.get_items()} new_manifest = {i.get_name() for i in new_book.get_items()} missing_resources = orig_manifest - new_manifest # Filter out NCX/Nav as they might be regenerated with different names missing_resources = {f for f in missing_resources if not f.endswith('.ncx') and 'nav' not in f.lower()} if missing_resources: print(f" ⚠️ Resources Missing in New Book ({len(missing_resources)}):") for f in sorted(list(missing_resources)): print(f" - {f}") else: print(" ✅ All resources preserved.") if __name__ == "__main__": orig_path = "input/To Explain the World The Discovery of Modern Science (H) (Steven Weinberg [Weinberg, Steven]) (Z-Library).epub" # Escaped path from user prompt: "input/To Explain the World The Discovery of Modern Science (H) (Steven Weinberg [Weinberg, Steven]) (Z-Library).epub" # We generated this in the previous batch test new_path = "test_output/To Explain the World The Discovery of Modern Science (H)_bilingual.epub" if len(sys.argv) > 2: orig_path = sys.argv[1] new_path = sys.argv[2] compare_epubs(orig_path, new_path)