Skip to content

Commit

Permalink
feat!: Released v1.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
Jemeni11 committed May 11, 2023
1 parent 16dfde2 commit ed36fcf
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 11 deletions.
19 changes: 19 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.0.1] - 2023-05-11

### Added
- Improved logging by adding an overview of downloaded images.
- Added a Changelog

## [1.0.0] - 2023-05-08
- Released FicImageScript


[1.0.1]: https://github.com/Jemeni11/FicImage/compare/v1.0.0...v1.0.1
[1.0.0]: https://github.com/Jemeni11/FicImage/releases/tag/v1.0.0
35 changes: 26 additions & 9 deletions FicImage/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ def main() -> None:
parser.add_argument("-c", "--config_file_path", help="The path to the ficimage.json file.")
parser.add_argument("-d", "--debug", help="Enable debug mode.", action="store_true")
args = parser.parse_args()

path_to_epub = args.path_to_epub
config_file_path = args.config_file_path
debug = args.debug

try:
book = epub.read_epub(path_to_epub)
print(f'Opened {path_to_epub}')

(config_file_exists, config_file_location) = config_check(config_file_path)
if config_file_exists:
ficimage_config = load_config_json(config_file_location)
Expand All @@ -35,15 +35,18 @@ def main() -> None:
if str(default_image_format_config).lower() not in ("jpg", "jpeg", "png"):
default_image_format_config = "JPEG"
max_image_size_config: int = ficimage_config.get("max_image_size", 1_000_000)

file_name = path_to_epub.split('/')[-1].split('.')[0]

images_downloaded = {}

for item in book.get_items_of_type(ebooklib.ITEM_DOCUMENT):
try:
soup = BeautifulSoup(item.content, "lxml-xml")
p_tags = soup.find_all('p')
images = [i for i in p_tags if '[img:' in i.text]
print(f'Found {len(images)} images in {item.file_name}')
item_file_name = item.file_name.split('.')[0]
images_downloaded[item.file_name] = [0, len(images)]
# Clean up the images link
# Right now they look like this: <p>[img: <a
# href="https://i.imgur.com/ABCDEF.jpg" rel="noopener noreferrer">data:image/gif;base64,R0lGODlhA</a>]</p>
Expand All @@ -53,11 +56,9 @@ def main() -> None:
if image is None:
print("NoneType, Skipping")
else:
item_file_name = item.file_name.split('.')[0]
image_link = image.a['href']
print(f"[{item_file_name}] Image {images.index(image) + 1} "
f"(out of {len(images)}). Source: {image_link}")

try:
(
image_content,
Expand All @@ -70,12 +71,13 @@ def main() -> None:
max_image_size=max_image_size_config,
debug=debug
)
images_downloaded[item.file_name][0] += 1
image_path = f"images/" \
f"{item_file_name}_image_{images.index(image)}.{image_extension.lower()}"
new_image = f"<img alt='Image {images.index(image)} from {item.file_name}' " \
f"style='text-align: center; margin: 2em auto; display: block;'" \
f" src='{image_path}' />"

img = epub.EpubItem(
uid=f"{item_file_name}_{images.index(image)}",
file_name=image_path,
Expand All @@ -91,10 +93,25 @@ def main() -> None:
print(f'Error while parsing images: {e}')
except TypeError:
print("NoneType error, skipping ...")

try:
epub.write_epub(f"[FicImage]{file_name}.epub", book)
total_number_of_images_downloaded = 0
number_of_all_images_found = 0
for _, chapter_image_list in images_downloaded.items():
total_number_of_images_downloaded += chapter_image_list[0]
number_of_all_images_found += chapter_image_list[1]

print(f'Wrote [FicImage]{file_name}.epub')
print(f"Image overview of {file_name}")
print("=" * 54)
for k, v in images_downloaded.items():
print(f"{k}{' ' * (18 - len(k))}\t{v[0]} out of {v[1]} images downloaded")
print("=" * 54)
print(f"Total images downloaded: {total_number_of_images_downloaded}"
f" out of {number_of_all_images_found}")
print("=" * 54)

except Exception as e:
print(f'Error while writing epub: {e}')
except FileNotFoundError:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "FicImageScript"
version = "1.0.0"
version = "1.0.1"
authors = [
{ name="Emmanuel C. Jemeni", email="[email protected]" }
]
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="FicImageScript",
version="1.0.0",
version="1.0.1",
author="Emmanuel C. Jemeni",
author_email="[email protected]",
description="FicImage is an application designed to enhance the reading experience of FicHub epubs.",
Expand Down

0 comments on commit ed36fcf

Please sign in to comment.