.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
base.py
Go to the documentation of this file.
1# SPDX-License-Identifier: AGPL-3.0-or-later
2"""BASE (Scholar publications)
3
4"""
5from datetime import datetime
6import re
7
8from urllib.parse import urlencode
9from lxml import etree
10from searx.utils import searx_useragent
11
12# about
13about = {
14 "website": 'https://base-search.net',
15 "wikidata_id": 'Q448335',
16 "official_api_documentation": 'https://api.base-search.net/',
17 "use_official_api": True,
18 "require_api_key": False,
19 "results": 'XML',
20}
21
22categories = ['science']
23
24base_url = (
25 'https://api.base-search.net/cgi-bin/BaseHttpSearchInterface.fcgi'
26 + '?func=PerformSearch&{query}&boost=oa&hits={hits}&offset={offset}'
27)
28
29# engine dependent config
30paging = True
31number_of_results = 10
32
33# shortcuts for advanced search
34shorcut_dict = {
35 # user-friendly keywords
36 'format:': 'dcformat:',
37 'author:': 'dccreator:',
38 'collection:': 'dccollection:',
39 'hdate:': 'dchdate:',
40 'contributor:': 'dccontributor:',
41 'coverage:': 'dccoverage:',
42 'date:': 'dcdate:',
43 'abstract:': 'dcdescription:',
44 'urls:': 'dcidentifier:',
45 'language:': 'dclanguage:',
46 'publisher:': 'dcpublisher:',
47 'relation:': 'dcrelation:',
48 'rights:': 'dcrights:',
49 'source:': 'dcsource:',
50 'subject:': 'dcsubject:',
51 'title:': 'dctitle:',
52 'type:': 'dcdctype:',
53}
54
55
56def request(query, params):
57 # replace shortcuts with API advanced search keywords
58 for key, val in shorcut_dict.items():
59 query = re.sub(key, val, query)
60
61 # basic search
62 offset = (params['pageno'] - 1) * number_of_results
63
64 string_args = {
65 'query': urlencode({'query': query}),
66 'offset': offset,
67 'hits': number_of_results,
68 }
69
70 params['url'] = base_url.format(**string_args)
71
72 params['headers']['User-Agent'] = searx_useragent()
73 return params
74
75
76def response(resp):
77 results = []
78
79 search_results = etree.XML(resp.content)
80
81 for entry in search_results.xpath('./result/doc'):
82 content = "No description available"
83
84 date = datetime.now() # needed in case no dcdate is available for an item
85 for item in entry:
86 if item.attrib["name"] == "dcdate":
87 date = item.text
88
89 elif item.attrib["name"] == "dctitle":
90 title = item.text
91
92 elif item.attrib["name"] == "dclink":
93 url = item.text
94
95 elif item.attrib["name"] == "dcdescription":
96 content = item.text[:300]
97 if len(item.text) > 300:
98 content += "..."
99
100 # dates returned by the BASE API are not several formats
101 publishedDate = None
102 for date_format in ['%Y-%m-%dT%H:%M:%SZ', '%Y-%m-%d', '%Y-%m', '%Y']:
103 try:
104 publishedDate = datetime.strptime(date, date_format)
105 break
106 except: # pylint: disable=bare-except
107 pass
108
109 if publishedDate is not None:
110 res_dict = {'url': url, 'title': title, 'publishedDate': publishedDate, 'content': content}
111 else:
112 res_dict = {'url': url, 'title': title, 'content': content}
113
114 results.append(res_dict)
115
116 return results
response(resp)
Definition base.py:76
request(query, params)
Definition base.py:56