| 
									
										
										
										
											2021-01-13 11:31:25 +01:00
										 |  |  | # SPDX-License-Identifier: AGPL-3.0-or-later | 
					
						
							| 
									
										
										
										
											2024-03-11 07:45:08 +01:00
										 |  |  | """ArXiV (Scientific preprints)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-23 14:16:06 +02:00
										 |  |  | """
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-11 07:45:08 +01:00
										 |  |  | from datetime import datetime | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-26 18:10:12 +02:00
										 |  |  | from lxml import etree | 
					
						
							|  |  |  | from lxml.etree import XPath | 
					
						
							|  |  |  | from searx.utils import eval_xpath, eval_xpath_list, eval_xpath_getindex | 
					
						
							| 
									
										
										
										
											2017-09-23 14:16:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-13 11:31:25 +01:00
										 |  |  | # about | 
					
						
							|  |  |  | about = { | 
					
						
							|  |  |  |     "website": 'https://arxiv.org', | 
					
						
							|  |  |  |     "wikidata_id": 'Q118398', | 
					
						
							|  |  |  |     "official_api_documentation": 'https://arxiv.org/help/api', | 
					
						
							|  |  |  |     "use_official_api": True, | 
					
						
							|  |  |  |     "require_api_key": False, | 
					
						
							|  |  |  |     "results": 'XML-RSS', | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-09-23 14:16:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-26 18:10:12 +02:00
										 |  |  | categories = ['science', 'scientific publications'] | 
					
						
							| 
									
										
										
										
											2019-10-16 13:12:17 +02:00
										 |  |  | paging = True | 
					
						
							| 
									
										
										
										
											2017-09-23 14:16:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-27 09:26:22 +01:00
										 |  |  | base_url = ( | 
					
						
							|  |  |  |     'https://export.arxiv.org/api/query?search_query=all:' + '{query}&start={offset}&max_results={number_of_results}' | 
					
						
							|  |  |  | ) | 
					
						
							| 
									
										
										
										
											2017-09-23 14:16:06 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | # engine dependent config | 
					
						
							|  |  |  | number_of_results = 10 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-26 18:10:12 +02:00
										 |  |  | # xpaths | 
					
						
							|  |  |  | arxiv_namespaces = { | 
					
						
							|  |  |  |     "atom": "http://www.w3.org/2005/Atom", | 
					
						
							|  |  |  |     "arxiv": "http://arxiv.org/schemas/atom", | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | xpath_entry = XPath('//atom:entry', namespaces=arxiv_namespaces) | 
					
						
							|  |  |  | xpath_title = XPath('.//atom:title', namespaces=arxiv_namespaces) | 
					
						
							|  |  |  | xpath_id = XPath('.//atom:id', namespaces=arxiv_namespaces) | 
					
						
							|  |  |  | xpath_summary = XPath('.//atom:summary', namespaces=arxiv_namespaces) | 
					
						
							|  |  |  | xpath_author_name = XPath('.//atom:author/atom:name', namespaces=arxiv_namespaces) | 
					
						
							|  |  |  | xpath_doi = XPath('.//arxiv:doi', namespaces=arxiv_namespaces) | 
					
						
							|  |  |  | xpath_pdf = XPath('.//atom:link[@title="pdf"]', namespaces=arxiv_namespaces) | 
					
						
							|  |  |  | xpath_published = XPath('.//atom:published', namespaces=arxiv_namespaces) | 
					
						
							|  |  |  | xpath_journal = XPath('.//arxiv:journal_ref', namespaces=arxiv_namespaces) | 
					
						
							|  |  |  | xpath_category = XPath('.//atom:category/@term', namespaces=arxiv_namespaces) | 
					
						
							|  |  |  | xpath_comment = XPath('./arxiv:comment', namespaces=arxiv_namespaces) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-23 14:16:06 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | def request(query, params): | 
					
						
							|  |  |  |     # basic search | 
					
						
							|  |  |  |     offset = (params['pageno'] - 1) * number_of_results | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-11 07:45:08 +01:00
										 |  |  |     string_args = {'query': query, 'offset': offset, 'number_of_results': number_of_results} | 
					
						
							| 
									
										
										
										
											2017-09-23 14:16:06 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     params['url'] = base_url.format(**string_args) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return params | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def response(resp): | 
					
						
							|  |  |  |     results = [] | 
					
						
							| 
									
										
										
										
											2022-08-26 18:10:12 +02:00
										 |  |  |     dom = etree.fromstring(resp.content) | 
					
						
							|  |  |  |     for entry in eval_xpath_list(dom, xpath_entry): | 
					
						
							|  |  |  |         title = eval_xpath_getindex(entry, xpath_title, 0).text | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         url = eval_xpath_getindex(entry, xpath_id, 0).text | 
					
						
							|  |  |  |         abstract = eval_xpath_getindex(entry, xpath_summary, 0).text | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         authors = [author.text for author in eval_xpath_list(entry, xpath_author_name)] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         #  doi | 
					
						
							|  |  |  |         doi_element = eval_xpath_getindex(entry, xpath_doi, 0, default=None) | 
					
						
							|  |  |  |         doi = None if doi_element is None else doi_element.text | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # pdf | 
					
						
							|  |  |  |         pdf_element = eval_xpath_getindex(entry, xpath_pdf, 0, default=None) | 
					
						
							|  |  |  |         pdf_url = None if pdf_element is None else pdf_element.attrib.get('href') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # journal | 
					
						
							|  |  |  |         journal_element = eval_xpath_getindex(entry, xpath_journal, 0, default=None) | 
					
						
							|  |  |  |         journal = None if journal_element is None else journal_element.text | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # tags | 
					
						
							|  |  |  |         tag_elements = eval_xpath(entry, xpath_category) | 
					
						
							|  |  |  |         tags = [str(tag) for tag in tag_elements] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # comments | 
					
						
							|  |  |  |         comments_elements = eval_xpath_getindex(entry, xpath_comment, 0, default=None) | 
					
						
							|  |  |  |         comments = None if comments_elements is None else comments_elements.text | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         publishedDate = datetime.strptime(eval_xpath_getindex(entry, xpath_published, 0).text, '%Y-%m-%dT%H:%M:%SZ') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         res_dict = { | 
					
						
							|  |  |  |             'template': 'paper.html', | 
					
						
							|  |  |  |             'url': url, | 
					
						
							|  |  |  |             'title': title, | 
					
						
							|  |  |  |             'publishedDate': publishedDate, | 
					
						
							|  |  |  |             'content': abstract, | 
					
						
							|  |  |  |             'doi': doi, | 
					
						
							|  |  |  |             'authors': authors, | 
					
						
							|  |  |  |             'journal': journal, | 
					
						
							|  |  |  |             'tags': tags, | 
					
						
							|  |  |  |             'comments': comments, | 
					
						
							|  |  |  |             'pdf_url': pdf_url, | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-09-23 14:16:06 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         results.append(res_dict) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return results |