add isbn check

This commit is contained in:
Willi Hohenstein 2022-02-13 09:35:15 +01:00
parent 27c26b4d16
commit 5801ef011f

View file

@ -12,6 +12,9 @@ from bookwyrm.settings import MEDIA_FULL_URL
# pylint: disable=arguments-differ # pylint: disable=arguments-differ
def search(query, min_confidence=0, filters=None, return_first=False): def search(query, min_confidence=0, filters=None, return_first=False):
# check if query is isbn
if isbn_check(query):
query = query.replace('-','')
"""search your local database""" """search your local database"""
filters = filters or [] filters = filters or []
if not query: if not query:
@ -133,6 +136,32 @@ def search_title_author(query, min_confidence, *filters, return_first=False):
return list_results return list_results
def isbn_check(query):
n = query.replace('-','')
if len(n) == 13:
# Multiply every other digit by 3
# Add these numbers and the other digits
product = (sum(int(ch) for ch in n[::2])
+ sum(int(ch) * 3 for ch in n[1::2]))
if product % 10 == 0:
return True
elif len(n) == 10:
if n[0:8].isdigit() and (n[9].isdigit() or n[9].lower() == "x"):
product = 0
# Iterate through code_string
for i in range(9):
# for each character, multiply by a different decreasing number: 10 - x
product = product + int(n[i]) * (10 - i)
# Handle last character
if n[9].lower() == "x":
product += 10
else:
product += int(n[9])
if product % 11 == 0:
return True
return False
@dataclass @dataclass
class SearchResult: class SearchResult:
"""standardized search result object""" """standardized search result object"""