summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--searx/engines/google.py142
-rw-r--r--searx/engines/swisscows.py108
-rw-r--r--searx/settings.yml4
-rw-r--r--searx/tests/engines/test_google.py29
-rw-r--r--searx/tests/engines/test_swisscows.py124
-rw-r--r--searx/tests/test_engines.py1
6 files changed, 387 insertions, 21 deletions
diff --git a/searx/engines/google.py b/searx/engines/google.py
index 807c58ed5..785cd5e66 100644
--- a/searx/engines/google.py
+++ b/searx/engines/google.py
@@ -14,18 +14,76 @@ from lxml import html
from searx.poolrequests import get
from searx.engines.xpath import extract_text, extract_url
+
# engine dependent config
categories = ['general']
paging = True
language_support = True
+use_locale_domain = True
+
+# based on https://en.wikipedia.org/wiki/List_of_Google_domains and tests
+default_hostname = 'www.google.com'
+
+country_to_hostname = {
+ 'BG': 'www.google.bg', # Bulgaria
+ 'CZ': 'www.google.cz', # Czech Republic
+ 'DE': 'www.google.de', # Germany
+ 'DK': 'www.google.dk', # Denmark
+ 'AT': 'www.google.at', # Austria
+ 'CH': 'www.google.ch', # Switzerland
+ 'GR': 'www.google.gr', # Greece
+ 'AU': 'www.google.com.au', # Australia
+ 'CA': 'www.google.ca', # Canada
+ 'GB': 'www.google.co.uk', # United Kingdom
+ 'ID': 'www.google.co.id', # Indonesia
+ 'IE': 'www.google.ie', # Ireland
+ 'IN': 'www.google.co.in', # India
+ 'MY': 'www.google.com.my', # Malaysia
+ 'NZ': 'www.google.co.nz', # New Zealand
+ 'PH': 'www.google.com.ph', # Philippines
+ 'SG': 'www.google.com.sg', # Singapore
+ # 'US': 'www.google.us', # United State, redirect to .com
+ 'ZA': 'www.google.co.za', # South Africa
+ 'AR': 'www.google.com.ar', # Argentina
+ 'CL': 'www.google.cl', # Chile
+ 'ES': 'www.google.es', # Span
+ 'MX': 'www.google.com.mx', # Mexico
+ 'EE': 'www.google.ee', # Estonia
+ 'FI': 'www.google.fi', # Finland
+ 'BE': 'www.google.be', # Belgium
+ 'FR': 'www.google.fr', # France
+ 'IL': 'www.google.co.il', # Israel
+ 'HR': 'www.google.hr', # Croatia
+ 'HU': 'www.google.hu', # Hungary
+ 'IT': 'www.google.it', # Italy
+ 'JP': 'www.google.co.jp', # Japan
+ 'KR': 'www.google.co.kr', # South Korean
+ 'LT': 'www.google.lt', # Lithuania
+ 'LV': 'www.google.lv', # Latvia
+ 'NO': 'www.google.no', # Norway
+ 'NL': 'www.google.nl', # Netherlands
+ 'PL': 'www.google.pl', # Poland
+ 'BR': 'www.google.com.br', # Brazil
+ 'PT': 'www.google.pt', # Portugal
+ 'RO': 'www.google.ro', # Romania
+ 'RU': 'www.google.ru', # Russia
+ 'SK': 'www.google.sk', # Slovakia
+ 'SL': 'www.google.si', # Slovenia (SL -> si)
+ 'SE': 'www.google.se', # Sweden
+ 'TH': 'www.google.co.th', # Thailand
+ 'TR': 'www.google.com.tr', # Turkey
+ 'UA': 'www.google.com.ua', # Ikraine
+ # 'CN': 'www.google.cn', # China, only from china ?
+ 'HK': 'www.google.com.hk', # Hong kong
+ 'TW': 'www.google.com.tw' # Taiwan
+}
# search-url
-google_hostname = 'www.google.com'
search_path = '/search'
+maps_path = '/maps/'
redirect_path = '/url'
images_path = '/images'
-search_url = ('https://' +
- google_hostname +
+search_url = ('https://{hostname}' +
search_path +
'?{query}&start={offset}&gbv=1')
@@ -34,6 +92,7 @@ results_xpath = '//li[@class="g"]'
url_xpath = './/h3/a/@href'
title_xpath = './/h3'
content_xpath = './/span[@class="st"]'
+content_misc_xpath = './/div[@class="f slp"]'
suggestion_xpath = '//p[@class="_Bmc"]'
images_xpath = './/div/a'
@@ -41,6 +100,7 @@ image_url_xpath = './@href'
image_img_src_xpath = './img/@src'
pref_cookie = ''
+nid_cookie = {}
# see https://support.google.com/websearch/answer/873?hl=en
@@ -52,8 +112,16 @@ def get_google_pref_cookie():
return pref_cookie
+def get_google_nid_cookie(google_hostname):
+ global nid_cookie
+ if google_hostname not in nid_cookie:
+ resp = get('https://' + google_hostname)
+ nid_cookie[google_hostname] = resp.cookies.get("NID", None)
+ return nid_cookie[google_hostname]
+
+
# remove google-specific tracking-url
-def parse_url(url_string):
+def parse_url(url_string, google_hostname):
parsed_url = urlparse(url_string)
if (parsed_url.netloc in [google_hostname, '']
and parsed_url.path == redirect_path):
@@ -63,21 +131,45 @@ def parse_url(url_string):
return url_string
+# returns extract_text on the first result selected by the xpath or None
+def extract_text_from_dom(result, xpath):
+ r = result.xpath(xpath)
+ if len(r) > 0:
+ return extract_text(r[0])
+ return None
+
+
# do search-request
def request(query, params):
offset = (params['pageno'] - 1) * 10
if params['language'] == 'all':
language = 'en'
+ country = 'US'
else:
- language = params['language'].replace('_', '-').lower()
+ language_array = params['language'].lower().split('_')
+ if len(language_array) == 2:
+ country = language_array[1]
+ else:
+ country = ' '
+ language = language_array[0] + ',' + language_array[0] + '-' + country
+
+ if use_locale_domain:
+ google_hostname = country_to_hostname.get(country.upper(), default_hostname)
+ else:
+ google_hostname = default_hostname
params['url'] = search_url.format(offset=offset,
- query=urlencode({'q': query}))
+ query=urlencode({'q': query}),
+ hostname=google_hostname)
params['headers']['Accept-Language'] = language
- if language.startswith('en'):
+ params['headers']['Accept'] = 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'
+ if google_hostname == default_hostname:
params['cookies']['PREF'] = get_google_pref_cookie()
+ params['cookies']['NID'] = get_google_nid_cookie(google_hostname)
+
+ params['google_hostname'] = google_hostname
return params
@@ -86,17 +178,30 @@ def request(query, params):
def response(resp):
results = []
+ # detect google sorry
+ resp_url = urlparse(resp.url)
+ if resp_url.netloc == 'sorry.google.com' or resp_url.path == '/sorry/IndexRedirect':
+ raise RuntimeWarning('sorry.google.com')
+
+ # which hostname ?
+ google_hostname = resp.search_params.get('google_hostname')
+ google_url = "https://" + google_hostname
+
+ # convert the text to dom
dom = html.fromstring(resp.text)
# parse results
for result in dom.xpath(results_xpath):
title = extract_text(result.xpath(title_xpath)[0])
try:
- url = parse_url(extract_url(result.xpath(url_xpath), search_url))
- parsed_url = urlparse(url)
+ url = parse_url(extract_url(result.xpath(url_xpath), google_url), google_hostname)
+ parsed_url = urlparse(url, google_hostname)
if (parsed_url.netloc == google_hostname
- and parsed_url.path == search_path):
- # remove the link to google news
+ and (parsed_url.path == search_path
+ or parsed_url.path.startswith(maps_path))):
+ # remove the link to google news and google maps
+ # FIXME : sometimes the URL is https://maps.google.*/maps
+ # no consequence, the result trigger an exception after which is ignored
continue
# images result
@@ -104,16 +209,21 @@ def response(resp):
and parsed_url.path == images_path):
# only thumbnail image provided,
# so skipping image results
- # results = results + parse_images(result)
+ # results = results + parse_images(result, google_hostname)
pass
else:
# normal result
- content = extract_text(result.xpath(content_xpath)[0])
+ content = extract_text_from_dom(result, content_xpath)
+ if content is None:
+ continue
+ content_misc = extract_text_from_dom(result, content_misc_xpath)
+ if content_misc is not None:
+ content = content_misc + "<br />" + content
# append result
results.append({'url': url,
'title': title,
'content': content})
- except:
+ except Exception:
continue
# parse suggestion
@@ -125,10 +235,10 @@ def response(resp):
return results
-def parse_images(result):
+def parse_images(result, google_hostname):
results = []
for image in result.xpath(images_xpath):
- url = parse_url(extract_text(image.xpath(image_url_xpath)[0]))
+ url = parse_url(extract_text(image.xpath(image_url_xpath)[0]), google_hostname)
img_src = extract_text(image.xpath(image_img_src_xpath)[0])
# append result
diff --git a/searx/engines/swisscows.py b/searx/engines/swisscows.py
new file mode 100644
index 000000000..2d31264ca
--- /dev/null
+++ b/searx/engines/swisscows.py
@@ -0,0 +1,108 @@
+"""
+ Swisscows (Web, Images)
+
+ @website https://swisscows.ch
+ @provide-api no
+
+ @using-api no
+ @results HTML (using search portal)
+ @stable no (HTML can change)
+ @parse url, title, content
+"""
+
+from json import loads
+from urllib import urlencode, unquote
+import re
+
+# engine dependent config
+categories = ['general', 'images']
+paging = True
+language_support = True
+
+# search-url
+base_url = 'https://swisscows.ch/'
+search_string = '?{query}&page={page}'
+
+# regex
+regex_json = re.compile('initialData: {"Request":(.|\n)*},\s*environment')
+regex_json_remove_start = re.compile('^initialData:\s*')
+regex_json_remove_end = re.compile(',\s*environment$')
+regex_img_url_remove_start = re.compile('^https?://i\.swisscows\.ch/\?link=')
+
+
+# do search-request
+def request(query, params):
+ if params['language'] == 'all':
+ ui_language = 'browser'
+ region = 'browser'
+ else:
+ region = params['language'].replace('_', '-')
+ ui_language = params['language'].split('_')[0]
+
+ search_path = search_string.format(
+ query=urlencode({'query': query,
+ 'uiLanguage': ui_language,
+ 'region': region}),
+ page=params['pageno'])
+
+ # image search query is something like 'image?{query}&page={page}'
+ if params['category'] == 'images':
+ search_path = 'image' + search_path
+
+ params['url'] = base_url + search_path
+
+ return params
+
+
+# get response from search-request
+def response(resp):
+ results = []
+
+ json_regex = regex_json.search(resp.content)
+
+ # check if results are returned
+ if not json_regex:
+ return []
+
+ json_raw = regex_json_remove_end.sub('', regex_json_remove_start.sub('', json_regex.group()))
+ json = loads(json_raw)
+
+ # parse results
+ for result in json['Results'].get('items', []):
+ result_title = result['Title'].replace(u'\uE000', '').replace(u'\uE001', '')
+
+ # parse image results
+ if result.get('ContentType', '').startswith('image'):
+ img_url = unquote(regex_img_url_remove_start.sub('', result['Url']))
+
+ # append result
+ results.append({'url': result['SourceUrl'],
+ 'title': result['Title'],
+ 'content': '',
+ 'img_src': img_url,
+ 'template': 'images.html'})
+
+ # parse general results
+ else:
+ result_url = result['Url'].replace(u'\uE000', '').replace(u'\uE001', '')
+ result_content = result['Description'].replace(u'\uE000', '').replace(u'\uE001', '')
+
+ # append result
+ results.append({'url': result_url,
+ 'title': result_title,
+ 'content': result_content})
+
+ # parse images
+ for result in json.get('Images', []):
+ # decode image url
+ img_url = unquote(regex_img_url_remove_start.sub('', result['Url']))
+
+ # append result
+ results.append({'url': result['SourceUrl'],
+ 'title': result['Title'],
+ 'content': '',
+ 'img_src': img_url,
+ 'template': 'images.html'})
+
+ # return results
+ return results
diff --git a/searx/settings.yml b/searx/settings.yml
index d35b1378a..ee8fc3d40 100644
--- a/searx/settings.yml
+++ b/searx/settings.yml
@@ -213,6 +213,10 @@ engines:
timeout : 6.0
disabled : True
+ - name : swisscows
+ engine : swisscows
+ shortcut : sw
+
- name : twitter
engine : twitter
shortcut : tw
diff --git a/searx/tests/engines/test_google.py b/searx/tests/engines/test_google.py
index 2a90fc5ec..b706e511d 100644
--- a/searx/tests/engines/test_google.py
+++ b/searx/tests/engines/test_google.py
@@ -8,6 +8,12 @@ from searx.testing import SearxTestCase
class TestGoogleEngine(SearxTestCase):
+ def mock_response(self, text):
+ response = mock.Mock(text=text, url='https://www.google.com/search?q=test&start=0&gbv=1')
+ response.search_params = mock.Mock()
+ response.search_params.get = mock.Mock(return_value='www.google.com')
+ return response
+
def test_request(self):
query = 'test_query'
dicto = defaultdict(dict)
@@ -16,14 +22,17 @@ class TestGoogleEngine(SearxTestCase):
params = google.request(query, dicto)
self.assertIn('url', params)
self.assertIn(query, params['url'])
- self.assertIn('google.com', params['url'])
+ self.assertIn('google.fr', params['url'])
self.assertNotIn('PREF', params['cookies'])
+ self.assertIn('NID', params['cookies'])
self.assertIn('fr', params['headers']['Accept-Language'])
dicto['language'] = 'all'
params = google.request(query, dicto)
+ self.assertIn('google.com', params['url'])
self.assertIn('en', params['headers']['Accept-Language'])
self.assertIn('PREF', params['cookies'])
+ self.assertIn('NID', params['cookies'])
def test_response(self):
self.assertRaises(AttributeError, google.response, None)
@@ -31,7 +40,7 @@ class TestGoogleEngine(SearxTestCase):
self.assertRaises(AttributeError, google.response, '')
self.assertRaises(AttributeError, google.response, '[]')
- response = mock.Mock(text='<html></html>')
+ response = self.mock_response('<html></html>')
self.assertEqual(google.response(response), [])
html = """
@@ -124,7 +133,7 @@ class TestGoogleEngine(SearxTestCase):
</a>
</p>
"""
- response = mock.Mock(text=html)
+ response = self.mock_response(html)
results = google.response(response)
self.assertEqual(type(results), list)
self.assertEqual(len(results), 2)
@@ -137,11 +146,21 @@ class TestGoogleEngine(SearxTestCase):
<li class="b_algo" u="0|5109|4755453613245655|UAGjXgIrPH5yh-o5oNHRx_3Zta87f_QO">
</li>
"""
- response = mock.Mock(text=html)
+ response = self.mock_response(html)
results = google.response(response)
self.assertEqual(type(results), list)
self.assertEqual(len(results), 0)
+ response = mock.Mock(text='<html></html>', url='https://sorry.google.com')
+ response.search_params = mock.Mock()
+ response.search_params.get = mock.Mock(return_value='www.google.com')
+ self.assertRaises(RuntimeWarning, google.response, response)
+
+ response = mock.Mock(text='<html></html>', url='https://www.google.com/sorry/IndexRedirect')
+ response.search_params = mock.Mock()
+ response.search_params.get = mock.Mock(return_value='www.google.com')
+ self.assertRaises(RuntimeWarning, google.response, response)
+
def test_parse_images(self):
html = """
<li>
@@ -154,7 +173,7 @@ class TestGoogleEngine(SearxTestCase):
</li>
"""
dom = lxml.html.fromstring(html)
- results = google.parse_images(dom)
+ results = google.parse_images(dom, 'www.google.com')
self.assertEqual(type(results), list)
self.assertEqual(len(results), 1)
self.assertEqual(results[0]['url'], 'http://this.is.the.url/')
diff --git a/searx/tests/engines/test_swisscows.py b/searx/tests/engines/test_swisscows.py
new file mode 100644
index 000000000..926ba885e
--- /dev/null
+++ b/searx/tests/engines/test_swisscows.py
@@ -0,0 +1,124 @@
+from collections import defaultdict
+import mock
+from searx.engines import swisscows
+from searx.testing import SearxTestCase
+
+
+class TestSwisscowsEngine(SearxTestCase):
+
+ def test_request(self):
+ query = 'test_query'
+ dicto = defaultdict(dict)
+ dicto['pageno'] = 1
+ dicto['language'] = 'de_DE'
+ params = swisscows.request(query, dicto)
+ self.assertTrue('url' in params)
+ self.assertTrue(query in params['url'])
+ self.assertTrue('swisscows.ch' in params['url'])
+ self.assertTrue('uiLanguage=de' in params['url'])
+ self.assertTrue('region=de-DE' in params['url'])
+
+ dicto['language'] = 'all'
+ params = swisscows.request(query, dicto)
+ self.assertTrue('uiLanguage=browser' in params['url'])
+ self.assertTrue('region=browser' in params['url'])
+
+ def test_response(self):
+ self.assertRaises(AttributeError, swisscows.response, None)
+ self.assertRaises(AttributeError, swisscows.response, [])
+ self.assertRaises(AttributeError, swisscows.response, '')
+ self.assertRaises(AttributeError, swisscows.response, '[]')
+
+ response = mock.Mock(content='<html></html>')
+ self.assertEqual(swisscows.response(response), [])
+
+ response = mock.Mock(content='<html></html>')
+ self.assertEqual(swisscows.response(response), [])
+
+ html = u"""
+ <script>
+ App.Dispatcher.dispatch("initialize", {
+ html5history: true,
+ initialData: {"Request":
+ {"Page":1,
+ "ItemsCount":1,
+ "Query":"This should ",
+ "NormalizedQuery":"This should ",
+ "Region":"de-AT",
+ "UILanguage":"de"},
+ "Results":{"items":[
+ {"Title":"\uE000This should\uE001 be the title",
+ "Description":"\uE000This should\uE001 be the content.",
+ "Url":"http://this.should.be.the.link/",
+ "DisplayUrl":"www.\uE000this.should.be.the\uE001.link",
+ "Id":"782ef287-e439-451c-b380-6ebc14ba033d"},
+ {"Title":"Datei:This should1.svg",
+ "Url":"https://i.swisscows.ch/?link=http%3a%2f%2fts2.mm.This/should1.png",
+ "SourceUrl":"http://de.wikipedia.org/wiki/Datei:This should1.svg",
+ "DisplayUrl":"de.wikipedia.org/wiki/Datei:This should1.svg",
+ "Width":950,
+ "Height":534,
+ "FileSize":92100,
+ "ContentType":"image/jpeg",
+ "Thumbnail":{
+ "Url":"https://i.swisscows.ch/?link=http%3a%2f%2fts2.mm.This/should1.png",
+ "ContentType":"image/jpeg",
+ "Width":300,
+ "Height":168,
+ "FileSize":9134},
+ "Id":"6a97a542-8f65-425f-b7f6-1178c3aba7be"
+ }
+ ],"TotalCount":55300,
+ "Query":"This should "
+ },
+ "Images":[{"Title":"Datei:This should.svg",
+ "Url":"https://i.swisscows.ch/?link=http%3a%2f%2fts2.mm.This/should.png",
+ "SourceUrl":"http://de.wikipedia.org/wiki/Datei:This should.svg",
+ "DisplayUrl":"de.wikipedia.org/wiki/Datei:This should.svg",
+ "Width":1280,
+ "Height":677,
+ "FileSize":50053,
+ "ContentType":"image/png",
+ "Thumbnail":{"Url":"https://i.swisscows.ch/?link=http%3a%2f%2fts2.mm.This/should.png",
+ "ContentType":"image/png",
+ "Width":300,
+ "Height":158,
+ "FileSize":8023},
+ "Id":"ae230fd8-a06a-47d6-99d5-e74766d8143a"}]},
+ environment: "production"
+ }).then(function (options) {
+ $('#Search_Form').on('submit', function (e) {
+ if (!Modernizr.history) return;
+ e.preventDefault();
+
+ var $form = $(this),
+ $query = $('#Query'),
+ query = $.trim($query.val()),
+ path = App.Router.makePath($form.attr('action'), null, $form.serializeObject())
+
+ if (query.length) {
+ options.html5history ?
+ ReactRouter.HistoryLocation.push(path) :
+ ReactRouter.RefreshLocation.push(path);
+ }
+ else $('#Query').trigger('blur');
+ });
+
+ });
+ </script>
+ """
+ response = mock.Mock(content=html)
+ results = swisscows.response(response)
+ self.assertEqual(type(results), list)
+ self.assertEqual(len(results), 3)
+ self.assertEqual(results[0]['title'], 'This should be the title')
+ self.assertEqual(results[0]['url'], 'http://this.should.be.the.link/')
+ self.assertEqual(results[0]['content'], 'This should be the content.')
+ self.assertEqual(results[1]['title'], 'Datei:This should1.svg')
+ self.assertEqual(results[1]['url'], 'http://de.wikipedia.org/wiki/Datei:This should1.svg')
+ self.assertEqual(results[1]['img_src'], 'http://ts2.mm.This/should1.png')
+ self.assertEqual(results[1]['template'], 'images.html')
+ self.assertEqual(results[2]['title'], 'Datei:This should.svg')
+ self.assertEqual(results[2]['url'], 'http://de.wikipedia.org/wiki/Datei:This should.svg')
+ self.assertEqual(results[2]['img_src'], 'http://ts2.mm.This/should.png')
+ self.assertEqual(results[2]['template'], 'images.html')
diff --git a/searx/tests/test_engines.py b/searx/tests/test_engines.py
index 5770458f3..90f0ce846 100644
--- a/searx/tests/test_engines.py
+++ b/searx/tests/test_engines.py
@@ -32,6 +32,7 @@ from searx.tests.engines.test_spotify import * # noqa
from searx.tests.engines.test_stackoverflow import * # noqa
from searx.tests.engines.test_startpage import * # noqa
from searx.tests.engines.test_subtitleseeker import * # noqa
+from searx.tests.engines.test_swisscows import * # noqa
from searx.tests.engines.test_twitter import * # noqa
from searx.tests.engines.test_vimeo import * # noqa
from searx.tests.engines.test_www1x import * # noqa