| 
									
										
										
										
											2017-09-23 14:16:06 +02:00
										 |  |  | #!/usr/bin/env python | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | """
 | 
					
						
							|  |  |  |  ArXiV (Scientific preprints) | 
					
						
							| 
									
										
										
										
											2017-11-01 12:28:18 +01:00
										 |  |  |  @website     https://arxiv.org | 
					
						
							| 
									
										
										
										
											2017-09-23 14:16:06 +02:00
										 |  |  |  @provide-api yes (export.arxiv.org/api/query) | 
					
						
							|  |  |  |  @using-api   yes | 
					
						
							|  |  |  |  @results     XML-RSS | 
					
						
							|  |  |  |  @stable      yes | 
					
						
							|  |  |  |  @parse       url, title, publishedDate, content | 
					
						
							|  |  |  |  More info on api: https://arxiv.org/help/api/user-manual | 
					
						
							|  |  |  | """
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | from lxml import html | 
					
						
							|  |  |  | from datetime import datetime | 
					
						
							| 
									
										
										
										
											2020-11-26 15:49:33 +01:00
										 |  |  | from searx.utils import eval_xpath_list, eval_xpath_getindex | 
					
						
							| 
									
										
										
										
											2017-09-23 14:16:06 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | categories = ['science'] | 
					
						
							| 
									
										
										
										
											2019-10-16 13:12:17 +02:00
										 |  |  | paging = True | 
					
						
							| 
									
										
										
										
											2017-09-23 14:16:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-09 17:33:18 +01:00
										 |  |  | base_url = 'https://export.arxiv.org/api/query?search_query=all:'\ | 
					
						
							| 
									
										
										
										
											2017-09-23 14:16:06 +02:00
										 |  |  |            + '{query}&start={offset}&max_results={number_of_results}' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # engine dependent config | 
					
						
							|  |  |  | number_of_results = 10 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def request(query, params): | 
					
						
							|  |  |  |     # basic search | 
					
						
							|  |  |  |     offset = (params['pageno'] - 1) * number_of_results | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-11 16:25:03 +02:00
										 |  |  |     string_args = dict(query=query, | 
					
						
							| 
									
										
										
										
											2017-09-23 14:16:06 +02:00
										 |  |  |                        offset=offset, | 
					
						
							|  |  |  |                        number_of_results=number_of_results) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     params['url'] = base_url.format(**string_args) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return params | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def response(resp): | 
					
						
							|  |  |  |     results = [] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-01 12:28:18 +01:00
										 |  |  |     dom = html.fromstring(resp.content) | 
					
						
							| 
									
										
										
										
											2017-09-23 14:16:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-26 15:49:33 +01:00
										 |  |  |     for entry in eval_xpath_list(dom, '//entry'): | 
					
						
							|  |  |  |         title = eval_xpath_getindex(entry, './/title', 0).text | 
					
						
							| 
									
										
										
										
											2017-09-23 14:16:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-26 15:49:33 +01:00
										 |  |  |         url = eval_xpath_getindex(entry, './/id', 0).text | 
					
						
							| 
									
										
										
										
											2017-09-23 14:16:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-27 16:01:31 +02:00
										 |  |  |         content_string = '{doi_content}{abstract_content}' | 
					
						
							| 
									
										
										
										
											2017-11-01 12:28:18 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-26 15:49:33 +01:00
										 |  |  |         abstract = eval_xpath_getindex(entry, './/summary', 0).text | 
					
						
							| 
									
										
										
										
											2017-09-23 14:16:06 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         #  If a doi is available, add it to the snipppet | 
					
						
							| 
									
										
										
										
											2020-11-26 15:49:33 +01:00
										 |  |  |         doi_element = eval_xpath_getindex(entry, './/link[@title="doi"]', 0, default=None) | 
					
						
							|  |  |  |         doi_content = doi_element.text if doi_element is not None else '' | 
					
						
							|  |  |  |         content = content_string.format(doi_content=doi_content, abstract_content=abstract) | 
					
						
							| 
									
										
										
										
											2017-09-23 14:16:06 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if len(content) > 300: | 
					
						
							| 
									
										
										
										
											2020-11-16 09:43:23 +01:00
										 |  |  |             content = content[0:300] + "..." | 
					
						
							| 
									
										
										
										
											2017-09-23 14:16:06 +02:00
										 |  |  |         # TODO: center snippet on query term | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-26 15:49:33 +01:00
										 |  |  |         publishedDate = datetime.strptime(eval_xpath_getindex(entry, './/published', 0).text, '%Y-%m-%dT%H:%M:%SZ') | 
					
						
							| 
									
										
										
										
											2017-09-23 14:16:06 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         res_dict = {'url': url, | 
					
						
							|  |  |  |                     'title': title, | 
					
						
							|  |  |  |                     'publishedDate': publishedDate, | 
					
						
							|  |  |  |                     'content': content} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         results.append(res_dict) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return results |