Skip to content

Commit 33ce03b

Browse files
authored
v2.5.11
1 parent 2c453b8 commit 33ce03b

File tree

1 file changed

+89
-78
lines changed

1 file changed

+89
-78
lines changed

komga_cover_extractor.py

Lines changed: 89 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
import settings as settings_file
4747

4848
# Version of the script
49-
script_version = (2, 5, 10)
49+
script_version = (2, 5, 11)
5050
script_version_text = "v{}.{}.{}".format(*script_version)
5151

5252
# Paths = existing library
@@ -1559,7 +1559,11 @@ def set_num_as_float_or_int(volume_number, silent=False):
15591559
if isinstance(volume_number, list):
15601560
result = "-".join(
15611561
[
1562-
str(int(num)) if float(num) == int(num) else str(float(num))
1562+
(
1563+
str(int(float(num)))
1564+
if float(num) == int(float(num))
1565+
else str(float(num))
1566+
)
15631567
for num in volume_number
15641568
]
15651569
)
@@ -1798,8 +1802,8 @@ def ends_with_bracket(s):
17981802
# check if volume file name is a chapter
17991803
@lru_cache(maxsize=None)
18001804
def contains_chapter_keywords(file_name):
1801-
# Remove "_extra" and replace underscores
1802-
file_name_clean = file_name.replace("_extra", "")
1805+
# Replace "_extra"
1806+
file_name_clean = file_name.replace("_extra", ".5")
18031807

18041808
# Replace underscores
18051809
file_name_clean = (
@@ -1926,8 +1930,8 @@ def remove_brackets(string):
19261930
# Checks if the passed string contains volume keywords
19271931
@lru_cache(maxsize=None)
19281932
def contains_volume_keywords(file):
1929-
# Remove _extra
1930-
file = file.replace("_extra", "")
1933+
# Replace _extra
1934+
file = file.replace("_extra", ".5")
19311935

19321936
# Remove dual spaces
19331937
file = remove_dual_space(file).strip()
@@ -2153,6 +2157,20 @@ def upgrade_to_file_class(
21532157
)[0]
21542158

21552159
# Create a list of tuples with arguments to pass to the File constructor
2160+
file_types = [
2161+
(
2162+
"chapter"
2163+
if not contains_volume_keywords(file) and contains_chapter_keywords(file)
2164+
else "volume"
2165+
)
2166+
for file in files
2167+
]
2168+
2169+
chapter_numbers = [
2170+
get_release_number_cache(file, chapter=file_type == "chapter")
2171+
for file, file_type in zip(files, file_types)
2172+
]
2173+
21562174
file_args = [
21572175
(
21582176
file,
@@ -2174,36 +2192,9 @@ def upgrade_to_file_class(
21742192
else None
21752193
),
21762194
)
2177-
for file, file_type, chapter_number in zip(
2178-
files,
2179-
[
2180-
(
2181-
"chapter"
2182-
if not contains_volume_keywords(file)
2183-
and contains_chapter_keywords(file)
2184-
else "volume"
2185-
)
2186-
for file in files
2187-
],
2188-
[
2189-
get_release_number_cache(file, chapter=file_type == "chapter")
2190-
for file, file_type in zip(
2191-
files,
2192-
[
2193-
(
2194-
"chapter"
2195-
if not contains_volume_keywords(file)
2196-
and contains_chapter_keywords(file)
2197-
else "volume"
2198-
)
2199-
for file in files
2200-
],
2201-
)
2202-
],
2203-
)
2195+
for file, file_type, chapter_number in zip(files, file_types, chapter_numbers)
22042196
]
22052197

2206-
# Process the files sequentially
22072198
results = [File(*args) for args in file_args]
22082199

22092200
return results
@@ -2489,8 +2480,8 @@ def get_series_name_from_volume(name, root, test_mode=False, second=False):
24892480
# remove the brackets only
24902481
name = re.sub(r"^(\[[^\]]*\]|\([^\)]*\)|\{[^}]*\})+\s+", "", name).strip()
24912482

2492-
# remove _extra
2493-
name = remove_dual_space(name.replace("_extra", "")).strip()
2483+
# replace _extra
2484+
name = remove_dual_space(name.replace("_extra", ".5")).strip()
24942485

24952486
# replace underscores
24962487
name = replace_underscores(name) if "_" in name else name
@@ -2658,7 +2649,7 @@ def get_series_name_from_chapter(name, root, chapter_number="", second=False):
26582649
name = re.sub(r"^(\[[^\]]*\]|\([^\)]*\)|\{[^}]*\})+\s+", "", name).strip()
26592650

26602651
# Replace _extra
2661-
name = name.replace("_extra", "")
2652+
name = name.replace("_extra", ".5")
26622653

26632654
# Remove dual space
26642655
name = remove_dual_space(name).strip()
@@ -2721,7 +2712,7 @@ def get_series_name_from_chapter(name, root, chapter_number="", second=False):
27212712

27222713

27232714
# Calculates the percentage of files in a folder that match a specified extension or file type.
2724-
def get_percent_for_folder(files, extensions=None, file_type=None):
2715+
def get_folder_type(files, extensions=None, file_type=None):
27252716
if not files:
27262717
return 0
27272718

@@ -2892,7 +2883,10 @@ def clean_series_name(name):
28922883
keywords = volume_regex_keywords if not chapter else chapter_regex_keywords
28932884
result = None
28942885

2895-
file = remove_dual_space(file.replace("_extra", "")).strip()
2886+
# Replace _extra
2887+
file = remove_dual_space(file.replace("_extra", ".5")).strip()
2888+
2889+
# Replace underscores
28962890
file = replace_underscores(file) if "_" in file else file
28972891

28982892
is_multi_volume = (
@@ -3671,9 +3665,7 @@ def get_file_size_info(path):
36713665
)
36723666
continue
36733667

3674-
chap_dl_percent = get_percent_for_folder(
3675-
downloaded_releases, file_type="chapter"
3676-
)
3668+
chap_dl_percent = get_folder_type(downloaded_releases, file_type="chapter")
36773669
is_chapter_dir = chap_dl_percent >= required_matching_percentage
36783670

36793671
highest_index_num = (
@@ -4959,24 +4951,24 @@ def get_percent_and_print(existing_files, file, file_type=None):
49594951
percent_existing = 0
49604952

49614953
if file_type in ["manga", "novel"]:
4962-
percent_dl = get_percent_for_folder(
4954+
percent_dl = get_folder_type(
49634955
[file.name],
49644956
extensions=(
49654957
manga_extensions if file_type == "manga" else novel_extensions
49664958
),
49674959
)
4968-
percent_existing = get_percent_for_folder(
4960+
percent_existing = get_folder_type(
49694961
[f.name for f in existing_files],
49704962
extensions=(
49714963
manga_extensions if file_type == "manga" else novel_extensions
49724964
),
49734965
)
49744966
elif file_type in ["chapter", "volume"]:
4975-
percent_dl = get_percent_for_folder(
4967+
percent_dl = get_folder_type(
49764968
[file],
49774969
file_type=file_type,
49784970
)
4979-
percent_existing = get_percent_for_folder(
4971+
percent_existing = get_folder_type(
49804972
existing_files,
49814973
file_type=file_type,
49824974
)
@@ -6715,7 +6707,7 @@ def get_matching_volumes(file, img_volumes):
67156707
# !OLD METHOD!: Only used for cleaning a folder name as a backup if no volumes were found inside the folder
67166708
# when renaming folders in the dowload directory.
67176709
def get_series_name(dir):
6718-
dir = remove_dual_space(dir.replace("_extra", "")).strip()
6710+
dir = remove_dual_space(dir.replace("_extra", ".5")).strip()
67196711
dir = (
67206712
re.sub(
67216713
r"(\b|\s)((\s|)-(\s|)|)(Part|)(%s)([-_. ]|)([-_. ]|)([0-9]+)(\b|\s).*"
@@ -7303,6 +7295,9 @@ def rename_files(
73037295

73047296
for root, dirs, files in scandir.walk(path):
73057297
if test_mode:
7298+
if root not in download_folders:
7299+
return
7300+
73067301
dirs = []
73077302
files = only_these_files
73087303

@@ -7336,6 +7331,10 @@ def rename_files(
73367331

73377332
print(f"\t{root}")
73387333
for file in volumes:
7334+
if "_extra" in file.name and ".5" in str(file.volume_number):
7335+
# remove .5 from the volume number and index_number
7336+
file.volume_number = int(file.volume_number)
7337+
file.index_number = int(file.index_number)
73397338
if test_mode:
73407339
print(f"\t\t[{volumes.index(file) + 1}/{len(volumes)}] {file.name}")
73417340

@@ -9554,16 +9553,14 @@ def search_bookwalker(
95549553
a_tag_chapter = tag_dict["a-tag-chapter"]
95559554
a_tag_simulpub = tag_dict["a-tag-simulpub"]
95569555

9557-
if a_tag_manga:
9558-
book_type = a_tag_manga.get_text()
9559-
elif a_tag_light_novel:
9560-
book_type = a_tag_light_novel.get_text()
9561-
elif a_tag_other:
9562-
book_type = a_tag_other.get_text()
9556+
book_type = a_tag_manga or a_tag_light_novel or a_tag_other
9557+
9558+
if book_type:
9559+
book_type = book_type.get_text()
9560+
book_type = re.sub(r"\n|\t|\r", "", book_type).strip()
95639561
else:
95649562
book_type = "Unknown"
95659563

9566-
book_type = re.sub(r"\n|\t|\r", "", book_type).strip()
95679564
title = o_tile_book_info.find("h2", class_="a-tile-ttl").text.strip()
95689565
original_title = title
95699566

@@ -9572,23 +9569,27 @@ def search_bookwalker(
95729569
if title:
95739570
print(f"\t\t\t\t\t[{item_index + 1}] {title}")
95749571

9575-
# remove brackets
9576-
title = remove_brackets(title) if contains_brackets(title) else title
9572+
# remove brackets
9573+
title = (
9574+
remove_brackets(title) if contains_brackets(title) else title
9575+
)
9576+
9577+
# unidecode the title
9578+
title = unidecode(title)
95779579

9578-
# unidecode the title
9579-
title = unidecode(title)
9580+
# replace any remaining unicode characters in the title with spaces
9581+
title = re.sub(r"[^\x00-\x7F]+", " ", title)
95809582

9581-
# replace any remaining unicode characters in the title with spaces
9582-
title = re.sub(r"[^\x00-\x7F]+", " ", title)
9583-
title = remove_dual_space(title).strip()
9583+
# remove any extra spaces
9584+
title = remove_dual_space(title).strip()
95849585

95859586
if a_tag_chapter or a_tag_simulpub:
95869587
chapter_releases.append(title)
95879588
continue
95889589

95899590
if (
95909591
title
9591-
and "chapter" in title.lower()
9592+
and ("chapter" in title.lower() or re.search(r"\s#\d+\b", title))
95929593
and not re.search(r"re([-_. :]+)?zero", title, re.IGNORECASE)
95939594
):
95949595
continue
@@ -9988,9 +9989,9 @@ def process_items(items, file_name, color, webhook_index):
99889989

99899990
# Gets the volume type based on the extensions in the folder
99909991
def determine_volume_type(volumes):
9991-
if get_percent_for_folder([f.name for f in volumes], manga_extensions) >= 70:
9992+
if get_folder_type([f.name for f in volumes], manga_extensions) >= 70:
99929993
return "m"
9993-
elif get_percent_for_folder([f.name for f in volumes], novel_extensions) >= 70:
9994+
elif get_folder_type([f.name for f in volumes], novel_extensions) >= 70:
99949995
return "l"
99959996
return None
99969997

@@ -10657,31 +10658,41 @@ def has_multiple_numbers(file_name):
1065710658

1065810659
# Extracts all the numbers from a string
1065910660
def extract_all_numbers(string, subtitle=None):
10661+
# Replace underscores
10662+
string = replace_underscores(string) if "_" in string else string
10663+
10664+
# Remove the subtitle if present
1066010665
if subtitle:
1066110666
string = re.sub(
1066210667
rf"(-|:)\s*{re.escape(subtitle)}$", "", string, re.IGNORECASE
1066310668
).strip()
1066410669

1066510670
numbers = re.findall(
10666-
r"\b(%s)(([0-9]+)(([-_.])([0-9]+)|)+(x[0-9]+)?(#([0-9]+)(([-_.])([0-9]+)|)+)?)"
10671+
r"\b(?:%s)(\d+(?:[-_.]\d+|)+(?:x\d+)?(?:#\d+(?:[-_.]\d+|)+)?)"
1066710672
% exclusion_keywords_regex,
1066810673
string,
1066910674
)
10670-
1067110675
new_numbers = []
10672-
if numbers:
10673-
for number in numbers:
10674-
if isinstance(number, tuple):
10675-
for item in tuple(filter(None, set(number))):
10676-
if item and not re.search(
10677-
r"(x[0-9]+)|(#([0-9]+)(([-_.])([0-9]+)|)+)|^(-|\.)$", item
10678-
):
10679-
new_numbers.append(set_num_as_float_or_int(item))
10676+
10677+
for number in numbers:
10678+
items = number if isinstance(number, tuple) else [number]
10679+
10680+
for item in items:
10681+
if not item:
10682+
continue
10683+
10684+
if "#" in item and re.search(r"(\d+#\d+)", item):
10685+
item = re.sub(r"((#)([0-9]+)(([-_.])([0-9]+)|)+)", "", item).strip()
10686+
if "x" in item:
10687+
item = re.sub(r"(x[0-9]+)", "", item, re.IGNORECASE).strip()
10688+
10689+
if "-" not in item:
10690+
new_numbers.append(set_num_as_float_or_int(item))
1068010691
else:
10681-
if number and not re.search(
10682-
r"(x[0-9]+)|(#([0-9]+)(([-_.])([0-9]+)|)+)|^(-|\.)$", number
10683-
):
10684-
new_numbers.append(set_num_as_float_or_int(number))
10692+
num_range = item.split("-")
10693+
new_range = [set_num_as_float_or_int(num) for num in num_range]
10694+
new_numbers.append(new_range)
10695+
1068510696
return new_numbers
1068610697

1068710698

0 commit comments

Comments
 (0)