1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/searx_ynh.git synced 2024-09-03 20:16:30 +02:00
searx_ynh/sources/searx/engines/startpage.py

125 lines
3.6 KiB
Python
Raw Normal View History

2014-12-01 12:26:38 +01:00
# Startpage (Web)
#
# @website https://startpage.com
# @provide-api no (nothing found)
#
# @using-api no
# @results HTML
# @stable no (HTML can change)
# @parse url, title, content
#
# @todo paging
2014-05-10 11:56:19 +02:00
from lxml import html
2014-12-01 12:26:38 +01:00
from cgi import escape
2015-12-29 09:26:27 +01:00
from dateutil import parser
from datetime import datetime, timedelta
2014-12-01 12:26:38 +01:00
import re
2015-02-17 12:45:54 +01:00
from searx.engines.xpath import extract_text
2014-12-01 12:26:38 +01:00
# engine dependent config
categories = ['general']
# there is a mechanism to block "bot" search
# (probably the parameter qid), require
# storing of qid's between mulitble search-calls
# paging = False
language_support = True
2014-05-10 11:56:19 +02:00
2014-12-01 12:26:38 +01:00
# search-url
base_url = 'https://startpage.com/'
search_url = base_url + 'do/search'
2014-05-10 11:56:19 +02:00
2014-12-01 12:26:38 +01:00
# specific xpath variables
# ads xpath //div[@id="results"]/div[@id="sponsored"]//div[@class="result"]
# not ads: div[@class="result"] are the direct childs of div[@id="results"]
results_xpath = '//div[@class="result"]'
link_xpath = './/h3/a'
2014-05-10 11:56:19 +02:00
2014-12-01 12:26:38 +01:00
# do search-request
2014-05-10 11:56:19 +02:00
def request(query, params):
2014-12-01 12:26:38 +01:00
offset = (params['pageno'] - 1) * 10
2014-05-10 11:56:19 +02:00
params['url'] = search_url
params['method'] = 'POST'
params['data'] = {'query': query,
2014-12-01 12:26:38 +01:00
'startat': offset}
# set language if specified
if params['language'] != 'all':
2015-02-17 12:45:54 +01:00
params['data']['with_language'] = ('lang_' + params['language'].split('_')[0])
2014-12-01 12:26:38 +01:00
2014-05-10 11:56:19 +02:00
return params
2014-12-01 12:26:38 +01:00
# get response from search-request
2014-05-10 11:56:19 +02:00
def response(resp):
results = []
2014-12-01 12:26:38 +01:00
2014-05-10 11:56:19 +02:00
dom = html.fromstring(resp.content)
2014-12-01 12:26:38 +01:00
# parse results
for result in dom.xpath(results_xpath):
links = result.xpath(link_xpath)
if not links:
continue
link = links[0]
2014-05-10 11:56:19 +02:00
url = link.attrib.get('href')
2014-12-01 12:26:38 +01:00
# block google-ad url's
2015-09-08 23:05:37 +02:00
if re.match("^http(s|)://(www\.)?google\.[a-z]+/aclk.*$", url):
continue
# block startpage search url's
if re.match("^http(s|)://(www\.)?startpage\.com/do/search\?.*$", url):
continue
# block ixquick search url's
if re.match("^http(s|)://(www\.)?ixquick\.com/do/search\?.*$", url):
2014-05-10 11:56:19 +02:00
continue
2015-02-17 12:45:54 +01:00
title = escape(extract_text(link))
2015-12-29 09:26:27 +01:00
if result.xpath('./p[@class="desc clk"]'):
content = escape(extract_text(result.xpath('./p[@class="desc clk"]')))
2014-12-01 12:26:38 +01:00
else:
content = ''
2014-05-10 11:56:19 +02:00
2015-12-29 09:26:27 +01:00
published_date = None
# check if search result starts with something like: "2 Sep 2014 ... "
if re.match("^([1-9]|[1-2][0-9]|3[0-1]) [A-Z][a-z]{2} [0-9]{4} \.\.\. ", content):
date_pos = content.find('...')+4
date_string = content[0:date_pos-5]
published_date = parser.parse(date_string, dayfirst=True)
# fix content string
content = content[date_pos:]
# check if search result starts with something like: "5 days ago ... "
elif re.match("^[0-9]+ days? ago \.\.\. ", content):
date_pos = content.find('...')+4
date_string = content[0:date_pos-5]
# calculate datetime
published_date = datetime.now() - timedelta(days=int(re.match(r'\d+', date_string).group()))
# fix content string
content = content[date_pos:]
if published_date:
# append result
results.append({'url': url,
'title': title,
'content': content,
'publishedDate': published_date})
else:
# append result
results.append({'url': url,
'title': title,
'content': content})
2014-05-10 11:56:19 +02:00
2014-12-01 12:26:38 +01:00
# return results
2014-05-10 11:56:19 +02:00
return results