@@ -16,15 +16,15 @@ def main() -> None:
16
16
parser .add_argument ("-c" , "--config_file_path" , help = "The path to the ficimage.json file." )
17
17
parser .add_argument ("-d" , "--debug" , help = "Enable debug mode." , action = "store_true" )
18
18
args = parser .parse_args ()
19
-
19
+
20
20
path_to_epub = args .path_to_epub
21
21
config_file_path = args .config_file_path
22
22
debug = args .debug
23
-
23
+
24
24
try :
25
25
book = epub .read_epub (path_to_epub )
26
26
print (f'Opened { path_to_epub } ' )
27
-
27
+
28
28
(config_file_exists , config_file_location ) = config_check (config_file_path )
29
29
if config_file_exists :
30
30
ficimage_config = load_config_json (config_file_location )
@@ -35,15 +35,18 @@ def main() -> None:
35
35
if str (default_image_format_config ).lower () not in ("jpg" , "jpeg" , "png" ):
36
36
default_image_format_config = "JPEG"
37
37
max_image_size_config : int = ficimage_config .get ("max_image_size" , 1_000_000 )
38
-
38
+
39
39
file_name = path_to_epub .split ('/' )[- 1 ].split ('.' )[0 ]
40
-
40
+ images_downloaded = {}
41
+
41
42
for item in book .get_items_of_type (ebooklib .ITEM_DOCUMENT ):
42
43
try :
43
44
soup = BeautifulSoup (item .content , "lxml-xml" )
44
45
p_tags = soup .find_all ('p' )
45
46
images = [i for i in p_tags if '[img:' in i .text ]
46
47
print (f'Found { len (images )} images in { item .file_name } ' )
48
+ item_file_name = item .file_name .split ('.' )[0 ]
49
+ images_downloaded [item .file_name ] = [0 , len (images )]
47
50
# Clean up the images link
48
51
# Right now they look like this: <p>[img: <a
49
52
# href="https://i.imgur.com/ABCDEF.jpg" rel="noopener noreferrer">data:image/gif;base64,R0lGODlhA</a>]</p>
@@ -53,11 +56,9 @@ def main() -> None:
53
56
if image is None :
54
57
print ("NoneType, Skipping" )
55
58
else :
56
- item_file_name = item .file_name .split ('.' )[0 ]
57
59
image_link = image .a ['href' ]
58
60
print (f"[{ item_file_name } ] Image { images .index (image ) + 1 } "
59
61
f"(out of { len (images )} ). Source: { image_link } " )
60
-
61
62
try :
62
63
(
63
64
image_content ,
@@ -70,12 +71,13 @@ def main() -> None:
70
71
max_image_size = max_image_size_config ,
71
72
debug = debug
72
73
)
74
+ images_downloaded [item .file_name ][0 ] += 1
73
75
image_path = f"images/" \
74
76
f"{ item_file_name } _image_{ images .index (image )} .{ image_extension .lower ()} "
75
77
new_image = f"<img alt='Image { images .index (image )} from { item .file_name } ' " \
76
78
f"style='text-align: center; margin: 2em auto; display: block;'" \
77
79
f" src='{ image_path } ' />"
78
-
80
+
79
81
img = epub .EpubItem (
80
82
uid = f"{ item_file_name } _{ images .index (image )} " ,
81
83
file_name = image_path ,
@@ -91,10 +93,25 @@ def main() -> None:
91
93
print (f'Error while parsing images: { e } ' )
92
94
except TypeError :
93
95
print ("NoneType error, skipping ..." )
94
-
96
+
95
97
try :
96
98
epub .write_epub (f"[FicImage]{ file_name } .epub" , book )
99
+ total_number_of_images_downloaded = 0
100
+ number_of_all_images_found = 0
101
+ for _ , chapter_image_list in images_downloaded .items ():
102
+ total_number_of_images_downloaded += chapter_image_list [0 ]
103
+ number_of_all_images_found += chapter_image_list [1 ]
104
+
97
105
print (f'Wrote [FicImage]{ file_name } .epub' )
106
+ print (f"Image overview of { file_name } " )
107
+ print ("=" * 54 )
108
+ for k , v in images_downloaded .items ():
109
+ print (f"{ k } { ' ' * (18 - len (k ))} \t { v [0 ]} out of { v [1 ]} images downloaded" )
110
+ print ("=" * 54 )
111
+ print (f"Total images downloaded: { total_number_of_images_downloaded } "
112
+ f" out of { number_of_all_images_found } " )
113
+ print ("=" * 54 )
114
+
98
115
except Exception as e :
99
116
print (f'Error while writing epub: { e } ' )
100
117
except FileNotFoundError :
0 commit comments