.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
arxiv.py
Go to the documentation of this file.
1# SPDX-License-Identifier: AGPL-3.0-or-later
2"""ArXiV (Scientific preprints)
3
4"""
5
6from datetime import datetime
7
8from lxml import etree
9from lxml.etree import XPath
10from searx.utils import eval_xpath, eval_xpath_list, eval_xpath_getindex
11
12# about
13about = {
14 "website": 'https://arxiv.org',
15 "wikidata_id": 'Q118398',
16 "official_api_documentation": 'https://arxiv.org/help/api',
17 "use_official_api": True,
18 "require_api_key": False,
19 "results": 'XML-RSS',
20}
21
22categories = ['science', 'scientific publications']
23paging = True
24
25base_url = (
26 'https://export.arxiv.org/api/query?search_query=all:' + '{query}&start={offset}&max_results={number_of_results}'
27)
28
29# engine dependent config
30number_of_results = 10
31
32# xpaths
33arxiv_namespaces = {
34 "atom": "http://www.w3.org/2005/Atom",
35 "arxiv": "http://arxiv.org/schemas/atom",
36}
37xpath_entry = XPath('//atom:entry', namespaces=arxiv_namespaces)
38xpath_title = XPath('.//atom:title', namespaces=arxiv_namespaces)
39xpath_id = XPath('.//atom:id', namespaces=arxiv_namespaces)
40xpath_summary = XPath('.//atom:summary', namespaces=arxiv_namespaces)
41xpath_author_name = XPath('.//atom:author/atom:name', namespaces=arxiv_namespaces)
42xpath_doi = XPath('.//arxiv:doi', namespaces=arxiv_namespaces)
43xpath_pdf = XPath('.//atom:link[@title="pdf"]', namespaces=arxiv_namespaces)
44xpath_published = XPath('.//atom:published', namespaces=arxiv_namespaces)
45xpath_journal = XPath('.//arxiv:journal_ref', namespaces=arxiv_namespaces)
46xpath_category = XPath('.//atom:category/@term', namespaces=arxiv_namespaces)
47xpath_comment = XPath('./arxiv:comment', namespaces=arxiv_namespaces)
48
49
50def request(query, params):
51 # basic search
52 offset = (params['pageno'] - 1) * number_of_results
53
54 string_args = {'query': query, 'offset': offset, 'number_of_results': number_of_results}
55
56 params['url'] = base_url.format(**string_args)
57
58 return params
59
60
61def response(resp):
62 results = []
63 dom = etree.fromstring(resp.content)
64 for entry in eval_xpath_list(dom, xpath_entry):
65 title = eval_xpath_getindex(entry, xpath_title, 0).text
66
67 url = eval_xpath_getindex(entry, xpath_id, 0).text
68 abstract = eval_xpath_getindex(entry, xpath_summary, 0).text
69
70 authors = [author.text for author in eval_xpath_list(entry, xpath_author_name)]
71
72 # doi
73 doi_element = eval_xpath_getindex(entry, xpath_doi, 0, default=None)
74 doi = None if doi_element is None else doi_element.text
75
76 # pdf
77 pdf_element = eval_xpath_getindex(entry, xpath_pdf, 0, default=None)
78 pdf_url = None if pdf_element is None else pdf_element.attrib.get('href')
79
80 # journal
81 journal_element = eval_xpath_getindex(entry, xpath_journal, 0, default=None)
82 journal = None if journal_element is None else journal_element.text
83
84 # tags
85 tag_elements = eval_xpath(entry, xpath_category)
86 tags = [str(tag) for tag in tag_elements]
87
88 # comments
89 comments_elements = eval_xpath_getindex(entry, xpath_comment, 0, default=None)
90 comments = None if comments_elements is None else comments_elements.text
91
92 publishedDate = datetime.strptime(eval_xpath_getindex(entry, xpath_published, 0).text, '%Y-%m-%dT%H:%M:%SZ')
93
94 res_dict = {
95 'template': 'paper.html',
96 'url': url,
97 'title': title,
98 'publishedDate': publishedDate,
99 'content': abstract,
100 'doi': doi,
101 'authors': authors,
102 'journal': journal,
103 'tags': tags,
104 'comments': comments,
105 'pdf_url': pdf_url,
106 }
107
108 results.append(res_dict)
109
110 return results
request(query, params)
Definition arxiv.py:50