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 15, 2022
1 parent fb39504 commit 58fb8ab
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions manubot/cite/isbn.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ class Handler_ISBN(Handler):
"isbn",
]

def inspect(self, citekey):
def inspect(self, citekey, timeout=3):
import isbnlib
isbnlib.config.setthreadstimeout(seconds=timeout)
isbnlib.config.seturlopentimeout(seconds=timeout)

fail = isbnlib.notisbn(citekey.accession, level="strict")
if fail:
Expand All @@ -30,7 +32,7 @@ def get_csl_item(self, citekey):
return get_isbn_csl_item(citekey.standard_accession)


def get_isbn_csl_item(isbn: str):
def get_isbn_csl_item(isbn: str, timeout=3):
"""
Generate CSL JSON Data for an ISBN. Converts all ISBNs to 13-digit format.
Expand All @@ -40,6 +42,8 @@ def get_isbn_csl_item(isbn: str):
non-failing method.
"""
import isbnlib
isbnlib.config.setthreadstimeout(seconds=timeout)
isbnlib.config.seturlopentimeout(seconds=timeout)

isbn = isbnlib.to_isbn13(isbn)
for retriever in isbn_retrievers:
Expand All @@ -63,7 +67,7 @@ def get_isbn_csl_item_zotero(isbn: str):
return get_csl_item(f"isbn:{isbn}")


def get_isbn_csl_item_citoid(isbn: str):
def get_isbn_csl_item_citoid(isbn: str, timeout=3):
"""
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 +78,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=timeut)
result = response.json()
if isinstance(result, dict):
if result["title"] == "Not found.":
Expand Down Expand Up @@ -125,11 +129,13 @@ 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=3):
"""
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)
Expand Down

0 comments on commit 58fb8ab

Please sign in to comment.