Skip to content

Commit

Permalink
画像の重複を排除, デフォルトの epub ファイル名を novel_id にする
Browse files Browse the repository at this point in the history
  • Loading branch information
ttk1 committed Jan 10, 2025
1 parent a05cb2e commit d4ae47e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,21 @@ options:
-h, --help show this help message and exit
-i, --illustration include illustrations
-o <file>, --output <file>
output file name
output file name; if not specified, ${novel_id}.epub is used
```
Example:
```sh
$ nepub xxxx
noval_id: xxxx, output: default.epub
noval_id: xxxx, illustration: False, output: xxxx.epub
3 episodes found.
Start downloading...
Downloading (1/3): https://ncode.syosetu.com/xxxx/1/
Downloading (2/3): https://ncode.syosetu.com/xxxx/2/
Downloading (3/3): https://ncode.syosetu.com/xxxx/3/
Download is complete!
Created default.epub.
Created xxxx.epub.
```
※ xxxx の部分には小説ページの URL の末尾部分 (`https://ncode.syosetu.com/{ここの文字列}/`) に置き換えてください。
Expand Down
24 changes: 19 additions & 5 deletions nepub/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@ def main():
"-o",
"--output",
metavar="<file>",
help="output file name",
help="output file name; if not specified, ${novel_id}.epub is used",
type=str,
default="default.epub",
)
args = parser.parse_args()
convert_narou_to_epub(args.novel_id, args.illustration, args.output)
if args.output:
output = args.output
else:
output = f"{args.novel_id}.epub"
convert_narou_to_epub(args.novel_id, args.illustration, output)


def convert_narou_to_epub(novel_id: str, illustration: bool, output: str):
Expand Down Expand Up @@ -73,13 +76,24 @@ def convert_narou_to_epub(novel_id: str, illustration: bool, output: str):

print("Download is complete!")

# deduplicate images
deduplicated_images = []
image_md5s = set()
for image in images:
if image["id"] not in image_md5s:
deduplicated_images.append(image)
image_md5s.add(image["id"])

# files
files: List[Tuple[str, str | bytes]] = []
files.append(("mimetype", "application/epub+zip"))
files.append(("META-INF/container.xml", container()))
files.append(("src/style.css", style()))
files.append(
("src/content.opf", content(title, author, created_at, episodes, images))
(
"src/content.opf",
content(title, author, created_at, episodes, deduplicated_images),
)
)
files.append(("src/navigation.xhtml", nav(chapters)))
for episode in episodes:
Expand All @@ -89,7 +103,7 @@ def convert_narou_to_epub(novel_id: str, illustration: bool, output: str):
text(episode["title"], episode["paragraphs"]),
)
)
for image in images:
for image in deduplicated_images:
files.append((f'src/image/{image["name"]}', image["data"]))
compose(output, files)

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

setup(
name="nepub",
version="1.1.0",
version="1.1.1",
description="Small tool to convert Narou Novels to vertically written EPUBs.",
long_description=readme,
author="[email protected]",
Expand Down

0 comments on commit d4ae47e

Please sign in to comment.