Skip to content

Commit

Permalink
manubot#337 Add unpaywall timeout.
Browse files Browse the repository at this point in the history
unpaywall -[depends]-> arxiv
  • Loading branch information
xihh87 committed Jul 28, 2022
1 parent 95a116b commit c7bcdaa
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions manubot/cite/unpaywall.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
Defaults to licenses that conform to <https://opendefinition.org/>.
"""
open_licenses = {"cc0", "cc-by", "cc-by-sa", "pd"}
default_timeout = 3


class Unpaywall:
Expand All @@ -24,7 +25,7 @@ class Unpaywall:
csl_item = None

@abc.abstractmethod
def set_oa_locations(self):
def set_oa_locations(self, timeout_seconds: int = default_timeout):
"""
Set `self.oa_locations`, which is a list of `Unpaywall_Location` objects.
"""
Expand Down Expand Up @@ -100,12 +101,12 @@ def __init__(self, doi, set_oa_locations=True):
if set_oa_locations:
self.set_oa_locations()

def set_oa_locations(self):
def set_oa_locations(self, timeout_seconds: int = default_timeout):
from manubot.util import contact_email

url = f"https://api.unpaywall.org/v2/{self.doi}"
params = {"email": contact_email}
response = requests.get(url, params=params)
response = requests.get(url, params=params, timeout=timeout_seconds)
response.raise_for_status()
self.results = response.json()
self.oa_locations = [
Expand All @@ -124,11 +125,13 @@ def __init__(self, arxiv_id, set_oa_locations=True, use_doi=True):
if set_oa_locations:
self.set_oa_locations()

def set_oa_locations(self):
def set_oa_locations(self, timeout_seconds: int = default_timeout):
from .arxiv import get_arxiv_csl_item

if not self.csl_item:
self.csl_item = get_arxiv_csl_item(self.arxiv_id)
self.csl_item = get_arxiv_csl_item(
self.arxiv_id, timeout_seconds=timeout_seconds
)
doi = self.csl_item.get("DOI")
if self.use_doi and doi:
unpaywall_doi = Unpaywall_DOI(doi)
Expand Down

0 comments on commit c7bcdaa

Please sign in to comment.