Created
September 10, 2024 03:10
-
-
Save Benau/a39f61524f44688fa0884e9a20495158 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import opencc | |
import os | |
import sys | |
import glob | |
import zipfile | |
converter = opencc.OpenCC('s2hk.json') | |
def expand_wildcards(pattern): | |
# Use glob to find all matching files recursively | |
return glob.glob(pattern, recursive=True) | |
def sc2tc(book_path): | |
if not book_path.endswith('.epub'): | |
return | |
print(f"Reading file: {book_path}") | |
temp_book_path = book_path.replace('.epub', '_temp.epub') | |
with zipfile.ZipFile(book_path, 'r') as zip_ref: | |
with zipfile.ZipFile(temp_book_path, 'w') as temp_book: | |
for file_info in zip_ref.infolist(): | |
# Read the content of the file | |
with zip_ref.open(file_info) as file: | |
if file_info.filename.endswith('.html') or file_info.filename.endswith('.ncx') or file_info.filename.endswith('.opf'): | |
content = file.read() | |
converted_content = converter.convert(content) | |
converted_content = converted_content.replace('zh-CN', 'zh-TW') | |
temp_book.writestr(file_info.filename, converted_content) | |
else: | |
temp_book.writestr(file_info, zip_ref.read(file_info.filename)) | |
os.remove(book_path) | |
os.rename(temp_book_path, book_path) | |
head, tail = os.path.split(book_path) | |
tc_path = converter.convert(tail) | |
if head: | |
tc_path = head + '/' + tc_path | |
os.rename(book_path, tc_path) | |
print(f"Writing file: {tc_path}") | |
if len(sys.argv) < 2: | |
print("Usage: sc2tcbook.py <filename(s)>") | |
sys.exit(1) | |
# Collect all matching files | |
all_matching_files = set() # Use a set to avoid duplicates | |
for pattern in sys.argv[1:]: | |
matching_files = expand_wildcards(pattern) | |
all_matching_files.update(matching_files) | |
if not all_matching_files: | |
print("No files found matching the provided patterns.") | |
sys.exit(1) | |
# Process each matching file | |
for file_path in all_matching_files: | |
if os.path.isfile(file_path): | |
sc2tc(file_path) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment