Skip to content

Commit

Permalink
manubot#337 Timeout for arxiv.
Browse files Browse the repository at this point in the history
  • Loading branch information
xihh87 committed Jun 15, 2022
1 parent 86e7085 commit dcb360c
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions manubot/cite/arxiv.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ def inspect(self, citekey):
if not self._get_pattern().fullmatch(citekey.accession):
return "arXiv identifiers must conform to syntax described at https://arxiv.org/help/arxiv_identifier."

def get_csl_item(self, citekey):
return get_arxiv_csl_item(citekey.standard_accession)
def get_csl_item(self, citekey, timeout: int = 3):
return get_arxiv_csl_item(citekey.standard_accession, timeout=timeout)


class CSL_Item_arXiv(CSL_Item):
Expand Down Expand Up @@ -77,15 +77,15 @@ def get_arxiv_csl_item(arxiv_id: str):
return get_arxiv_csl_item_oai(arxiv_id)


def query_arxiv_api(url, params):
def query_arxiv_api(url, params, timeout: int = 3):
headers = {"User-Agent": get_manubot_user_agent()}
response = requests.get(url, params, headers=headers)
response = requests.get(url, params, headers=headers, timeout=timeout)
response.raise_for_status()
xml_tree = xml.etree.ElementTree.fromstring(response.text)
return xml_tree


def get_arxiv_csl_item_export_api(arxiv_id):
def get_arxiv_csl_item_export_api(arxiv_id, timeout: int = 3):
"""
Return csl_item item for an arXiv record.
Expand All @@ -105,6 +105,7 @@ def get_arxiv_csl_item_export_api(arxiv_id):
xml_tree = query_arxiv_api(
url="https://export.arxiv.org/api/query",
params={"id_list": arxiv_id, "max_results": 1},
timeout=timeout,
)

# XML namespace prefixes
Expand Down Expand Up @@ -157,7 +158,7 @@ def get_arxiv_csl_item_export_api(arxiv_id):
return csl_item


def get_arxiv_csl_item_oai(arxiv_id):
def get_arxiv_csl_item_oai(arxiv_id, timeout: int = 3):
"""
Generate a CSL Item for an unversioned arXiv identifier
using arXiv's OAI_PMH v2.0 API <https://arxiv.org/help/oa>.
Expand All @@ -174,6 +175,7 @@ def get_arxiv_csl_item_oai(arxiv_id):
"metadataPrefix": "arXiv",
"identifier": f"oai:arXiv.org:{arxiv_id}",
},
timeout=timeout,
)

# Create dictionary for CSL Item
Expand Down Expand Up @@ -237,10 +239,10 @@ def remove_newlines(text):
return re.sub(pattern=r"\n(?!\s)", repl=" ", string=text)


def get_arxiv_csl_item_zotero(arxiv_id):
def get_arxiv_csl_item_zotero(arxiv_id, timeout: int = 3):
"""
Generate CSL JSON Data for an arXiv ID using Zotero's translation-server.
"""
from manubot.cite.zotero import get_csl_item

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

0 comments on commit dcb360c

Please sign in to comment.