From 5801ef011fc535f11da6091b06579b9b14acbc3f Mon Sep 17 00:00:00 2001 From: Willi Hohenstein Date: Sun, 13 Feb 2022 09:35:15 +0100 Subject: [PATCH] add isbn check --- bookwyrm/book_search.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/bookwyrm/book_search.py b/bookwyrm/book_search.py index e42a6d8c3..10bf6499e 100644 --- a/bookwyrm/book_search.py +++ b/bookwyrm/book_search.py @@ -12,6 +12,9 @@ from bookwyrm.settings import MEDIA_FULL_URL # pylint: disable=arguments-differ 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""" filters = filters or [] if not query: @@ -133,6 +136,32 @@ def search_title_author(query, min_confidence, *filters, return_first=False): 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 class SearchResult: """standardized search result object"""