Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- usage:
- $ python3 fix_shueisha.py [EPUB_FILE]...
- $ ls | grep *.epub | python3 fix_shueisha.py
- """
- import sys
- from zipfile import ZipFile
- from io import BytesIO, TextIOWrapper
- def fix_container(input_buffer):
- output_buffer = TextIOWrapper(BytesIO(), encoding="utf-8")
- for line in TextIOWrapper(input_buffer, encoding="utf-8"):
- if "advanced.opf" in line and not line.startswith("<!--"):
- line = f"<!--{line[:-1]}-->{line[-1]}"
- output_buffer.write(line)
- output_buffer.seek(0)
- return output_buffer
- def fix_opf(input_buffer):
- output_buffer = TextIOWrapper(BytesIO(), encoding="utf-8")
- for line in TextIOWrapper(input_buffer, encoding="utf-8"):
- if "cover.jpg" in line and 'properties="cover-image"' not in line:
- line = line.replace("href", 'properties="cover-image" href')
- output_buffer.write(line)
- output_buffer.seek(0)
- return output_buffer
- def fix(epub_name):
- filters = {
- "META-INF/container.xml": fix_container,
- "OPS/standard.opf": fix_opf,
- }
- archive = {}
- with ZipFile(epub_name) as zipfile:
- for info in zipfile.infolist():
- if info.filename in filters:
- with zipfile.open(info) as f:
- archive[info.filename] = filters[info.filename](f)
- else:
- with zipfile.open(info) as f:
- archive[info.filename] = TextIOWrapper(BytesIO(f.read()), encoding="utf-8")
- with ZipFile(epub_name, "w") as zipfile:
- for filename, io in archive.items():
- with zipfile.open(filename, "w") as f:
- f.write(io.buffer.read())
- def main():
- epub_list = sys.argv[1:] or sys.stdin
- for epub in epub_list:
- print(epub.strip(), end="...")
- fix(epub.strip())
- print("done")
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement