Skip to content

Commit

Permalink
manubot#337 Allow setting timeout on ISBN search.
Browse files Browse the repository at this point in the history
  • Loading branch information
xihh87 committed Jun 17, 2022
1 parent adb7383 commit 46189ef
Showing 1 changed file with 20 additions and 9 deletions.
29 changes: 20 additions & 9 deletions manubot/cite/isbn.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

from .handlers import Handler

timeout_seconds = 3


class Handler_ISBN(Handler):

Expand All @@ -13,9 +15,12 @@ class Handler_ISBN(Handler):
"isbn",
]

def inspect(self, citekey):
def inspect(self, citekey, timeout: int = timeout_seconds):
import isbnlib

isbnlib.config.setthreadstimeout(seconds=timeout)
isbnlib.config.seturlopentimeout(seconds=timeout)

fail = isbnlib.notisbn(citekey.accession, level="strict")
if fail:
return f"identifier violates the ISBN syntax according to isbnlib v{isbnlib.__version__}"
Expand All @@ -26,11 +31,11 @@ def standardize_prefix_accession(self, accession):
accession = to_isbn13(accession)
return self.standard_prefix, accession

def get_csl_item(self, citekey):
return get_isbn_csl_item(citekey.standard_accession)
def get_csl_item(self, citekey, timeout: int = timeout_seconds):
return get_isbn_csl_item(citekey.standard_accession, timeout=timeout)


def get_isbn_csl_item(isbn: str):
def get_isbn_csl_item(isbn: str, timeout: int = timeout_seconds):
"""
Generate CSL JSON Data for an ISBN. Converts all ISBNs to 13-digit format.
Expand All @@ -41,6 +46,9 @@ def get_isbn_csl_item(isbn: str):
"""
import isbnlib

isbnlib.config.setthreadstimeout(seconds=timeout)
isbnlib.config.seturlopentimeout(seconds=timeout)

isbn = isbnlib.to_isbn13(isbn)
for retriever in isbn_retrievers:
try:
Expand All @@ -54,16 +62,16 @@ def get_isbn_csl_item(isbn: str):
raise Exception(f"all get_isbn_csl_item methods failed for {isbn}")


def get_isbn_csl_item_zotero(isbn: str):
def get_isbn_csl_item_zotero(isbn: str, timeout: int = timeout_seconds):
"""
Generate CSL JSON Data for an ISBN using Zotero's translation-server.
"""
from manubot.cite.zotero import get_csl_item

return get_csl_item(f"isbn:{isbn}")
return get_csl_item(f"isbn:{isbn}", timeout=timeout)


def get_isbn_csl_item_citoid(isbn: str):
def get_isbn_csl_item_citoid(isbn: str, timeout: int = timeout_seconds):
"""
Return CSL JSON Data for an ISBN using the Wikipedia Citoid API.
https://en.wikipedia.org/api/rest_v1/#!/Citation/getCitation
Expand All @@ -74,7 +82,7 @@ def get_isbn_csl_item_citoid(isbn: str):

headers = {"User-Agent": get_manubot_user_agent()}
url = f"https://en.wikipedia.org/api/rest_v1/data/citation/mediawiki/{isbn}"
response = requests.get(url, headers=headers)
response = requests.get(url, headers=headers, timeout=timeout)
result = response.json()
if isinstance(result, dict):
if result["title"] == "Not found.":
Expand Down Expand Up @@ -125,12 +133,15 @@ def get_isbn_csl_item_citoid(isbn: str):
return csl_item


def get_isbn_csl_item_isbnlib(isbn: str):
def get_isbn_csl_item_isbnlib(isbn: str, timeout: int = timeout_seconds):
"""
Generate CSL JSON Data for an ISBN using isbnlib.
"""
import isbnlib

isbnlib.config.setthreadstimeout(seconds=timeout)
isbnlib.config.seturlopentimeout(seconds=timeout)

metadata = isbnlib.meta(isbn)
csl_json = isbnlib.registry.bibformatters["csl"](metadata)
csl_data = json.loads(csl_json)
Expand Down

0 comments on commit 46189ef

Please sign in to comment.