Skip to content

Commit

Permalink
Fixed logging errors in kindle_epub_fixer and convert_library as well…
Browse files Browse the repository at this point in the history
… as fixing integration between ingest_processor and convert_library with kindle_epub_fixer
  • Loading branch information
crocodilestick committed Jan 6, 2025
1 parent 47e8cf0 commit 2e7dafe
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 28 deletions.
5 changes: 4 additions & 1 deletion root/app/calibre-web/cps/templates/cwa_convert_library.html
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ <h3 style="color: whitesmoke;">CWA Library Convertor - Current Target Format - {
console.error("Error: ", e);
}

document.getElementById("innerStatus").innerHTML = get.status.replace(/\n/g, "<br>"); // * 10 + "&percnt;"
// Check if get.status is a non-empty string
if (get.status && get.status.trim() !== "") {
document.getElementById("innerStatus").innerHTML = get.status.replace(/\n/g, "<br>");
}

if (get.status.includes("CWA Convert Library Service - Run Ended:") || get.status.includes("CONVERT LIBRARY PROCESS TERMINATED BY USER")){
document.getElementById("innerStatus").innerHTML;
Expand Down
5 changes: 4 additions & 1 deletion root/app/calibre-web/cps/templates/cwa_epub_fixer.html
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ <h3 style="color: whitesmoke;">CWA Send-to-Kindle EPUB Fixer</h3><br>
console.error("Error: ", e);
}

document.getElementById("innerStatus").innerHTML = get.status.replace(/\n/g, "<br>"); // * 10 + "&percnt;"
// Check if get.status is a non-empty string
if (get.status && get.status.trim() !== "") {
document.getElementById("innerStatus").innerHTML = get.status.replace(/\n/g, "<br>");
}

if (get.status.includes("CWA Kindle EPUB Fixer Service - Run Ended:") || get.status.includes("CWA EPUB FIXER PROCESS TERMINATED BY USER AT")){
document.getElementById("innerStatus").innerHTML;
Expand Down
12 changes: 6 additions & 6 deletions scripts/convert_library.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

def print_and_log(string) -> None:
""" Ensures the provided string is passed to STDOUT and stored in the runs log file """
logging.info(string)
logger.info(string)
print(string)


Expand All @@ -41,7 +41,7 @@ def print_and_log(string) -> None:
lock.close()
except FileExistsError:
print_and_log("[convert-library]: CANCELLING... convert-library was initiated but is already running")
logging.info(f"\nCWA Convert Library Service - Run Cancelled: {datetime.now()}")
logger.info(f"\nCWA Convert Library Service - Run Cancelled: {datetime.now()}")
sys.exit(2)

# Defining function to delete the lock on script exit
Expand Down Expand Up @@ -183,7 +183,7 @@ def convert_library(self):

if self.target_format == "epub" and self.kindle_epub_fixer:
try:
EPUBFixer().process(target_filepath)
EPUBFixer().process(input_path=target_filepath)
print_and_log(f"[convert-library]: ({self.current_book}/{len(self.to_convert)}) Resulting EPUB file successfully processed by CWA-EPUB-Fixer!")
except Exception as e:
print_and_log(f"[convert-library]: ({self.current_book}/{len(self.to_convert)}) An error occurred while processing {os.path.basename(target_filepath)} with the kindle-epub-fixer. See the following error:\n{e}")
Expand Down Expand Up @@ -321,17 +321,17 @@ def main():
parser.add_argument('--verbose', '-v', action='store_true', required=False, dest='verbose', help='When passed, the output from the ebook-convert command will be included in what is shown to the user in the Web UI', default=False)
args = parser.parse_args()

logging.info(f"CWA Convert Library Service - Run Started: {datetime.now()}\n")
logger.info(f"CWA Convert Library Service - Run Started: {datetime.now()}\n")
converter = LibraryConverter(args)
if len(converter.to_convert) > 0:
converter.convert_library()
else:
print_and_log("[convert-library]: No books found in library without a copy in the target format. Exiting now...")
logging.info(f"\nCWA Convert Library Service - Run Ended: {datetime.now()}")
logger.info(f"\nCWA Convert Library Service - Run Ended: {datetime.now()}")
sys.exit(0)

print_and_log(f"\n[convert-library]: Library conversion complete! {len(converter.to_convert)} books converted! Exiting now...")
logging.info(f"\nCWA Convert Library Service - Run Ended: {datetime.now()}")
logger.info(f"\nCWA Convert Library Service - Run Ended: {datetime.now()}")
sys.exit(0)


Expand Down
2 changes: 1 addition & 1 deletion scripts/ingest_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ def add_book_to_library(self, book_path:str) -> None:

def run_kindle_epub_fixer(self, filepath:str, dest=None) -> None:
try:
EPUBFixer().process(filepath, dest)
EPUBFixer().process(input_path=filepath, output_path=dest)
except Exception as e:
print(f"[ingest-processor] An error occurred while processing {os.path.basename(filepath)} with the kindle-epub-fixer. See the following error:\n{e}")

Expand Down
54 changes: 35 additions & 19 deletions scripts/kindle_epub_fixer.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import os
import re
import zipfile
# import xml.etree.ElementTree as ET
from xml.dom import minidom
import argparse
from pathlib import Path
Expand Down Expand Up @@ -40,7 +39,7 @@
def print_and_log(string, log=True) -> None:
""" Ensures the provided string is passed to STDOUT AND stored in the run's log file """
if log:
logging.info(string)
logger.info(string)
print(string)

### LOCK FILES
Expand All @@ -52,7 +51,7 @@ def print_and_log(string, log=True) -> None:
lock.close()
except FileExistsError:
print_and_log("[cwa-kindle-epub-fixer] CANCELLING... kindle-epub-fixer was initiated but is already running")
logging.info(f"\nCWA Kindle EPUB Fixer Service - Run Ended: {datetime.now()}")
logger.info(f"\nCWA Kindle EPUB Fixer Service - Run Ended: {datetime.now()}")
sys.exit(2)

# Defining function to delete the lock on script exit
Expand Down Expand Up @@ -81,6 +80,7 @@ def __init__(self, manually_triggered:bool=False, current_position:str=None):


def backup_original_file(self, epub_path):
"""Backup original file"""
if self.cwa_settings['auto_backup_epub_fixes']:
try:
output_path = f"/config/processed_books/fixed_originals/"
Expand Down Expand Up @@ -239,49 +239,65 @@ def write_epub(self, output_path):
for filename, content in self.binary_files.items():
zip_ref.writestr(filename, content)

def export_summary(self):
def export_issue_summary(self, epub_path):
if self.current_position:
line_suffix = f"[cwa-kindle-epub-fixer] {self.current_position} - "
else:
line_suffix = "[cwa-kindle-epub-fixer] "

if self.fixed_problems:
print_and_log(line_suffix + f"{len(self.fixed_problems)} issues fixed with {self.epub_path}:", log=self.manually_triggered)
print_and_log(line_suffix + f"{len(self.fixed_problems)} issues fixed with {epub_path}:", log=self.manually_triggered)
for count, problem in enumerate(self.fixed_problems):
print_and_log(f" {count + 1} - {problem}", log=self.manually_triggered)
else:
print_and_log(line_suffix + f"No issues found! - {self.epub_path}", log=self.manually_triggered)
print_and_log(line_suffix + f"No issues found! - {epub_path}", log=self.manually_triggered)

def add_entry_to_db(self, input_path, output_path):
self.db.epub_fixer_add_entry(Path(input_path).basename,
self.db.epub_fixer_add_entry(Path(input_path).stem,
self.manually_triggered,
len(self.fixed_problems),
str(self.cwa_settings['auto_backup_epub_fixes']),
output_path,
self.fixed_problems)
"\n".join(self.fixed_problems))


def process(self, input_path, output_path, default_language='en'):
def process(self, input_path, output_path=None, default_language='en'):
"""Process a single EPUB file"""
if not output_path:
output_path = input_path
try:
# Back Up Original File
print_and_log("[cwa-kindle-epub-fixer] Backing up original file...", log=self.manually_triggered)
self.backup_original_file(input_path)

# Load EPUB
print_and_log("[cwa-kindle-epub-fixer] Loading provided EPUB...", log=self.manually_triggered)
self.read_epub(input_path)

# Run fixing procedures
print_and_log("[cwa-kindle-epub-fixer] Checking linking to body ID to prevent unresolved hyperlinks...", log=self.manually_triggered)
self.fix_body_id_link()
print_and_log("[cwa-kindle-epub-fixer] Checking language field tag is valid...", log=self.manually_triggered)
self.fix_book_language(default_language)
print_and_log("[cwa-kindle-epub-fixer] Checking for stray images...", log=self.manually_triggered)
self.fix_stray_img()
print_and_log("[cwa-kindle-epub-fixer] Checking UTF-8 encoding declaration...", log=self.manually_triggered)
self.fix_encoding()

# Notify user and/or write to log
self.export_issue_summary(input_path)

# Write EPUB
print_and_log("[cwa-kindle-epub-fixer] Writing EPUB...", log=self.manually_triggered)
if Path(output_path).is_dir():
output_path = output_path + os.path.basename(input_path)
self.write_epub(output_path)
print_and_log("[cwa-kindle-epub-fixer] EPUB successfully written.", log=self.manually_triggered)

# Notify user and/or write to log
self.export_summary()
# Add entry to cwa.db
print_and_log("[cwa-kindle-epub-fixer] Adding run to cwa.db...", log=self.manually_triggered)
self.add_entry_to_db(input_path, output_path)
print_and_log("[cwa-kindle-epub-fixer] Run successfully added to cwa.db.", log=self.manually_triggered)
return self.fixed_problems

except Exception as e:
Expand Down Expand Up @@ -310,35 +326,35 @@ def main():
prevent the file from being compatible with Amazon\'s Send-to-Kindle service. If the "-all" flag is \
passed, all epub files in the user\'s calibre library will be processed'
)
parser.add_argument('input_file', required=False, help='Input EPUB file path')
parser.add_argument('--input_file', '-i', required=False, help='Input EPUB file path')
parser.add_argument('--output', '-o', required=False, help='Output EPUB file path')
parser.add_argument('--language', '-l', required=False, default='en', help='Default language to use if not specified or invalid')
parser.add_argument('--suffix', '-s',required=False, default=False, action='store_true', help='Adds suffix "fixed" to output filename if given')
parser.add_argument('--all', '-a', required=False, default=False, action='store_true', help='Will attempt to fix any issues in every EPUB in th user\'s library')

args = parser.parse_args()
logging.info(f"CWA Kindle EPUB Fixer Service - Run Started: {datetime.now()}\n")
logger.info(f"CWA Kindle EPUB Fixer Service - Run Started: {datetime.now()}\n")

### CATCH INCOMPATIBLE COMBINATIONS OF ARGUMENTS
if not args.input_file and not args.all:
print("[cwa-kindle-epub-fixer] ERROR - No file provided")
logging.info(f"\nCWA Kindle EPUB Fixer Service - Run Ended: {datetime.now()}\n")
logger.info(f"\nCWA Kindle EPUB Fixer Service - Run Ended: {datetime.now()}\n")
sys.exit(4)
elif args.all and args.input_file:
print("[cwa-kindle-epub-fixer] ERROR - Can't give all and a filepath at the same time")
logging.info(f"\nCWA Kindle EPUB Fixer Service - Run Ended: {datetime.now()}\n")
logger.info(f"\nCWA Kindle EPUB Fixer Service - Run Ended: {datetime.now()}\n")
sys.exit(5)

### INPUT_FILE PROVIDED
elif args.input_file and not args.all:
# Validate input file
if not Path(args.input_file).exists():
print_and_log(f"[cwa-kindle-epub-fixer] ERROR - Given file {args.input_file} does not exist")
logging.info(f"\nCWA Kindle EPUB Fixer Service - Run Ended: {datetime.now()}\n")
logger.info(f"\nCWA Kindle EPUB Fixer Service - Run Ended: {datetime.now()}\n")
sys.exit(3)
if not args.input_file.lower().endswith('.epub'):
print_and_log("[cwa-kindle-epub-fixer] ERROR - The input file must be an EPUB file with a .epub extension.")
logging.info(f"\nCWA Kindle EPUB Fixer Service - Run Ended: {datetime.now()}\n")
logger.info(f"\nCWA Kindle EPUB Fixer Service - Run Ended: {datetime.now()}\n")
sys.exit(1)
# Determine output path
if args.output:
Expand All @@ -351,7 +367,7 @@ def main():
# Run EPUBFixer
print(f"[cwa-kindle-epub-fixer] Processing given file - {args.input_file}...")
EPUBFixer(manually_triggered=True).process(args.input_file, output_path, args.language)
logging.info(f"\nCWA Kindle EPUB Fixer Service - Run Ended: {datetime.now()}\n")
logger.info(f"\nCWA Kindle EPUB Fixer Service - Run Ended: {datetime.now()}\n")
sys.exit(0)

### ALL PASSED AS ARGUMENT
Expand Down Expand Up @@ -379,7 +395,7 @@ def main():
print_and_log(f"\n[cwa-kindle-epub-fixer] All {len(epubs_to_process)} EPUBs in Library successfully processed! Exiting now...")
else:
print_and_log("[cwa-kindle-epub-fixer] No EPUBs found to process. Exiting now...")
logging.info(f"\nCWA Kindle EPUB Fixer Service - Run Ended: {datetime.now()}\n")
logger.info(f"\nCWA Kindle EPUB Fixer Service - Run Ended: {datetime.now()}\n")
sys.exit(0)


Expand Down

0 comments on commit 2e7dafe

Please sign in to comment.