searxng/searx/engines/arxiv.py

77 lines
2.0 KiB
Python
Raw Normal View History

2017-09-23 12:16:06 +00:00
#!/usr/bin/env python
"""
ArXiV (Scientific preprints)
2017-11-01 11:28:18 +00:00
@website https://arxiv.org
2017-09-23 12:16:06 +00: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
categories = ['science']
paging = True
2017-09-23 12:16:06 +00:00
base_url = 'http://export.arxiv.org/api/query?search_query=all:'\
+ '{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
string_args = dict(query=query,
2017-09-23 12:16:06 +00: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 11:28:18 +00:00
dom = html.fromstring(resp.content)
search_results = dom.xpath('//entry')
2017-09-23 12:16:06 +00:00
for entry in search_results:
title = entry.xpath('.//title')[0].text
url = entry.xpath('.//id')[0].text
content_string = '{doi_content}{abstract_content}'
2017-11-01 11:28:18 +00:00
abstract = entry.xpath('.//summary')[0].text
2017-09-23 12:16:06 +00:00
# If a doi is available, add it to the snipppet
try:
doi_content = entry.xpath('.//link[@title="doi"]')[0].text
2017-11-01 11:28:18 +00:00
content = content_string.format(doi_content=doi_content, abstract_content=abstract)
2017-09-23 12:16:06 +00:00
except:
2017-11-01 11:28:18 +00:00
content = content_string.format(doi_content="", abstract_content=abstract)
2017-09-23 12:16:06 +00:00
if len(content) > 300:
content = content[0:300] + "..."
2017-09-23 12:16:06 +00:00
# TODO: center snippet on query term
publishedDate = datetime.strptime(entry.xpath('.//published')[0].text, '%Y-%m-%dT%H:%M:%SZ')
res_dict = {'url': url,
'title': title,
'publishedDate': publishedDate,
'content': content}
results.append(res_dict)
return results