| 
									
										
										
										
											2021-01-13 11:31:25 +01:00
										 |  |  | # SPDX-License-Identifier: AGPL-3.0-or-later | 
					
						
							| 
									
										
										
										
											2024-03-11 07:45:08 +01:00
										 |  |  | """BASE (Scholar publications)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-29 11:59:16 +02:00
										 |  |  | """
 | 
					
						
							| 
									
										
										
										
											2024-03-11 07:45:08 +01:00
										 |  |  | from datetime import datetime | 
					
						
							|  |  |  | import re | 
					
						
							| 
									
										
										
										
											2016-03-29 11:59:16 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-06 17:42:46 +02:00
										 |  |  | from urllib.parse import urlencode | 
					
						
							| 
									
										
										
										
											2016-03-29 11:59:16 +02:00
										 |  |  | from lxml import etree | 
					
						
							| 
									
										
										
										
											2016-11-30 18:43:03 +01:00
										 |  |  | from searx.utils import searx_useragent | 
					
						
							| 
									
										
										
										
											2016-03-29 11:59:16 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-13 11:31:25 +01:00
										 |  |  | # about | 
					
						
							|  |  |  | about = { | 
					
						
							|  |  |  |     "website": 'https://base-search.net', | 
					
						
							|  |  |  |     "wikidata_id": 'Q448335', | 
					
						
							|  |  |  |     "official_api_documentation": 'https://api.base-search.net/', | 
					
						
							|  |  |  |     "use_official_api": True, | 
					
						
							|  |  |  |     "require_api_key": False, | 
					
						
							|  |  |  |     "results": 'XML', | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2016-03-29 11:59:16 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | categories = ['science'] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-27 09:26:22 +01:00
										 |  |  | base_url = ( | 
					
						
							|  |  |  |     'https://api.base-search.net/cgi-bin/BaseHttpSearchInterface.fcgi' | 
					
						
							|  |  |  |     + '?func=PerformSearch&{query}&boost=oa&hits={hits}&offset={offset}' | 
					
						
							|  |  |  | ) | 
					
						
							| 
									
										
										
										
											2016-03-29 11:59:16 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | # engine dependent config | 
					
						
							|  |  |  | paging = True | 
					
						
							|  |  |  | number_of_results = 10 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # shortcuts for advanced search | 
					
						
							|  |  |  | shorcut_dict = { | 
					
						
							|  |  |  |     # user-friendly keywords | 
					
						
							|  |  |  |     'format:': 'dcformat:', | 
					
						
							|  |  |  |     'author:': 'dccreator:', | 
					
						
							|  |  |  |     'collection:': 'dccollection:', | 
					
						
							|  |  |  |     'hdate:': 'dchdate:', | 
					
						
							|  |  |  |     'contributor:': 'dccontributor:', | 
					
						
							|  |  |  |     'coverage:': 'dccoverage:', | 
					
						
							|  |  |  |     'date:': 'dcdate:', | 
					
						
							|  |  |  |     'abstract:': 'dcdescription:', | 
					
						
							|  |  |  |     'urls:': 'dcidentifier:', | 
					
						
							|  |  |  |     'language:': 'dclanguage:', | 
					
						
							|  |  |  |     'publisher:': 'dcpublisher:', | 
					
						
							|  |  |  |     'relation:': 'dcrelation:', | 
					
						
							|  |  |  |     'rights:': 'dcrights:', | 
					
						
							|  |  |  |     'source:': 'dcsource:', | 
					
						
							|  |  |  |     'subject:': 'dcsubject:', | 
					
						
							|  |  |  |     'title:': 'dctitle:', | 
					
						
							| 
									
										
										
										
											2021-12-27 09:26:22 +01:00
										 |  |  |     'type:': 'dcdctype:', | 
					
						
							| 
									
										
										
										
											2016-03-29 11:59:16 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def request(query, params): | 
					
						
							|  |  |  |     # replace shortcuts with API advanced search keywords | 
					
						
							| 
									
										
										
										
											2024-03-11 07:45:08 +01:00
										 |  |  |     for key, val in shorcut_dict.items(): | 
					
						
							|  |  |  |         query = re.sub(key, val, query) | 
					
						
							| 
									
										
										
										
											2016-03-29 11:59:16 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # basic search | 
					
						
							|  |  |  |     offset = (params['pageno'] - 1) * number_of_results | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-11 07:45:08 +01:00
										 |  |  |     string_args = { | 
					
						
							|  |  |  |         'query': urlencode({'query': query}), | 
					
						
							|  |  |  |         'offset': offset, | 
					
						
							|  |  |  |         'hits': number_of_results, | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-03-29 11:59:16 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     params['url'] = base_url.format(**string_args) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     params['headers']['User-Agent'] = searx_useragent() | 
					
						
							|  |  |  |     return params | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def response(resp): | 
					
						
							|  |  |  |     results = [] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-01 16:50:27 +01:00
										 |  |  |     search_results = etree.XML(resp.content) | 
					
						
							| 
									
										
										
										
											2016-03-29 11:59:16 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     for entry in search_results.xpath('./result/doc'): | 
					
						
							|  |  |  |         content = "No description available" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         date = datetime.now()  # needed in case no dcdate is available for an item | 
					
						
							|  |  |  |         for item in entry: | 
					
						
							| 
									
										
										
										
											2020-11-16 09:43:23 +01:00
										 |  |  |             if item.attrib["name"] == "dcdate": | 
					
						
							| 
									
										
										
										
											2016-03-29 11:59:16 +02:00
										 |  |  |                 date = item.text | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             elif item.attrib["name"] == "dctitle": | 
					
						
							|  |  |  |                 title = item.text | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             elif item.attrib["name"] == "dclink": | 
					
						
							|  |  |  |                 url = item.text | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             elif item.attrib["name"] == "dcdescription": | 
					
						
							| 
									
										
										
										
											2016-12-09 11:44:24 +01:00
										 |  |  |                 content = item.text[:300] | 
					
						
							| 
									
										
										
										
											2016-03-29 11:59:16 +02:00
										 |  |  |                 if len(item.text) > 300: | 
					
						
							|  |  |  |                     content += "..." | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-27 09:26:22 +01:00
										 |  |  |         # dates returned by the BASE API are not several formats | 
					
						
							| 
									
										
										
										
											2016-03-29 11:59:16 +02:00
										 |  |  |         publishedDate = None | 
					
						
							|  |  |  |         for date_format in ['%Y-%m-%dT%H:%M:%SZ', '%Y-%m-%d', '%Y-%m', '%Y']: | 
					
						
							|  |  |  |             try: | 
					
						
							|  |  |  |                 publishedDate = datetime.strptime(date, date_format) | 
					
						
							|  |  |  |                 break | 
					
						
							| 
									
										
										
										
											2024-03-11 07:45:08 +01:00
										 |  |  |             except:  # pylint: disable=bare-except | 
					
						
							| 
									
										
										
										
											2016-03-29 11:59:16 +02:00
										 |  |  |                 pass | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if publishedDate is not None: | 
					
						
							| 
									
										
										
										
											2021-12-27 09:26:22 +01:00
										 |  |  |             res_dict = {'url': url, 'title': title, 'publishedDate': publishedDate, 'content': content} | 
					
						
							| 
									
										
										
										
											2016-03-29 11:59:16 +02:00
										 |  |  |         else: | 
					
						
							| 
									
										
										
										
											2021-12-27 09:26:22 +01:00
										 |  |  |             res_dict = {'url': url, 'title': title, 'content': content} | 
					
						
							| 
									
										
										
										
											2016-03-29 11:59:16 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         results.append(res_dict) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return results |