Skip to content

Commit

Permalink
Fixed configuration errors with the logs for the epub_fixer and conve…
Browse files Browse the repository at this point in the history
…rt_library services as well as improving the formatting of the epub_fixer log to make it easier to read as well as fixing a typo in cwa_functions
  • Loading branch information
crocodilestick committed Jan 3, 2025
1 parent 20e485e commit df199e9
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 57 deletions.
21 changes: 1 addition & 20 deletions root/app/calibre-web/cps/cwa_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,25 +373,6 @@ def epub_fixer_start(queue):
ef_process = subprocess.Popen(['python3', '/app/calibre-web-automated/scripts/kindle_epub_fixer.py', '--all'])
queue.put(ef_process)

# def get_tmp_conversion_dir() -> str:
# dirs_json_path = "/app/calibre-web-automated/dirs.json"
# dirs = {}
# with open(dirs_json_path, 'r') as f:
# dirs: dict[str, str] = json.load(f)
# tmp_conversion_dir = f"{dirs['tmp_conversion_dir']}/"

# return tmp_conversion_dir

# def empty_tmp_con_dir(tmp_conversion_dir) -> None:
# try:
# files = os.listdir(tmp_conversion_dir)
# for file in files:
# file_path = os.path.join(tmp_conversion_dir, file)
# if os.path.isfile(file_path):
# os.remove(file_path)
# except Exception as e:
# print(f"[cwa-functions]: An error occurred while emptying {tmp_conversion_dir}. See the following error: {e}")

def is_epub_fixer_finished() -> bool:
with open("/config/epub-fixer.log", 'r') as log:
if "CWA Kindle EPUB Fixer Service - Run Ended: " in log.read():
Expand Down Expand Up @@ -419,7 +400,7 @@ def kill_epub_fixer(queue):
os.remove(trigger_file)
except FileNotFoundError:
...
with open("/config/convert-library.log", 'a') as f:
with open("/config/epub-fixer.log", 'a') as f:
f.write(f"\nCWA EPUB FIXER PROCESS TERMINATED BY USER AT {datetime.now()}")
break
elif is_epub_fixer_finished():
Expand Down
14 changes: 10 additions & 4 deletions scripts/convert_library.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,20 @@
from kindle_epub_fixer import EPUBFixer


# Define the logger
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO) # Set the logging level
# Create a FileHandler
file_handler = logging.FileHandler('/config/convert-library.log', mode='w')
# Create a Formatter and set it for the handler
LOG_FORMAT = '%(message)s'
logging.basicConfig(filename='/config/convert-library.log',
level=logging.INFO,
filemode='w',
format=LOG_FORMAT)
formatter = logging.Formatter(LOG_FORMAT)
file_handler.setFormatter(formatter)
# Add the handler to the logger
logger.addHandler(file_handler)

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

Expand Down
82 changes: 49 additions & 33 deletions scripts/kindle_epub_fixer.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,25 @@
change_logs_dir = "/app/calibre-web-automated/metadata_change_logs"
metadata_temp_dir = "/app/calibre-web-automated/metadata_temp"


# Define the logger
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO) # Set the logging level
# Create a FileHandler
file_handler = logging.FileHandler('/config/epub-fixer.log', mode='w')
# Create a Formatter and set it for the handler
LOG_FORMAT = '%(message)s'
logging.basicConfig(filename='/config/epub-fixer.log',
level=logging.INFO,
filemode='w',
format=LOG_FORMAT)
formatter = logging.Formatter(LOG_FORMAT)
file_handler.setFormatter(formatter)
# Add the handler to the logger
logger.addHandler(file_handler)

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


# Creates a lock file unless one already exists meaning an instance of the script is
# already running, then the script is closed, the user is notified and the program
# exits with code 2
Expand Down Expand Up @@ -77,10 +85,10 @@ def fix_encoding(self):
if not xml_declaration_pattern.match(content):
self.files[filename] = f"{encoding_declaration}\n{content}"
self.fixed_problems.append(f"Fixed encoding for file {filename}")
if self.as_script:
print_and_log(f" - Fixed encoding for file {filename}")
else:
print(f" - Fixed encoding for file {filename}")
# if self.as_script:
# print_and_log(f" - Fixed encoding for file {filename}")
# else:
# print(f" - Fixed encoding for file {filename}")

def fix_language(self):
allowed_languages = {# ISO 639-1
Expand Down Expand Up @@ -109,10 +117,10 @@ def fix_language(self):

self.files[opf_file] = ET.tostring(root, encoding='unicode')
self.fixed_problems.append(f"Updated language from {current_lang} to {new_lang}")
if self.as_script:
print_and_log(f" - Updated language from {current_lang} to {new_lang}")
else:
print(f" - Updated language from {current_lang} to {new_lang}")
# if self.as_script:
# print_and_log(f" - Updated language from {current_lang} to {new_lang}")
# else:
# print(f" - Updated language from {current_lang} to {new_lang}")

def fix_stray_images(self):
img_tag_pattern = re.compile(r'<img([^>]*)>', re.IGNORECASE)
Expand All @@ -130,10 +138,10 @@ def fix_stray_images(self):
if content != original_content:
self.files[filename] = content
self.fixed_problems.append(f"Removed stray images in {filename}")
if self.as_script:
print_and_log(f" - Removed stray images in {filename}")
else:
print(f" - Removed stray images in {filename}")
# if self.as_script:
# print_and_log(f" - Removed stray images in {filename}")
# else:
# print(f" - Removed stray images in {filename}")

def write_epub(self):
with zipfile.ZipFile(self.epub_path, 'w') as zip_out:
Expand All @@ -149,13 +157,21 @@ def process(self):
self.fix_language()
self.fix_stray_images()
self.write_epub()
print("[cwa-kindle-epub-fixer] Processing completed.")
# print("[cwa-kindle-epub-fixer] Processing completed.")
if self.fixed_problems:
print(f"[cwa-kindle-epub-fixer] {len(self.fixed_problems)} issues fixed with {self.epub_path}:")
for count, problem in enumerate(self.fixed_problems):
print(f" {count} - {problem}")
if self.as_script:
print_and_log(f"[cwa-kindle-epub-fixer] {len(self.fixed_problems)} issues fixed with {self.epub_path}:")
for count, problem in enumerate(self.fixed_problems):
print_and_log(f" {count + 1} - {problem}")
else:
print(f"[cwa-kindle-epub-fixer] {len(self.fixed_problems)} issues fixed with {self.epub_path}:")
for count, problem in enumerate(self.fixed_problems):
print(f" {count + 1} - {problem}")
else:
print(f"[cwa-kindle-epub-fixer] No issues found! - {self.epub_path}")
if self.as_script:
print_and_log(f"[cwa-kindle-epub-fixer] No issues found! - {self.epub_path}")
else:
print(f"[cwa-kindle-epub-fixer] No issues found! - {self.epub_path}")


def get_library_location() -> str:
Expand Down Expand Up @@ -186,27 +202,27 @@ def get_all_epubs_in_library() -> list[str]:

logging.info(f"CWA Kindle EPUB Fixer Service - Run Started: {datetime.now()}\n")
if not args.file and not args.all:
print("[cwa-kindle-epub-fixer] ERROR - Nothing given")
logging.info(f"CWA Kindle EPUB Fixer Service - Run Ended: {datetime.now()}\n")
print("[cwa-kindle-epub-fixer] ERROR - No file provided")
logging.info(f"\nCWA Kindle EPUB Fixer Service - Run Ended: {datetime.now()}\n")
sys.exit(4)
elif args.all and args.file:
print("[cwa-kindle-epub-fixer] ERROR - Can't give all and file at the same time")
logging.info(f"CWA Kindle EPUB Fixer Service - Run Ended: {datetime.now()}\n")
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")
sys.exit(5)
elif args.file and not args.all:
if not args.file.lower().endswith('.epub'):
print("[cwa-kindle-epub-fixer] ERROR - The input file must be an EPUB file with a .epub extension.")
logging.info(f"CWA Kindle EPUB Fixer Service - Run Ended: {datetime.now()}\n")
logging.info(f"\nCWA Kindle EPUB Fixer Service - Run Ended: {datetime.now()}\n")
sys.exit(1)
else:
if Path(args.file).exists():
print(f"[cwa-kindle-epub-fixer] Processing given file - {args.file}...")
EPUBFixer(args.file, as_script=True).process()
logging.info(f"CWA Kindle EPUB Fixer Service - Run Ended: {datetime.now()}\n")
logging.info(f"\nCWA Kindle EPUB Fixer Service - Run Ended: {datetime.now()}\n")
sys.exit(0)
else:
print(f"[cwa-kindle-epub-fixer] ERROR - Given file {args.file} does not exist")
logging.info(f"CWA Kindle EPUB Fixer Service - Run Ended: {datetime.now()}\n")
logging.info(f"\nCWA Kindle EPUB Fixer Service - Run Ended: {datetime.now()}\n")
sys.exit(3)
elif args.all and not args.file:
print("[cwa-kindle-epub-fixer] Processing all epubs in library...")
Expand All @@ -215,14 +231,14 @@ def get_all_epubs_in_library() -> list[str]:
print_and_log(f"[cwa-kindle-epub-fixer] {len(epubs_to_process)} EPUBs found to process.")
for count, epub in enumerate(epubs_to_process):
try:
print_and_log(f"[cwa-kindle-epub-fixer] {count}/{len(epubs_to_process)} - Processing {epub}...")
print_and_log(f"\n[cwa-kindle-epub-fixer] {count + 1}/{len(epubs_to_process)} - Processing {epub}...")
EPUBFixer(epub, as_script=True).process()
except Exception as e:
print_and_log(f"[cwa-kindle-epub-fixer] {count}/{len(epubs_to_process)} - The following error occurred when processing {epub}\n{e}")
print_and_log(f"All {len(epubs_to_process)} EPUBs in Library successfully processed! Exiting now...")
logging.info(f"CWA Kindle EPUB Fixer Service - Run Ended: {datetime.now()}\n")
print_and_log(f"[cwa-kindle-epub-fixer] {count + 1}/{len(epubs_to_process)} - The following error occurred when processing {epub}\n{e}")
print_and_log(f"\nAll {len(epubs_to_process)} EPUBs in Library successfully processed! Exiting now...")
logging.info(f"\nCWA Kindle EPUB Fixer Service - Run Ended: {datetime.now()}\n")
sys.exit(0)
else:
print_and_log("[cwa-kindle-epub-fixer] No EPUBs found to process. Exiting now...")
logging.info(f"CWA Kindle EPUB Fixer Service - Run Ended: {datetime.now()}\n")
logging.info(f"\nCWA Kindle EPUB Fixer Service - Run Ended: {datetime.now()}\n")
sys.exit(0)

0 comments on commit df199e9

Please sign in to comment.