Added input control and better char replacement

This commit is contained in:
Willi Hohenstein 2022-02-14 17:38:45 +01:00
parent 0b02287378
commit 03ff8c248d

View file

@ -130,8 +130,8 @@ def list_search(query, viewer, *_):
def isbn_check(query): def isbn_check(query):
"""isbn10 or isbn13 check, if so remove separators""" """isbn10 or isbn13 check, if so remove separators"""
if query: if query:
su_num = query.replace("-", "").replace(" ", "") su_num = re.sub(r"(?<=\d)\D(?=\d|[xX])", "", query)
if len(su_num) == 13: if len(su_num) == 13 and su_num.isdecimal():
# Multiply every other digit by 3 # Multiply every other digit by 3
# Add these numbers and the other digits # Add these numbers and the other digits
product = sum(int(ch) for ch in su_num[::2]) + sum( product = sum(int(ch) for ch in su_num[::2]) + sum(
@ -139,9 +139,10 @@ def isbn_check(query):
) )
if product % 10 == 0: if product % 10 == 0:
return su_num return su_num
elif len(su_num) == 10: elif (
if su_num[0:8].isdigit() and ( len(su_num) == 10
su_num[9].isdigit() or su_num[9].lower() == "x" and su_num[:-1].isdecimal()
and (su_num[-1].isdecimal() or su_num[-1].lower() == "x")
): ):
product = 0 product = 0
# Iterate through code_string # Iterate through code_string