Commit aea4667f authored by Markus Heiser's avatar Markus Heiser

Merge branch 'master' of https://github.com/asciimoo/searx into makefile-doc

parents d1154202 34ad3d6b
...@@ -14,6 +14,7 @@ import random ...@@ -14,6 +14,7 @@ import random
from json import loads from json import loads
from time import time from time import time
from lxml.html import fromstring from lxml.html import fromstring
from searx.poolrequests import get
from searx.url_utils import urlencode from searx.url_utils import urlencode
from searx.utils import eval_xpath from searx.utils import eval_xpath
...@@ -31,13 +32,9 @@ search_string = 'search?{query}'\ ...@@ -31,13 +32,9 @@ search_string = 'search?{query}'\
'&c=main'\ '&c=main'\
'&s={offset}'\ '&s={offset}'\
'&format=json'\ '&format=json'\
'&qh=0'\ '&langcountry={lang}'\
'&qlang={lang}'\
'&ff={safesearch}'\ '&ff={safesearch}'\
'&rxiec={rxieu}'\ '&rand={rxikd}'
'&ulse={ulse}'\
'&rand={rxikd}'\
'&dbez={dbez}'
# specific xpath variables # specific xpath variables
results_xpath = '//response//result' results_xpath = '//response//result'
url_xpath = './/url' url_xpath = './/url'
...@@ -46,9 +43,26 @@ content_xpath = './/sum' ...@@ -46,9 +43,26 @@ content_xpath = './/sum'
supported_languages_url = 'https://gigablast.com/search?&rxikd=1' supported_languages_url = 'https://gigablast.com/search?&rxikd=1'
extra_param = '' # gigablast requires a random extra parameter
# which can be extracted from the source code of the search page
def parse_extra_param(text):
global extra_param
param_lines = [x for x in text.splitlines() if x.startswith('var url=') or x.startswith('url=url+')]
extra_param = ''
for l in param_lines:
extra_param += l.split("'")[1]
extra_param = extra_param.split('&')[-1]
def init(engine_settings=None):
parse_extra_param(get('http://gigablast.com/search?c=main&qlangcountry=en-us&q=south&s=10').text)
# do search-request # do search-request
def request(query, params): def request(query, params):
print("EXTRAPARAM:", extra_param)
offset = (params['pageno'] - 1) * number_of_results offset = (params['pageno'] - 1) * number_of_results
if params['language'] == 'all': if params['language'] == 'all':
...@@ -67,14 +81,11 @@ def request(query, params): ...@@ -67,14 +81,11 @@ def request(query, params):
search_path = search_string.format(query=urlencode({'q': query}), search_path = search_string.format(query=urlencode({'q': query}),
offset=offset, offset=offset,
number_of_results=number_of_results, number_of_results=number_of_results,
rxikd=int(time() * 1000),
rxieu=random.randint(1000000000, 9999999999),
ulse=random.randint(100000000, 999999999),
lang=language, lang=language,
safesearch=safesearch, rxikd=int(time() * 1000),
dbez=random.randint(100000000, 999999999)) safesearch=safesearch)
params['url'] = base_url + search_path params['url'] = base_url + search_path + '&' + extra_param
return params return params
...@@ -84,7 +95,11 @@ def response(resp): ...@@ -84,7 +95,11 @@ def response(resp):
results = [] results = []
# parse results # parse results
response_json = loads(resp.text) try:
response_json = loads(resp.text)
except:
parse_extra_param(resp.text)
raise Exception('extra param expired, please reload')
for result in response_json['results']: for result in response_json['results']:
# append result # append result
......
...@@ -24,7 +24,7 @@ result_base_url = 'https://openstreetmap.org/{osm_type}/{osm_id}' ...@@ -24,7 +24,7 @@ result_base_url = 'https://openstreetmap.org/{osm_type}/{osm_id}'
# do search-request # do search-request
def request(query, params): def request(query, params):
params['url'] = base_url + search_string.format(query=query) params['url'] = base_url + search_string.format(query=query.decode('utf-8'))
return params return params
......
...@@ -50,6 +50,7 @@ def request(query, params): ...@@ -50,6 +50,7 @@ def request(query, params):
language = match_language(params['language'], supported_languages, language_aliases) language = match_language(params['language'], supported_languages, language_aliases)
params['url'] += '&locale=' + language.replace('-', '_').lower() params['url'] += '&locale=' + language.replace('-', '_').lower()
params['headers']['User-Agent'] = 'Mozilla/5.0 (X11; Linux x86_64; rv:69.0) Gecko/20100101 Firefox/69.0'
return params return params
......
...@@ -79,6 +79,9 @@ def response(resp): ...@@ -79,6 +79,9 @@ def response(resp):
# wikipedia article's unique id # wikipedia article's unique id
# first valid id is assumed to be the requested article # first valid id is assumed to be the requested article
if 'pages' not in search_result['query']:
return results
for article_id in search_result['query']['pages']: for article_id in search_result['query']['pages']:
page = search_result['query']['pages'][article_id] page = search_result['query']['pages'][article_id]
if int(article_id) > 0: if int(article_id) > 0:
......
# -*- coding: utf-8 -*-
from collections import defaultdict
import mock
from searx.engines import pubmed
from searx.testing import SearxTestCase
class TestPubmedEngine(SearxTestCase):
def test_request(self):
query = 'test_query'
dicto = defaultdict(dict)
dicto['pageno'] = 1
params = pubmed.request(query, dicto)
self.assertIn('url', params)
self.assertIn('eutils.ncbi.nlm.nih.gov/', params['url'])
self.assertIn('term', params['url'])
def test_response(self):
self.assertRaises(AttributeError, pubmed.response, None)
self.assertRaises(AttributeError, pubmed.response, [])
self.assertRaises(AttributeError, pubmed.response, '')
self.assertRaises(AttributeError, pubmed.response, '[]')
response = mock.Mock(text='<PubmedArticleSet></PubmedArticleSet>')
self.assertEqual(pubmed.response(response), [])
xml_mock = """<eSearchResult><Count>1</Count><RetMax>1</RetMax><RetStart>0</RetStart><IdList>
<Id>1</Id>
</IdList></eSearchResult>
"""
response = mock.Mock(text=xml_mock.encode('utf-8'))
results = pubmed.response(response)
self.assertEqual(type(results), list)
self.assertEqual(len(results), 1)
self.assertEqual(results[0]['content'], 'No abstract is available for this publication.')
This diff is collapsed.
# coding=utf-8
from collections import defaultdict
import mock
from searx.engines import acgsou
from searx.testing import SearxTestCase
class TestAcgsouEngine(SearxTestCase):
def test_request(self):
query = 'test_query'
dic = defaultdict(dict)
dic['pageno'] = 1
params = acgsou.request(query, dic)
self.assertTrue('url' in params)
self.assertTrue(query in params['url'])
self.assertTrue('acgsou.com' in params['url'])
def test_response(self):
resp = mock.Mock(text='<html></html>')
self.assertEqual(acgsou.response(resp), [])
html = u"""
<html>
<table id="listTable" class="list_style table_fixed">
<thead class="tcat">
<tr>
<th axis="string" class="l1 tableHeaderOver">test</th>
<th axis="string" class="l2 tableHeaderOver">test</th>
<th axis="string" class="l3 tableHeaderOver">test</th>
<th axis="size" class="l4 tableHeaderOver">test</th>
<th axis="number" class="l5 tableHeaderOver">test</th>
<th axis="number" class="l6 tableHeaderOver">test</th>
<th axis="number" class="l7 tableHeaderOver">test</th>
<th axis="string" class="l8 tableHeaderOver">test</th>
</tr>
</thead>
<tbody class="tbody" id="data_list">
<tr class="alt1 ">
<td nowrap="nowrap">date</td>
<td><a href="category.html">testcategory テスト</a></td>
<td style="text-align:left;">
<a href="show-torrentid.html" target="_blank">torrentname テスト</a>
</td>
<td>1MB</td>
<td nowrap="nowrap">
<span class="bts_1">
29
</span>
</td>
<td nowrap="nowrap">
<span class="btl_1">
211
</span>
</td>
<td nowrap="nowrap">
<span class="btc_">
168
</span>
</td>
<td><a href="random.html">user</a></td>
</tr>
</tbody>
</table>
</html>
"""
resp = mock.Mock(text=html)
results = acgsou.response(resp)
self.assertEqual(type(results), list)
self.assertEqual(len(results), 1)
r = results[0]
self.assertEqual(r['url'], 'http://www.acgsou.com/show-torrentid.html')
self.assertEqual(r['content'], u'Category: "testcategory テスト".')
self.assertEqual(r['title'], u'torrentname テスト')
self.assertEqual(r['filesize'], 1048576)
from collections import defaultdict
import mock
from searx.engines import archlinux
from searx.testing import SearxTestCase
domains = {
'all': 'https://wiki.archlinux.org',
'de': 'https://wiki.archlinux.de',
'fr': 'https://wiki.archlinux.fr',
'ja': 'https://wiki.archlinuxjp.org',
'ro': 'http://wiki.archlinux.ro',
'tr': 'http://archtr.org/wiki'
}
class TestArchLinuxEngine(SearxTestCase):
def test_request(self):
query = 'test_query'
dic = defaultdict(dict)
dic['pageno'] = 1
dic['language'] = 'en-US'
params = archlinux.request(query, dic)
self.assertTrue('url' in params)
self.assertTrue(query in params['url'])
self.assertTrue('wiki.archlinux.org' in params['url'])
for lang, name in archlinux.main_langs:
dic['language'] = lang
params = archlinux.request(query, dic)
self.assertTrue(name in params['url'])
for lang, domain in domains.items():
dic['language'] = lang
params = archlinux.request(query, dic)
self.assertTrue(domain in params['url'])
def test_response(self):
response = mock.Mock(text='<html></html>',
search_params={'language': 'en_US'})
self.assertEqual(archlinux.response(response), [])
html = """
<ul class="mw-search-results">
<li>
<div class="mw-search-result-heading">
<a href="/index.php/ATI" title="ATI">ATI</a>
</div>
<div class="searchresult">
Lorem ipsum dolor sit amet
</div>
<div class="mw-search-result-data">
30 KB (4,630 words) - 19:04, 17 March 2016</div>
</li>
<li>
<div class="mw-search-result-heading">
<a href="/index.php/Frequently_asked_questions" title="Frequently asked questions">
Frequently asked questions
</a>
</div>
<div class="searchresult">
CPUs with AMDs instruction set "AMD64"
</div>
<div class="mw-search-result-data">
17 KB (2,722 words) - 20:13, 21 March 2016
</div>
</li>
<li>
<div class="mw-search-result-heading">
<a href="/index.php/CPU_frequency_scaling" title="CPU frequency scaling">CPU frequency scaling</a>
</div>
<div class="searchresult">
ondemand for AMD and older Intel CPU
</div>
<div class="mw-search-result-data">
15 KB (2,319 words) - 23:46, 16 March 2016
</div>
</li>
</ul>
"""
expected = [
{
'title': 'ATI',
'url': 'https://wiki.archlinux.org/index.php/ATI'
},
{
'title': 'Frequently asked questions',
'url': 'https://wiki.archlinux.org/index.php/Frequently_asked_questions'
},
{
'title': 'CPU frequency scaling',
'url': 'https://wiki.archlinux.org/index.php/CPU_frequency_scaling'
}
]
response = mock.Mock(text=html)
response.search_params = {
'language': 'en_US'
}
results = archlinux.response(response)
self.assertEqual(type(results), list)
self.assertEqual(len(results), len(expected))
i = 0
for exp in expected:
res = results[i]
i += 1
for key, value in exp.items():
self.assertEqual(res[key], value)
# -*- coding: utf-8 -*-
from collections import defaultdict
import mock
from searx.engines import arxiv
from searx.testing import SearxTestCase
class TestBaseEngine(SearxTestCase):
def test_request(self):
query = 'test_query'.encode('utf-8')
dicto = defaultdict(dict)
dicto['pageno'] = 1
params = arxiv.request(query, dicto)
self.assertIn('url', params)
self.assertIn('export.arxiv.org/api/', params['url'])
def test_response(self):
self.assertRaises(AttributeError, arxiv.response, None)
self.assertRaises(AttributeError, arxiv.response, [])
self.assertRaises(AttributeError, arxiv.response, '')
self.assertRaises(AttributeError, arxiv.response, '[]')
response = mock.Mock(content=b'''<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom"></feed>''')
self.assertEqual(arxiv.response(response), [])
xml_mock = b'''<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title type="html">ArXiv Query: search_query=all:test_query&amp;id_list=&amp;start=0&amp;max_results=1</title>
<id>http://arxiv.org/api/1</id>
<updated>2000-01-21T00:00:00-01:00</updated>
<opensearch:totalResults xmlns:opensearch="http://a9.com/-/spec/opensearch/1.1/">1</opensearch:totalResults>
<opensearch:startIndex xmlns:opensearch="http://a9.com/-/spec/opensearch/1.1/">0</opensearch:startIndex>
<opensearch:itemsPerPage xmlns:opensearch="http://a9.com/-/spec/opensearch/1.1/">1</opensearch:itemsPerPage>
<entry>
<id>http://arxiv.org/1</id>
<updated>2000-01-01T00:00:01Z</updated>
<published>2000-01-01T00:00:01Z</published>
<title>Mathematical proof.</title>
<summary>Mathematical formula.</summary>
<author>
<name>A. B.</name>
</author>
<link href="http://arxiv.org/1" rel="alternate" type="text/html"/>
<link title="pdf" href="http://arxiv.org/1" rel="related" type="application/pdf"/>
<category term="math.QA" scheme="http://arxiv.org/schemas/atom"/>
<category term="1" scheme="http://arxiv.org/schemas/atom"/>
</entry>
</feed>
'''
response = mock.Mock(content=xml_mock)
results = arxiv.response(response)
self.assertEqual(type(results), list)
self.assertEqual(len(results), 1)
self.assertEqual(results[0]['title'], 'Mathematical proof.')
self.assertEqual(results[0]['content'], 'Mathematical formula.')
# -*- coding: utf-8 -*-
from collections import defaultdict
import mock
from searx.engines import base
from searx.testing import SearxTestCase
class TestBaseEngine(SearxTestCase):
def test_request(self):
query = 'test_query'
dicto = defaultdict(dict)
dicto['pageno'] = 1
params = base.request(query, dicto)
self.assertIn('url', params)
self.assertIn('base-search.net', params['url'])
def test_response(self):
self.assertRaises(AttributeError, base.response, None)
self.assertRaises(AttributeError, base.response, [])
self.assertRaises(AttributeError, base.response, '')
self.assertRaises(AttributeError, base.response, '[]')
response = mock.Mock(content=b'<response></response>')
self.assertEqual(base.response(response), [])
xml_mock = b"""<?xml version="1.0"?>
<response>
<lst name="responseHeader">
<int name="status">0</int>
<int name="QTime">1</int>
</lst>
<result name="response" numFound="1" start="0">
<doc>
<date name="dchdate">2000-01-01T01:01:01Z</date>
<str name="dcdocid">1</str>
<str name="dccontinent">cna</str>
<str name="dccountry">us</str>
<str name="dccollection">ftciteseerx</str>
<str name="dcprovider">CiteSeerX</str>
<str name="dctitle">Science and more</str>
<arr name="dccreator">
<str>Someone</str>
</arr>
<arr name="dcperson">
<str>Someone</str>
</arr>
<arr name="dcsubject">
<str>Science and more</str>
</arr>
<str name="dcdescription">Science, and even more.</str>
<arr name="dccontributor">
<str>The neighbour</str>
</arr>
<str name="dcdate">2001</str>
<int name="dcyear">2001</int>
<arr name="dctype">
<str>text</str>
</arr>
<arr name="dctypenorm">
<str>1</str>
</arr>
<arr name="dcformat">
<str>application/pdf</str>
</arr>
<arr name="dccontenttype">
<str>application/pdf</str>
</arr>
<arr name="dcidentifier">
<str>http://example.org/</str>
</arr>
<str name="dclink">http://example.org</str>
<str name="dcsource">http://example.org</str>
<arr name="dclanguage">
<str>en</str>
</arr>
<str name="dcrights">Under the example.org licence</str>
<int name="dcoa">1</int>
<arr name="dclang">
<str>eng</str>
</arr>
</doc>
</result>
</response>"""
response = mock.Mock(content=xml_mock)
results = base.response(response)
self.assertEqual(type(results), list)
self.assertEqual(len(results), 1)
self.assertEqual(results[0]['title'], 'Science and more')
self.assertEqual(results[0]['content'], 'Science, and even more.')
# -*- coding: utf-8 -*-
from collections import defaultdict
import mock
from searx.engines import bing
from searx.testing import SearxTestCase
class TestBingEngine(SearxTestCase):
def test_request(self):
bing.supported_languages = ['en', 'fr', 'zh-CHS', 'zh-CHT', 'pt-PT', 'pt-BR']
query = u'test_query'
dicto = defaultdict(dict)
dicto['pageno'] = 1
dicto['language'] = 'fr-FR'
params = bing.request(query.encode('utf-8'), dicto)
self.assertTrue('url' in params)
self.assertTrue(query in params['url'])
self.assertTrue('language%3AFR' in params['url'])
self.assertTrue('bing.com' in params['url'])
dicto['language'] = 'all'
params = bing.request(query.encode('utf-8'), dicto)
self.assertTrue('language' in params['url'])
def test_response(self):
dicto = defaultdict(dict)
dicto['pageno'] = 1
dicto['language'] = 'fr-FR'
self.assertRaises(AttributeError, bing.response, None)
self.assertRaises(AttributeError, bing.response, [])
self.assertRaises(AttributeError, bing.response, '')
self.assertRaises(AttributeError, bing.response, '[]')
response = mock.Mock(text='<html></html>')
response.search_params = dicto
self.assertEqual(bing.response(response), [])
response = mock.Mock(text='<html></html>')
response.search_params = dicto
self.assertEqual(bing.response(response), [])
html = """
<div>
<div id="b_tween">
<span class="sb_count" data-bm="4">23 900 000 résultats</span>
</div>
<ol id="b_results" role="main">
<div class="sa_cc" u="0|5109|4755453613245655|UAGjXgIrPH5yh-o5oNHRx_3Zta87f_QO">
<div Class="sa_mc">
<div class="sb_tlst">
<h3>
<a href="http://this.should.be.the.link/" h="ID=SERP,5124.1">
<strong>This</strong> should be the title</a>
</h3>
</div>
<div class="sb_meta"><cite><strong>this</strong>.meta.com</cite>
<span class="c_tlbxTrg">
<span class="c_tlbxH" H="BASE:CACHEDPAGEDEFAULT" K="SERP,5125.1">
</span>
</span>
</div>
<p><strong>This</strong> should be the content.</p>
</div>
</div>
</ol>
</div>
"""
response = mock.Mock(text=html)
response.search_params = dicto
results = bing.response(response)
self.assertEqual(type(results), list)
self.assertEqual(len(results), 2)
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]['number_of_results'], 23900000)
html = """
<div>
<div id="b_tween">
<span class="sb_count" data-bm="4">9-18 résultats sur 23 900 000</span>
</div>
<ol id="b_results" role="main">
<li class="b_algo" u="0|5109|4755453613245655|UAGjXgIrPH5yh-o5oNHRx_3Zta87f_QO">
<div Class="sa_mc">
<div class="sb_tlst">
<h2>
<a href="http://this.should.be.the.link/" h="ID=SERP,5124.1">
<strong>This</strong> should be the title</a>
</h2>
</div>
<div class="sb_meta"><cite><strong>this</strong>.meta.com</cite>
<span class="c_tlbxTrg">
<span class="c_tlbxH" H="BASE:CACHEDPAGEDEFAULT" K="SERP,5125.1">
</span>
</span>
</div>
<p><strong>This</strong> should be the content.</p>
</div>
</li>
</ol>
</div>
"""
dicto['pageno'] = 2
response = mock.Mock(text=html)
response.search_params = dicto
results = bing.response(response)
self.assertEqual(type(results), list)
self.assertEqual(len(results), 2)
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]['number_of_results'], 23900000)
html = """
<div>
<div id="b_tween">
<span class="sb_count" data-bm="4">23 900 000 résultats</span>
</div>
<ol id="b_results" role="main">
<li class="b_algo" u="0|5109|4755453613245655|UAGjXgIrPH5yh-o5oNHRx_3Zta87f_QO">
<div Class="sa_mc">
<div class="sb_tlst">
<h2>
<a href="http://this.should.be.the.link/" h="ID=SERP,5124.1">
<strong>This</strong> should be the title</a>
</h2>
</div>
<div class="sb_meta"><cite><strong>this</strong>.meta.com</cite>
<span class="c_tlbxTrg">
<span class="c_tlbxH" H="BASE:CACHEDPAGEDEFAULT" K="SERP,5125.1">
</span>
</span>
</div>
<p><strong>This</strong> should be the content.</p>
</div>
</li>
</ol>
</div>
"""
dicto['pageno'] = 33900000
response = mock.Mock(text=html)
response.search_params = dicto
results = bing.response(response)
self.assertEqual(bing.response(response), [])
def test_fetch_supported_languages(self):
html = """<html></html>"""
response = mock.Mock(text=html)
results = bing._fetch_supported_languages(response)
self.assertEqual(type(results), list)
self.assertEqual(len(results), 0)
html = """
<html>
<body>
<form>
<div id="limit-languages">
<div>
<div><input id="es" value="es"></input></div>
</div>
<div>
<div><input id="pt_BR" value="pt_BR"></input></div>
<div><input id="pt_PT" value="pt_PT"></input></div>
</div>
</div>
</form>
</body>
</html>
"""
response = mock.Mock(text=html)
languages = bing._fetch_supported_languages(response)
self.assertEqual(type(languages), list)
self.assertEqual(len(languages), 3)
self.assertIn('es', languages)
self.assertIn('pt-BR', languages)
self.assertIn('pt-PT', languages)
# -*- coding: utf-8 -*-
from collections import defaultdict
import mock
from searx.engines import bing_images
from searx.testing import SearxTestCase
class TestBingImagesEngine(SearxTestCase):
def test_request(self):
bing_images.supported_languages = ['fr-FR', 'en-US']
bing_images.language_aliases = {}
query = 'test_query'
dicto = defaultdict(dict)
dicto['pageno'] = 1
dicto['language'] = 'fr-FR'
dicto['safesearch'] = 1
dicto['time_range'] = ''
params = bing_images.request(query, dicto)
self.assertTrue('url' in params)
self.assertTrue(query in params['url'])
self.assertTrue('bing.com' in params['url'])
self.assertTrue('SRCHHPGUSR' in params['cookies'])
self.assertTrue('DEMOTE' in params['cookies']['SRCHHPGUSR'])
self.assertTrue('_EDGE_S' in params['cookies'])
self.assertTrue('fr-fr' in params['cookies']['_EDGE_S'])
dicto['language'] = 'fr'
params = bing_images.request(query, dicto)
self.assertTrue('_EDGE_S' in params['cookies'])
self.assertTrue('fr-fr' in params['cookies']['_EDGE_S'])
dicto['language'] = 'all'
params = bing_images.request(query, dicto)
self.assertTrue('_EDGE_S' in params['cookies'])
self.assertTrue('en-us' in params['cookies']['_EDGE_S'])
def test_response(self):
self.assertRaises(AttributeError, bing_images.response, None)
self.assertRaises(AttributeError, bing_images.response, [])
self.assertRaises(AttributeError, bing_images.response, '')
self.assertRaises(AttributeError, bing_images.response, '[]')
response = mock.Mock(text='<html></html>')
self.assertEqual(bing_images.response(response), [])
response = mock.Mock(text='<html></html>')
self.assertEqual(bing_images.response(response), [])
html = """
<div id="mmComponent_images_1">
<ul>
<li>
<div>
<div class="imgpt">
<a m='{"purl":"page_url","murl":"img_url","turl":"thumb_url","t":"Page 1 title"}'>
<img src="" alt="alt text" />
</a>
<div class="img_info">
<span>1 x 1 - jpeg</span>
<a>1.example.org</a>
</div>
</div>
<div></div>
</div>
<div>
<div class="imgpt">
<a m='{"purl":"page_url2","murl":"img_url2","turl":"thumb_url2","t":"Page 2 title"}'>
<img src="" alt="alt text 2" />
</a>
<div class="img_info">
<span>2 x 2 - jpeg</span>
<a>2.example.org</a>
</div>
</div>
</div>
</li>
</ul>
<ul>
<li>
<div>
<div class="imgpt">
<a m='{"purl":"page_url3","murl":"img_url3","turl":"thumb_url3","t":"Page 3 title"}'>
<img src="" alt="alt text 3" />
</a>
<div class="img_info">
<span>3 x 3 - jpeg</span>
<a>3.example.org</a>
</div>
</div>
</div>
</li>
</ul>
</div>
"""
html = html.replace('\r\n', '').replace('\n', '').replace('\r', '')
response = mock.Mock(text=html)
results = bing_images.response(response)
self.assertEqual(type(results), list)
self.assertEqual(len(results), 3)
self.assertEqual(results[0]['title'], 'Page 1 title')
self.assertEqual(results[0]['url'], 'page_url')
self.assertEqual(results[0]['content'], '')
self.assertEqual(results[0]['thumbnail_src'], 'thumb_url')
self.assertEqual(results[0]['img_src'], 'img_url')
self.assertEqual(results[0]['img_format'], '1 x 1 - jpeg')
self.assertEqual(results[0]['source'], '1.example.org')
def test_fetch_supported_languages(self):
html = """
<div>
<div id="region-section-content">
<ul class="b_vList">
<li>
<a href="https://bing...&setmkt=de-DE&s...">Germany</a>
<a href="https://bing...&setmkt=nb-NO&s...">Norway</a>
</li>
</ul>
<ul class="b_vList">
<li>
<a href="https://bing...&setmkt=es-AR&s...">Argentina</a>
</li>
</ul>
</div>
</div>
"""
response = mock.Mock(text=html)
languages = list(bing_images._fetch_supported_languages(response))
self.assertEqual(len(languages), 3)
self.assertIn('de-DE', languages)
self.assertIn('no-NO', languages)
self.assertIn('es-AR', languages)
from collections import defaultdict
import mock
from searx.engines import bing_news
from searx.testing import SearxTestCase
import lxml
class TestBingNewsEngine(SearxTestCase):
def test_request(self):
bing_news.supported_languages = ['en', 'fr']
query = 'test_query'
dicto = defaultdict(dict)
dicto['pageno'] = 1
dicto['language'] = 'fr-FR'
dicto['time_range'] = ''
params = bing_news.request(query, dicto)
self.assertIn('url', params)
self.assertIn(query, params['url'])
self.assertIn('bing.com', params['url'])
self.assertIn('fr', params['url'])
dicto['language'] = 'all'
params = bing_news.request(query, dicto)
self.assertIn('en', params['url'])
def test_no_url_in_request_year_time_range(self):
dicto = defaultdict(dict)
query = 'test_query'
dicto['time_range'] = 'year'
params = bing_news.request(query, dicto)
self.assertEqual({}, params['url'])
def test_response(self):
self.assertRaises(AttributeError, bing_news.response, None)
self.assertRaises(AttributeError, bing_news.response, [])
self.assertRaises(AttributeError, bing_news.response, '')
self.assertRaises(AttributeError, bing_news.response, '[]')
response = mock.Mock(content='<html></html>')
self.assertEqual(bing_news.response(response), [])
response = mock.Mock(content='<html></html>')
self.assertEqual(bing_news.response(response), [])
html = """<?xml version="1.0" encoding="utf-8" ?>
<rss version="2.0" xmlns:News="https://www.bing.com:443/news/search?q=python&amp;setmkt=en-US&amp;first=1&amp;format=RSS">
<channel>
<title>python - Bing News</title>
<link>https://www.bing.com:443/news/search?q=python&amp;setmkt=en-US&amp;first=1&amp;format=RSS</link>
<description>Search results</description>
<image>
<url>http://10.53.64.9/rsslogo.gif</url>
<title>test</title>
<link>https://www.bing.com:443/news/search?q=test&amp;setmkt=en-US&amp;first=1&amp;format=RSS</link>
</image>
<copyright>Copyright</copyright>
<item>
<title>Title</title>
<link>https://www.bing.com/news/apiclick.aspx?ref=FexRss&amp;aid=&amp;tid=c237eccc50bd4758b106a5e3c94fce09&amp;url=http%3a%2f%2furl.of.article%2f&amp;c=xxxxxxxxx&amp;mkt=en-us</link>
<description>Article Content</description>
<pubDate>Tue, 02 Jun 2015 13:37:00 GMT</pubDate>
<News:Source>Infoworld</News:Source>
<News:Image>http://a1.bing4.com/th?id=ON.13371337133713371337133713371337&amp;pid=News</News:Image>
<News:ImageSize>w={0}&amp;h={1}&amp;c=7</News:ImageSize>
<News:ImageKeepOriginalRatio></News:ImageKeepOriginalRatio>
<News:ImageMaxWidth>620</News:ImageMaxWidth>
<News:ImageMaxHeight>413</News:ImageMaxHeight>
</item>
<item>
<title>Another Title</title>
<link>https://www.bing.com/news/apiclick.aspx?ref=FexRss&amp;aid=&amp;tid=c237eccc50bd4758b106a5e3c94fce09&amp;url=http%3a%2f%2fanother.url.of.article%2f&amp;c=xxxxxxxxx&amp;mkt=en-us</link>
<description>Another Article Content</description>
<pubDate>Tue, 02 Jun 2015 13:37:00 GMT</pubDate>
</item>
</channel>
</rss>""" # noqa
response = mock.Mock(content=html.encode('utf-8'))
results = bing_news.response(response)
self.assertEqual(type(results), list)
self.assertEqual(len(results), 2)
self.assertEqual(results[0]['title'], 'Title')
self.assertEqual(results[0]['url'], 'http://url.of.article/')
self.assertEqual(results[0]['content'], 'Article Content')
self.assertEqual(results[0]['img_src'], 'https://www.bing.com/th?id=ON.13371337133713371337133713371337')
self.assertEqual(results[1]['title'], 'Another Title')
self.assertEqual(results[1]['url'], 'http://another.url.of.article/')
self.assertEqual(results[1]['content'], 'Another Article Content')
self.assertNotIn('img_src', results[1])
html = """<?xml version="1.0" encoding="utf-8" ?>
<rss version="2.0" xmlns:News="https://www.bing.com:443/news/search?q=python&amp;setmkt=en-US&amp;first=1&amp;format=RSS">
<channel>
<title>python - Bing News</title>
<link>https://www.bing.com:443/news/search?q=python&amp;setmkt=en-US&amp;first=1&amp;format=RSS</link>
<description>Search results</description>
<image>
<url>http://10.53.64.9/rsslogo.gif</url>
<title>test</title>
<link>https://www.bing.com:443/news/search?q=test&amp;setmkt=en-US&amp;first=1&amp;format=RSS</link>
</image>
<copyright>Copyright</copyright>
<item>
<title>Title</title>
<link>http://another.url.of.article/</link>
<description>Article Content</description>
<pubDate>garbage</pubDate>
<News:Source>Infoworld</News:Source>
<News:Image>http://another.bing.com/image</News:Image>
<News:ImageSize>w={0}&amp;h={1}&amp;c=7</News:ImageSize>
<News:ImageKeepOriginalRatio></News:ImageKeepOriginalRatio>
<News:ImageMaxWidth>620</News:ImageMaxWidth>
<News:ImageMaxHeight>413</News:ImageMaxHeight>
</item>
</channel>
</rss>""" # noqa
response = mock.Mock(content=html.encode('utf-8'))
results = bing_news.response(response)
self.assertEqual(type(results), list)
self.assertEqual(len(results), 1)
self.assertEqual(results[0]['title'], 'Title')
self.assertEqual(results[0]['url'], 'http://another.url.of.article/')
self.assertEqual(results[0]['content'], 'Article Content')
self.assertEqual(results[0]['img_src'], 'http://another.bing.com/image')
html = """<?xml version="1.0" encoding="utf-8" ?>
<rss version="2.0" xmlns:News="https://www.bing.com:443/news/search?q=python&amp;setmkt=en-US&amp;first=1&amp;format=RSS">
<channel>
<title>python - Bing News</title>
<link>https://www.bing.com:443/news/search?q=python&amp;setmkt=en-US&amp;first=1&amp;format=RSS</link>
<description>Search results</description>
<image>
<url>http://10.53.64.9/rsslogo.gif</url>
<title>test</title>
<link>https://www.bing.com:443/news/search?q=test&amp;setmkt=en-US&amp;first=1&amp;format=RSS</link>
</image>
</channel>
</rss>""" # noqa
response = mock.Mock(content=html.encode('utf-8'))
results = bing_news.response(response)
self.assertEqual(type(results), list)
self.assertEqual(len(results), 0)
html = """<?xml version="1.0" encoding="utf-8" ?>gabarge"""
response = mock.Mock(content=html.encode('utf-8'))
self.assertRaises(lxml.etree.XMLSyntaxError, bing_news.response, response)
# -*- coding: utf-8 -*-
from collections import defaultdict
import mock
from searx.engines import bing_videos
from searx.testing import SearxTestCase
class TestBingVideosEngine(SearxTestCase):
def test_request(self):
bing_videos.supported_languages = ['fr-FR', 'en-US']
bing_videos.language_aliases = {}
query = 'test_query'
dicto = defaultdict(dict)
dicto['pageno'] = 1
dicto['language'] = 'fr-FR'
dicto['safesearch'] = 0
dicto['time_range'] = ''
params = bing_videos.request(query, dicto)
self.assertTrue('url' in params)
self.assertTrue(query in params['url'])
self.assertTrue('bing.com' in params['url'])
self.assertTrue('SRCHHPGUSR' in params['cookies'])
self.assertTrue('OFF' in params['cookies']['SRCHHPGUSR'])
self.assertTrue('_EDGE_S' in params['cookies'])
self.assertTrue('fr-fr' in params['cookies']['_EDGE_S'])
dicto['pageno'] = 2
dicto['time_range'] = 'day'
dicto['safesearch'] = 2
params = bing_videos.request(query, dicto)
self.assertTrue('first=29' in params['url'])
self.assertTrue('1440' in params['url'])
self.assertIn('SRCHHPGUSR', params['cookies'])
self.assertTrue('STRICT' in params['cookies']['SRCHHPGUSR'])
def test_response(self):
self.assertRaises(AttributeError, bing_videos.response, None)
self.assertRaises(AttributeError, bing_videos.response, [])
self.assertRaises(AttributeError, bing_videos.response, '')
self.assertRaises(AttributeError, bing_videos.response, '[]')
response = mock.Mock(text='<html></html>')
self.assertEqual(bing_videos.response(response), [])
response = mock.Mock(text='<html></html>')
self.assertEqual(bing_videos.response(response), [])
html = """
<div class="dg_u">
<div>
<a>
<div>
<div>
<div class="mc_vtvc_meta_block">
<div><span>100 views</span><span>1 year ago</span></div><div><span>ExampleTube</span><span>Channel 1<span></div> #noqa
</div>
</div>
<div class="vrhdata" vrhm='{"du":"01:11","murl":"https://www.example.com/watch?v=DEADBEEF","thid":"OVP.BINGTHUMB1","vt":"Title 1"}'></div> # noqa
</div>
</a>
</div>
</div>
"""
response = mock.Mock(text=html)
results = bing_videos.response(response)
self.assertEqual(type(results), list)
self.assertEqual(len(results), 1)
self.assertEqual(results[0]['title'], 'Title 1')
self.assertEqual(results[0]['url'], 'https://www.example.com/watch?v=DEADBEEF')
self.assertEqual(results[0]['content'], '01:11 - 100 views - 1 year ago - ExampleTube - Channel 1')
self.assertEqual(results[0]['thumbnail'], 'https://www.bing.com/th?id=OVP.BINGTHUMB1')
# -*- coding: utf-8 -*-
from collections import defaultdict
import mock
from searx.engines import btdigg
from searx.testing import SearxTestCase
class TestBtdiggEngine(SearxTestCase):
def test_request(self):
query = 'test_query'
dicto = defaultdict(dict)
dicto['pageno'] = 0
params = btdigg.request(query, dicto)
self.assertIn('url', params)
self.assertIn(query, params['url'])
self.assertIn('btdig.com', params['url'])
def test_response(self):
self.assertRaises(AttributeError, btdigg.response, None)
self.assertRaises(AttributeError, btdigg.response, [])
self.assertRaises(AttributeError, btdigg.response, '')
self.assertRaises(AttributeError, btdigg.response, '[]')
response = mock.Mock(text='<html></html>')
self.assertEqual(btdigg.response(response), [])
html = u"""
<div class="one_result" style="display:table-row;background-color:#e8e8e8">
<div style="display:table-cell;color:rgb(0, 0, 0)">
<div style="display:table">
<div style="display:table-row">
<div class="torrent_name" style="display:table-cell">
<a style="color:rgb(0, 0, 204);text-decoration:underline;font-size:150%"
href="http://btdig.com/a72f35b7ee3a10928f02bb799e40ae5db701ed1c/pdf?q=pdf&amp;p=1&amp;order=0"
>3.9GBdeLibrosByHuasoFromHell(3de4)</a>
</div>
</div>
</div>
<div style="display:table">
<div style="display:table-row">
<div style="display:table-cell">
<span class="torrent_files" style="color:#666;padding-left:10px">4217</span> files <span
class="torrent_size" style="color:#666;padding-left:10px">1 GB</span><span
class="torrent_age" style="color:rgb(0, 102, 0);padding-left:10px;margin: 0px 4px"
>found 3 years ago</span>
</div>
</div>
</div>
<div style="display:table;width:100%;padding:10px">
<div style="display:table-row">
<div class="torrent_magnet" style="display:table-cell">
<div class="fa fa-magnet" style="color:#cc0000">
<a href="magnet:?xt=urn:btih:a72f35b7ee3a10928f02bb799e40ae5db701ed1c&amp;dn=3.9GBdeLibrosBy..."
title="Download via magnet-link"> magnet:?xt=urn:btih:a72f35b7ee...</a>
</div>
</div>
<div style="display:table-cell;color:rgb(0, 0, 0);text-align:right">
<span style="color:rgb(136, 136, 136);margin: 0px 0px 0px 4px"></span><span
style="color:rgb(0, 102, 0);margin: 0px 4px">found 3 years ago</span>
</div>
</div>
</div>
<div class="torrent_excerpt" style="display:table;padding:10px;white-space:nowrap">
<div class="fa fa-folder-open" style="padding-left:0em"> 3.9GBdeLibrosByHuasoFromHell(3de4)</div><br/>
<div class="fa fa-folder-open" style="padding-left:1em"> Libros H-Z</div><br/>
<div class="fa fa-folder-open" style="padding-left:2em"> H</div><br/><div class="fa fa-file-archive-o"
style="padding-left:3em"> H.H. Hollis - El truco de la espada-<b
style="color:red; background-color:yellow">pdf</b>.zip</div><span
style="color:#666;padding-left:10px">17 KB</span><br/>
<div class="fa fa-file-archive-o" style="padding-left:3em"> Hagakure - El Libro del Samurai-<b
style="color:red; background-color:yellow">pdf</b>.zip</div><span
style="color:#666;padding-left:10px">95 KB</span><br/>
<div class="fa fa-folder-open" style="padding-left:3em"> Hamsun, Knut (1859-1952)</div><br/>
<div class="fa fa-file-archive-o" style="padding-left:4em"> Hamsun, Knut - Hambre-<b
style="color:red; background-color:yellow">pdf</b>.zip</div><span
style="color:#666;padding-left:10px">786 KB</span><br/>
<div class="fa fa-plus-circle"><a
href="http://btdig.com/a72f35b7ee3a10928f02bb799e40ae5db701ed1c/pdf?q=pdf&amp;p=1&amp;order=0"
> 4214 hidden files<span style="color:#666;padding-left:10px">1 GB</span></a></div>
</div>
</div>
</div>
"""
response = mock.Mock(text=html.encode('utf-8'))
results = btdigg.response(response)
self.assertEqual(type(results), list)
self.assertEqual(len(results), 1)
self.assertEqual(results[0]['title'], '3.9GBdeLibrosByHuasoFromHell(3de4)')
self.assertEqual(results[0]['url'],
'http://btdig.com/a72f35b7ee3a10928f02bb799e40ae5db701ed1c/pdf?q=pdf&p=1&order=0')
self.assertEqual(results[0]['content'],
'3.9GBdeLibrosByHuasoFromHell(3de4) | ' +
'Libros H-Z | ' +
'H H.H. Hollis - El truco de la espada-pdf.zip17 KB | ' +
'Hagakure - El Libro del Samurai-pdf.zip95 KB | ' +
'Hamsun, Knut (1859-1952) | Hamsun, Knut - Hambre-pdf.zip786 KB | ' +
'4214 hidden files1 GB')
self.assertEqual(results[0]['filesize'], 1 * 1024 * 1024 * 1024)
self.assertEqual(results[0]['files'], 4217)
self.assertEqual(results[0]['magnetlink'],
'magnet:?xt=urn:btih:a72f35b7ee3a10928f02bb799e40ae5db701ed1c&dn=3.9GBdeLibrosBy...')
html = """
<div style="display:table-row;background-color:#e8e8e8">
</div>
"""
response = mock.Mock(text=html.encode('utf-8'))
results = btdigg.response(response)
self.assertEqual(type(results), list)
self.assertEqual(len(results), 0)
from collections import defaultdict
from datetime import datetime
import mock
from searx.engines import currency_convert
from searx.testing import SearxTestCase
class TestCurrencyConvertEngine(SearxTestCase):
def test_request(self):
query = b'test_query'
dicto = defaultdict(dict)
dicto['pageno'] = 1
params = currency_convert.request(query, dicto)
self.assertNotIn('url', params)
query = b'convert 10 Pound Sterlings to United States Dollars'
params = currency_convert.request(query, dicto)
self.assertIn('url', params)
self.assertIn('duckduckgo.com', params['url'])
self.assertIn('GBP', params['url'])
self.assertIn('USD', params['url'])
def test_response(self):
dicto = defaultdict(dict)
dicto['amount'] = float(10)
dicto['from'] = "GBP"
dicto['to'] = "USD"
dicto['from_name'] = "pound sterling"
dicto['to_name'] = "United States dollar"
response = mock.Mock(text='a,b,c,d', search_params=dicto)
self.assertEqual(currency_convert.response(response), [])
body = """ddg_spice_currency(
{
"conversion":{
"converted-amount": "0.5"
},
"topConversions":[
{
},
{
}
]
}
);
"""
response = mock.Mock(text=body, search_params=dicto)
results = currency_convert.response(response)
self.assertEqual(type(results), list)
self.assertEqual(len(results), 1)
self.assertEqual(results[0]['answer'], '10.0 GBP = 5.0 USD, 1 GBP (pound sterling)' +
' = 0.5 USD (United States dollar)')
target_url = 'https://duckduckgo.com/js/spice/currency/1/{}/{}'.format(
dicto['from'], dicto['to'])
self.assertEqual(results[0]['url'], target_url)
# -*- coding: utf-8 -*-
from collections import defaultdict
import mock
from searx.engines import dailymotion
from searx.testing import SearxTestCase
class TestDailymotionEngine(SearxTestCase):
def test_request(self):
dailymotion.supported_languages = ['en', 'fr']
query = 'test_query'
dicto = defaultdict(dict)
dicto['pageno'] = 0
dicto['language'] = 'fr-FR'
params = dailymotion.request(query, dicto)
self.assertTrue('url' in params)
self.assertTrue(query in params['url'])
self.assertTrue('dailymotion.com' in params['url'])
self.assertTrue('fr' in params['url'])
dicto['language'] = 'all'
params = dailymotion.request(query, dicto)
self.assertTrue('en' in params['url'])
def test_response(self):
self.assertRaises(AttributeError, dailymotion.response, None)
self.assertRaises(AttributeError, dailymotion.response, [])
self.assertRaises(AttributeError, dailymotion.response, '')
self.assertRaises(AttributeError, dailymotion.response, '[]')
response = mock.Mock(text='{}')
self.assertEqual(dailymotion.response(response), [])
response = mock.Mock(text='{"data": []}')
self.assertEqual(dailymotion.response(response), [])
json = """
{
"page": 1,
"limit": 5,
"explicit": false,
"total": 289487,
"has_more": true,
"list": [
{
"created_time": 1422173451,
"title": "Title",
"description": "Description",
"duration": 81,
"url": "http://www.url",
"thumbnail_360_url": "http://thumbnail",
"id": "x2fit7q"
}
]
}
"""
response = mock.Mock(text=json)
results = dailymotion.response(response)
self.assertEqual(type(results), list)
self.assertEqual(len(results), 1)
self.assertEqual(results[0]['title'], 'Title')
self.assertEqual(results[0]['url'], 'http://www.url')
self.assertEqual(results[0]['content'], 'Description')
self.assertIn('x2fit7q', results[0]['embedded'])
json = r"""
{"toto":[
{"id":200,"name":"Artist Name",
"link":"http:\/\/www.dailymotion.com\/artist\/1217","type":"artist"}
]}
"""
response = mock.Mock(text=json)
results = dailymotion.response(response)
self.assertEqual(type(results), list)
self.assertEqual(len(results), 0)
def test_fetch_supported_languages(self):
json = r"""
{"list":[{"code":"af","name":"Afrikaans","native_name":"Afrikaans",
"localized_name":"Afrikaans","display_name":"Afrikaans"},
{"code":"ar","name":"Arabic","native_name":"\u0627\u0644\u0639\u0631\u0628\u064a\u0629",
"localized_name":"Arabic","display_name":"Arabic"},
{"code":"la","name":"Latin","native_name":null,
"localized_name":"Latin","display_name":"Latin"}
]}
"""
response = mock.Mock(text=json)
languages = dailymotion._fetch_supported_languages(response)
self.assertEqual(type(languages), dict)
self.assertEqual(len(languages), 3)
self.assertIn('af', languages)
self.assertIn('ar', languages)
self.assertIn('la', languages)
self.assertEqual(type(languages['af']), dict)
self.assertEqual(type(languages['ar']), dict)
self.assertEqual(type(languages['la']), dict)
self.assertIn('name', languages['af'])
self.assertIn('name', languages['ar'])
self.assertNotIn('name', languages['la'])
self.assertIn('english_name', languages['af'])
self.assertIn('english_name', languages['ar'])
self.assertIn('english_name', languages['la'])
self.assertEqual(languages['af']['name'], 'Afrikaans')
self.assertEqual(languages['af']['english_name'], 'Afrikaans')
self.assertEqual(languages['ar']['name'], u'العربية')
self.assertEqual(languages['ar']['english_name'], 'Arabic')
self.assertEqual(languages['la']['english_name'], 'Latin')
from collections import defaultdict
import mock
from searx.engines import deezer
from searx.testing import SearxTestCase
class TestDeezerEngine(SearxTestCase):
def test_request(self):
query = 'test_query'
dicto = defaultdict(dict)
dicto['pageno'] = 0
params = deezer.request(query, dicto)
self.assertTrue('url' in params)
self.assertTrue(query in params['url'])
self.assertTrue('deezer.com' in params['url'])
def test_response(self):
self.assertRaises(AttributeError, deezer.response, None)
self.assertRaises(AttributeError, deezer.response, [])
self.assertRaises(AttributeError, deezer.response, '')
self.assertRaises(AttributeError, deezer.response, '[]')
response = mock.Mock(text='{}')
self.assertEqual(deezer.response(response), [])
response = mock.Mock(text='{"data": []}')
self.assertEqual(deezer.response(response), [])
json = r"""
{"data":[
{"id":100, "title":"Title of track",
"link":"https:\/\/www.deezer.com\/track\/1094042","duration":232,
"artist":{"id":200,"name":"Artist Name",
"link":"https:\/\/www.deezer.com\/artist\/1217","type":"artist"},
"album":{"id":118106,"title":"Album Title","type":"album"},"type":"track"}
]}
"""
response = mock.Mock(text=json)
results = deezer.response(response)
self.assertEqual(type(results), list)
self.assertEqual(len(results), 1)
self.assertEqual(results[0]['title'], 'Title of track')
self.assertEqual(results[0]['url'], 'https://www.deezer.com/track/1094042')
self.assertEqual(results[0]['content'], 'Artist Name - Album Title - Title of track')
self.assertTrue('100' in results[0]['embedded'])
json = r"""
{"data":[
{"id":200,"name":"Artist Name",
"link":"https:\/\/www.deezer.com\/artist\/1217","type":"artist"}
]}
"""
response = mock.Mock(text=json)
results = deezer.response(response)
self.assertEqual(type(results), list)
self.assertEqual(len(results), 0)
from collections import defaultdict
import mock
from searx.engines import deviantart
from searx.testing import SearxTestCase
class TestDeviantartEngine(SearxTestCase):
def test_request(self):
dicto = defaultdict(dict)
query = 'test_query'
dicto['pageno'] = 0
dicto['time_range'] = ''
params = deviantart.request(query, dicto)
self.assertTrue('url' in params)
self.assertTrue(query in params['url'])
self.assertTrue('deviantart.com' in params['url'])
def test_no_url_in_request_year_time_range(self):
dicto = defaultdict(dict)
query = 'test_query'
dicto['time_range'] = 'year'
params = deviantart.request(query, dicto)
self.assertEqual({}, params['url'])
from collections import defaultdict
import mock
from searx.engines import digbt
from searx.testing import SearxTestCase
class TestDigBTEngine(SearxTestCase):
def test_request(self):
query = 'test_query'
dicto = defaultdict(dict)
dicto['pageno'] = 0
params = digbt.request(query, dicto)
self.assertIn('url', params)
self.assertIn(query, params['url'])
self.assertIn('digbt.org', params['url'])
def test_response(self):
self.assertRaises(AttributeError, digbt.response, None)
self.assertRaises(AttributeError, digbt.response, [])
self.assertRaises(AttributeError, digbt.response, '')
self.assertRaises(AttributeError, digbt.response, '[]')
response = mock.Mock(text='<html></html>')
self.assertEqual(digbt.response(response), [])
html = """
<table class="table">
<tr><td class="x-item">
<div>
<a title="The Big Bang Theory" class="title" href="/The-Big-Bang-Theory-d2.html">
The Big <span class="highlight">Bang</span> Theory
</a>
<span class="ctime"><span style="color:red;">4 hours ago</span></span>
</div>
<div class="files">
<ul>
<li>The Big Bang Theory 2.9 GB</li>
<li>....</li>
</ul>
</div>
<div class="tail">
Files: 1 Size: 2.9 GB Downloads: 1 Updated: <span style="color:red;">4 hours ago</span>
&nbsp; &nbsp;
<a class="title" href="magnet:?xt=urn:btih:a&amp;dn=The+Big+Bang+Theory">
<span class="glyphicon glyphicon-magnet"></span> magnet-link
</a>
&nbsp; &nbsp;
</div>
</td></tr>
</table>
"""
response = mock.Mock(text=html.encode('utf-8'))
results = digbt.response(response)
self.assertEqual(type(results), list)
self.assertEqual(len(results), 1)
self.assertEqual(results[0]['title'], 'The Big Bang Theory')
self.assertEqual(results[0]['url'], 'https://digbt.org/The-Big-Bang-Theory-d2.html')
self.assertEqual(results[0]['content'], 'The Big Bang Theory 2.9 GB ....')
self.assertEqual(results[0]['filesize'], 3113851289)
self.assertEqual(results[0]['magnetlink'], 'magnet:?xt=urn:btih:a&dn=The+Big+Bang+Theory')
from collections import defaultdict
import mock
from searx.engines import digg
from searx.testing import SearxTestCase
class TestDiggEngine(SearxTestCase):
def test_request(self):
query = 'test_query'
dicto = defaultdict(dict)
dicto['pageno'] = 1
params = digg.request(query, dicto)
self.assertIn('url', params)
self.assertIn(query, params['url'])
self.assertIn('digg.com', params['url'])
# -*- coding: utf-8 -*-
from collections import defaultdict
import mock
from searx.engines import doku
from searx.testing import SearxTestCase
class TestDokuEngine(SearxTestCase):
def test_request(self):
query = 'test_query'
dicto = defaultdict(dict)
params = doku.request(query, dicto)
self.assertIn('url', params)
self.assertIn(query, params['url'])
def test_response(self):
self.assertRaises(AttributeError, doku.response, None)
self.assertRaises(AttributeError, doku.response, [])
self.assertRaises(AttributeError, doku.response, '')
self.assertRaises(AttributeError, doku.response, '[]')
response = mock.Mock(text='<html></html>')
self.assertEqual(doku.response(response), [])
html = u"""
<div class="search_quickresult">
<h3>Pages trouvées :</h3>
<ul class="search_quickhits">
<li> <a href="/xfconf-query" class="wikilink1" title="xfconf-query">xfconf-query</a></li>
</ul>
<div class="clearer"></div>
</div>
"""
response = mock.Mock(text=html)
results = doku.response(response)
expected = [{'content': '', 'title': 'xfconf-query', 'url': 'http://localhost:8090/xfconf-query'}]
self.assertEqual(doku.response(response), expected)
html = u"""
<dl class="search_results">
<dt><a href="/xvnc?s[]=query" class="wikilink1" title="xvnc">xvnc</a>: 40 Occurrences trouvées</dt>
<dd>er = /usr/bin/Xvnc
server_args = -inetd -<strong class="search_hit">query</strong> localhost -geometry 640x480 ... er = /usr/bin/Xvnc
server_args = -inetd -<strong class="search_hit">query</strong> localhost -geometry 800x600 ... er = /usr/bin/Xvnc
server_args = -inetd -<strong class="search_hit">query</strong> localhost -geometry 1024x768 ... er = /usr/bin/Xvnc
server_args = -inetd -<strong class="search_hit">query</strong> localhost -geometry 1280x1024 -depth 8 -Sec</dd>
<dt><a href="/postfix_mysql_tls_sasl_1404?s[]=query"
class="wikilink1"
title="postfix_mysql_tls_sasl_1404">postfix_mysql_tls_sasl_1404</a>: 14 Occurrences trouvées</dt>
<dd>tdepasse
hosts = 127.0.0.1
dbname = postfix
<strong class="search_hit">query</strong> = SELECT goto FROM alias WHERE address='%s' AND a... tdepasse
hosts = 127.0.0.1
dbname = postfix
<strong class="search_hit">query</strong> = SELECT domain FROM domain WHERE domain='%s'
#optional <strong class="search_hit">query</strong> to use when relaying for backup MX
#<strong class="search_hit">query</strong> = SELECT domain FROM domain WHERE domain='%s' and backupmx =</dd>
<dt><a href="/bind9?s[]=query" class="wikilink1" title="bind9">bind9</a>: 12 Occurrences trouvées</dt>
<dd> printcmd
;; Got answer:
;; -&gt;&gt;HEADER&lt;&lt;- opcode: <strong class="search_hit">QUERY</strong>, status: NOERROR, id: 13427
;; flags: qr aa rd ra; <strong class="search_hit">QUERY</strong>: 1, ANSWER: 1, AUTHORITY: 1, ADDITIONAL: 1
[...]
;; <strong class="search_hit">Query</strong> time: 1 msec
;; SERVER: 127.0.0.1#53(127.0.0.1)
;... par la requête (<strong class="search_hit">Query</strong> time) , entre la première et la deuxième requête.</dd>
</dl>
"""
response = mock.Mock(text=html)
results = doku.response(response)
self.assertEqual(type(results), list)
self.assertEqual(len(results), 3)
self.assertEqual(results[0]['title'], 'xvnc')
# FIXME self.assertEqual(results[0]['url'], u'http://this.should.be.the.link/ű')
# FIXME self.assertEqual(results[0]['content'], 'This should be the content.')
# -*- coding: utf-8 -*-
from collections import defaultdict
import mock
from searx.engines import load_engine, duckduckgo
from searx.testing import SearxTestCase
class TestDuckduckgoEngine(SearxTestCase):
def test_request(self):
duckduckgo = load_engine({'engine': 'duckduckgo', 'name': 'duckduckgo'})
query = 'test_query'
dicto = defaultdict(dict)
dicto['pageno'] = 1
dicto['time_range'] = ''
dicto['language'] = 'de-CH'
params = duckduckgo.request(query, dicto)
self.assertIn('url', params)
self.assertIn(query, params['url'])
self.assertIn('duckduckgo.com', params['url'])
self.assertIn('ch-de', params['url'])
self.assertIn('s=0', params['url'])
# when ddg uses non standard codes
dicto['language'] = 'zh-HK'
params = duckduckgo.request(query, dicto)
self.assertIn('hk-tzh', params['url'])
dicto['language'] = 'en-GB'
params = duckduckgo.request(query, dicto)
self.assertIn('uk-en', params['url'])
# no country given
dicto['language'] = 'en'
params = duckduckgo.request(query, dicto)
self.assertIn('us-en', params['url'])
def test_no_url_in_request_year_time_range(self):
dicto = defaultdict(dict)
query = 'test_query'
dicto['time_range'] = 'year'
params = duckduckgo.request(query, dicto)
self.assertEqual({}, params['url'])
def test_response(self):
self.assertRaises(AttributeError, duckduckgo.response, None)
self.assertRaises(AttributeError, duckduckgo.response, [])
self.assertRaises(AttributeError, duckduckgo.response, '')
self.assertRaises(AttributeError, duckduckgo.response, '[]')
response = mock.Mock(text='<html></html>')
self.assertEqual(duckduckgo.response(response), [])
html = u"""
<div class="result results_links results_links_deep web-result result--no-result">
<div class="links_main links_deep result__body">
<h2 class="result__title">
</h2>
<div class="no-results">No results</div>
<div class="result__extras">
</div>
</div>
</div>
"""
response = mock.Mock(text=html)
results = duckduckgo.response(response)
self.assertEqual(duckduckgo.response(response), [])
html = u"""
<div class="result results_links results_links_deep web-result ">
<div class="links_main links_deep result__body">
<h2 class="result__title">
<a rel="nofollow" class="result__a" href="http://this.should.be.the.link/ű">
This <b>is</b> <b>the</b> title
</a>
</h2>
<a class="result__snippet" href="http://this.should.be.the.link/ű">
<b>This</b> should be the content.
</a>
<div class="result__extras">
</div>
</div>
</div>
"""
response = mock.Mock(text=html)
results = duckduckgo.response(response)
self.assertEqual(type(results), list)
self.assertEqual(len(results), 1)
self.assertEqual(results[0]['title'], 'This is the title')
self.assertEqual(results[0]['url'], u'http://this.should.be.the.link/ű')
self.assertEqual(results[0]['content'], 'This should be the content.')
def test_fetch_supported_languages(self):
js = """some code...regions:{
"wt-wt":"All Results","ar-es":"Argentina","au-en":"Australia","at-de":"Austria","be-fr":"Belgium (fr)"
}some more code..."""
response = mock.Mock(text=js)
languages = list(duckduckgo._fetch_supported_languages(response))
self.assertEqual(len(languages), 5)
self.assertIn('wt-WT', languages)
self.assertIn('es-AR', languages)
self.assertIn('en-AU', languages)
self.assertIn('de-AT', languages)
self.assertIn('fr-BE', languages)
from collections import defaultdict
import mock
from searx.engines import duckduckgo_definitions
from searx.testing import SearxTestCase
class TestDDGDefinitionsEngine(SearxTestCase):
def test_result_to_text(self):
url = ''
text = 'Text'
html_result = 'Html'
result = duckduckgo_definitions.result_to_text(url, text, html_result)
self.assertEqual(result, text)
html_result = '<a href="url">Text in link</a>'
result = duckduckgo_definitions.result_to_text(url, text, html_result)
self.assertEqual(result, 'Text in link')
def test_request(self):
duckduckgo_definitions.supported_languages = ['en-US', 'es-ES']
query = 'test_query'
dicto = defaultdict(dict)
dicto['pageno'] = 1
dicto['language'] = 'es'
params = duckduckgo_definitions.request(query, dicto)
self.assertIn('url', params)
self.assertIn(query, params['url'])
self.assertIn('duckduckgo.com', params['url'])
self.assertIn('headers', params)
self.assertIn('Accept-Language', params['headers'])
self.assertIn('es', params['headers']['Accept-Language'])
def test_response(self):
self.assertRaises(AttributeError, duckduckgo_definitions.response, None)
self.assertRaises(AttributeError, duckduckgo_definitions.response, [])
self.assertRaises(AttributeError, duckduckgo_definitions.response, '')
self.assertRaises(AttributeError, duckduckgo_definitions.response, '[]')
response = mock.Mock(text='{}')
self.assertEqual(duckduckgo_definitions.response(response), [])
response = mock.Mock(text='{"data": []}')
self.assertEqual(duckduckgo_definitions.response(response), [])
json = """
{
"DefinitionSource": "definition source",
"Heading": "heading",
"ImageWidth": 0,
"RelatedTopics": [
{
"Result": "Top-level domains",
"Icon": {
"URL": "",
"Height": "",
"Width": ""
},
"FirstURL": "https://first.url",
"Text": "text"
},
{
"Topics": [
{
"Result": "result topic",
"Icon": {
"URL": "",
"Height": "",
"Width": ""
},
"FirstURL": "https://duckduckgo.com/?q=2%2F2",
"Text": "result topic text"
}
],
"Name": "name"
}
],
"Entity": "Entity",
"Type": "A",
"Redirect": "",
"DefinitionURL": "http://definition.url",
"AbstractURL": "https://abstract.url",
"Definition": "this is the definition",
"AbstractSource": "abstract source",
"Infobox": {
"content": [
{
"data_type": "string",
"value": "1999",
"label": "Introduced",
"wiki_order": 0
}
],
"meta": [
{
"data_type": "string",
"value": ".test",
"label": "article_title"
}
]
},
"Image": "image.png",
"ImageIsLogo": 0,
"Abstract": "abstract",
"AbstractText": "abstract text",
"AnswerType": "",
"ImageHeight": 0,
"Results": [{
"Result" : "result title",
"Icon" : {
"URL" : "result url",
"Height" : 16,
"Width" : 16
},
"FirstURL" : "result first url",
"Text" : "result text"
}
],
"Answer": "answer"
}
"""
response = mock.Mock(text=json)
results = duckduckgo_definitions.response(response)
self.assertEqual(type(results), list)
self.assertEqual(len(results), 4)
self.assertEqual(results[0]['answer'], 'answer')
self.assertEqual(results[1]['title'], 'heading')
self.assertEqual(results[1]['url'], 'result first url')
self.assertEqual(results[2]['suggestion'], 'text')
self.assertEqual(results[3]['infobox'], 'heading')
self.assertEqual(results[3]['id'], 'https://definition.url')
self.assertEqual(results[3]['entity'], 'Entity')
self.assertIn('abstract', results[3]['content'])
self.assertIn('this is the definition', results[3]['content'])
self.assertEqual(results[3]['img_src'], 'image.png')
self.assertIn('Introduced', results[3]['attributes'][0]['label'])
self.assertIn('1999', results[3]['attributes'][0]['value'])
self.assertIn({'url': 'https://abstract.url', 'title': 'abstract source'}, results[3]['urls'])
self.assertIn({'url': 'http://definition.url', 'title': 'definition source'}, results[3]['urls'])
self.assertIn({'name': 'name', 'suggestions': ['result topic text']}, results[3]['relatedTopics'])
json = """
{
"DefinitionSource": "definition source",
"Heading": "heading",
"ImageWidth": 0,
"RelatedTopics": [],
"Entity": "Entity",
"Type": "A",
"Redirect": "",
"DefinitionURL": "",
"AbstractURL": "https://abstract.url",
"Definition": "",
"AbstractSource": "abstract source",
"Image": "",
"ImageIsLogo": 0,
"Abstract": "",
"AbstractText": "abstract text",
"AnswerType": "",
"ImageHeight": 0,
"Results": [],
"Answer": ""
}
"""
response = mock.Mock(text=json)
results = duckduckgo_definitions.response(response)
self.assertEqual(type(results), list)
self.assertEqual(len(results), 1)
self.assertEqual(results[0]['url'], 'https://abstract.url')
self.assertEqual(results[0]['title'], 'heading')
self.assertEqual(results[0]['content'], '')
json = """
{
"DefinitionSource": "definition source",
"Heading": "heading",
"ImageWidth": 0,
"RelatedTopics": [
{
"Result": "Top-level domains",
"Icon": {
"URL": "",
"Height": "",
"Width": ""
},
"FirstURL": "https://first.url",
"Text": "heading"
},
{
"Name": "name"
},
{
"Topics": [
{
"Result": "result topic",
"Icon": {
"URL": "",
"Height": "",
"Width": ""
},
"FirstURL": "https://duckduckgo.com/?q=2%2F2",
"Text": "heading"
}
],
"Name": "name"
}
],
"Entity": "Entity",
"Type": "A",
"Redirect": "",
"DefinitionURL": "http://definition.url",
"AbstractURL": "https://abstract.url",
"Definition": "this is the definition",
"AbstractSource": "abstract source",
"Infobox": {
"meta": [
{
"data_type": "string",
"value": ".test",
"label": "article_title"
}
]
},
"Image": "image.png",
"ImageIsLogo": 0,
"Abstract": "abstract",
"AbstractText": "abstract text",
"AnswerType": "",
"ImageHeight": 0,
"Results": [{
"Result" : "result title",
"Icon" : {
"URL" : "result url",
"Height" : 16,
"Width" : 16
},
"Text" : "result text"
}
],
"Answer": ""
}
"""
response = mock.Mock(text=json)
results = duckduckgo_definitions.response(response)
self.assertEqual(type(results), list)
self.assertEqual(len(results), 1)
self.assertEqual(results[0]['infobox'], 'heading')
self.assertEqual(results[0]['id'], 'https://definition.url')
self.assertEqual(results[0]['entity'], 'Entity')
self.assertIn('abstract', results[0]['content'])
self.assertIn('this is the definition', results[0]['content'])
self.assertEqual(results[0]['img_src'], 'image.png')
self.assertIn({'url': 'https://abstract.url', 'title': 'abstract source'}, results[0]['urls'])
self.assertIn({'url': 'http://definition.url', 'title': 'definition source'}, results[0]['urls'])
self.assertIn({'name': 'name', 'suggestions': []}, results[0]['relatedTopics'])
# -*- coding: utf-8 -*-
from collections import defaultdict
import mock
from searx.engines import duckduckgo_images
from searx.testing import SearxTestCase
class TestDuckduckgoImagesEngine(SearxTestCase):
def test_request(self):
duckduckgo_images.supported_languages = ['de-CH', 'en-US']
query = 'test_query'
dicto = defaultdict(dict)
dicto['is_test'] = True
dicto['pageno'] = 1
dicto['safesearch'] = 0
dicto['language'] = 'all'
params = duckduckgo_images.request(query, dicto)
self.assertIn('url', params)
self.assertIn(query, params['url'])
self.assertIn('duckduckgo.com', params['url'])
self.assertIn('s=0', params['url'])
self.assertIn('p=-1', params['url'])
self.assertIn('vqd=12345', params['url'])
# test paging, safe search and language
dicto['pageno'] = 2
dicto['safesearch'] = 2
dicto['language'] = 'de'
params = duckduckgo_images.request(query, dicto)
self.assertIn('url', params)
self.assertIn(query, params['url'])
self.assertIn('s=50', params['url'])
self.assertIn('p=1', params['url'])
self.assertIn('ch-de', params['url'])
def test_response(self):
self.assertRaises(AttributeError, duckduckgo_images.response, None)
self.assertRaises(AttributeError, duckduckgo_images.response, [])
self.assertRaises(AttributeError, duckduckgo_images.response, '')
self.assertRaises(AttributeError, duckduckgo_images.response, '[]')
response = mock.Mock(text='If this error persists, please let us know: ops@duckduckgo.com')
self.assertRaises(Exception, duckduckgo_images.response, response)
json = u"""
{
"query": "test_query",
"results": [
{
"title": "Result 1",
"url": "https://site1.url",
"thumbnail": "https://thumb1.nail",
"image": "https://image1"
},
{
"title": "Result 2",
"url": "https://site2.url",
"thumbnail": "https://thumb2.nail",
"image": "https://image2"
}
]
}
"""
response = mock.Mock(text=json)
results = duckduckgo_images.response(response)
self.assertEqual(len(results), 2)
self.assertEqual(results[0]['title'], 'Result 1')
self.assertEqual(results[0]['url'], 'https://site1.url')
self.assertEqual(results[0]['thumbnail_src'], 'https://thumb1.nail')
self.assertEqual(results[0]['img_src'], 'https://image1')
self.assertEqual(results[1]['title'], 'Result 2')
self.assertEqual(results[1]['url'], 'https://site2.url')
self.assertEqual(results[1]['thumbnail_src'], 'https://thumb2.nail')
self.assertEqual(results[1]['img_src'], 'https://image2')
from collections import defaultdict
import mock
from searx.engines import duden
from searx.testing import SearxTestCase
from datetime import datetime
class TestDudenEngine(SearxTestCase):
def test_request(self):
query = 'Haus'
dic = defaultdict(dict)
data = [
[1, 'https://www.duden.de/suchen/dudenonline/Haus'],
[2, 'https://www.duden.de/suchen/dudenonline/Haus?search_api_fulltext=&page=1']
]
for page_no, exp_res in data:
dic['pageno'] = page_no
params = duden.request(query, dic)
self.assertTrue('url' in params)
self.assertTrue(query in params['url'])
self.assertTrue('duden.de' in params['url'])
self.assertEqual(params['url'], exp_res)
def test_response(self):
resp = mock.Mock(text='<html></html>')
self.assertEqual(duden.response(resp), [])
html = """
<section class="vignette">
<h2"> <a href="/rechtschreibung/Haus">
<strong>This is the title also here</strong>
</a> </h2>
<p>This is the content</p>
</section>
"""
resp = mock.Mock(text=html)
results = duden.response(resp)
self.assertEqual(len(results), 1)
self.assertEqual(type(results), list)
# testing result (dictionary entry)
r = results[0]
self.assertEqual(r['url'], 'https://www.duden.de/rechtschreibung/Haus')
self.assertEqual(r['title'], 'This is the title also here')
self.assertEqual(r['content'], 'This is the content')
from searx.engines import dummy
from searx.testing import SearxTestCase
class TestDummyEngine(SearxTestCase):
def test_request(self):
test_params = [
[1, 2, 3],
['a'],
[],
1
]
for params in test_params:
self.assertEqual(dummy.request(None, params), params)
def test_response(self):
responses = [
None,
[],
True,
dict(),
tuple()
]
for response in responses:
self.assertEqual(dummy.response(response), [])
# -*- coding: utf-8 -*-
from collections import defaultdict
import mock
from searx.engines import faroo
from searx.testing import SearxTestCase
class TestFarooEngine(SearxTestCase):
def test_request(self):
query = 'test_query'
dicto = defaultdict(dict)
dicto['pageno'] = 1
dicto['language'] = 'fr-FR'
dicto['category'] = 'general'
params = faroo.request(query, dicto)
self.assertIn('url', params)
self.assertIn(query, params['url'])
self.assertIn('faroo.com', params['url'])
self.assertIn('en', params['url'])
self.assertIn('web', params['url'])
dicto['language'] = 'all'
params = faroo.request(query, dicto)
self.assertIn('en', params['url'])
dicto['language'] = 'de-DE'
params = faroo.request(query, dicto)
self.assertIn('de', params['url'])
def test_response(self):
self.assertRaises(AttributeError, faroo.response, None)
self.assertRaises(AttributeError, faroo.response, [])
self.assertRaises(AttributeError, faroo.response, '')
self.assertRaises(AttributeError, faroo.response, '[]')
response = mock.Mock(text='{}')
self.assertEqual(faroo.response(response), [])
response = mock.Mock(text='{"data": []}')
self.assertEqual(faroo.response(response), [])
response = mock.Mock(text='{"data": []}', status_code=429)
self.assertRaises(Exception, faroo.response, response)
json = """
{
"results": [
{
"title": "This is the title",
"kwic": "This is the content",
"content": "",
"url": "http://this.is.the.url/",
"iurl": "",
"domain": "css3test.com",
"author": "Jim Dalrymple",
"news": true,
"votes": "10",
"date": 1360622563000,
"related": []
},
{
"title": "This is the title2",
"kwic": "This is the content2",
"content": "",
"url": "http://this.is.the.url2/",
"iurl": "",
"domain": "css3test.com",
"author": "Jim Dalrymple",
"news": false,
"votes": "10",
"related": []
},
{
"title": "This is the title3",
"kwic": "This is the content3",
"content": "",
"url": "http://this.is.the.url3/",
"iurl": "http://upload.wikimedia.org/optimized.jpg",
"domain": "css3test.com",
"author": "Jim Dalrymple",
"news": false,
"votes": "10",
"related": []
}
],
"query": "test",
"suggestions": [],
"count": 100,
"start": 1,
"length": 10,
"time": "15"
}
"""
response = mock.Mock(text=json)
results = faroo.response(response)
self.assertEqual(type(results), list)
self.assertEqual(len(results), 3)
self.assertEqual(results[0]['title'], 'This is the title')
self.assertEqual(results[0]['url'], 'http://this.is.the.url/')
self.assertEqual(results[0]['content'], 'This is the content')
self.assertEqual(results[1]['title'], 'This is the title2')
self.assertEqual(results[1]['url'], 'http://this.is.the.url2/')
self.assertEqual(results[1]['content'], 'This is the content2')
self.assertEqual(results[2]['thumbnail'], 'http://upload.wikimedia.org/optimized.jpg')
json = """
{}
"""
response = mock.Mock(text=json)
results = faroo.response(response)
self.assertEqual(type(results), list)
self.assertEqual(len(results), 0)
import mock
from collections import defaultdict
from searx.engines import fdroid
from searx.testing import SearxTestCase
class TestFdroidEngine(SearxTestCase):
def test_request(self):
query = 'test_query'
dic = defaultdict(dict)
dic['pageno'] = 1
params = fdroid.request(query, dic)
self.assertTrue('url' in params)
self.assertTrue(query in params['url'])
self.assertTrue('search.f-droid.org' in params['url'])
def test_response_empty(self):
resp = mock.Mock(text='<html></html>')
self.assertEqual(fdroid.response(resp), [])
def test_response_oneresult(self):
html = """
<!DOCTYPE html>
<html>
<head>
<title>test</title>
</head>
<body>
<div class="site-wrapper">
<div class="main-content">
<a class="package-header" href="https://example.com/app.url">
<img class="package-icon" src="https://example.com/appexample.logo.png" />
<div class="package-info">
<h4 class="package-name">
App Example 1
</h4>
<div class="package-desc">
<span class="package-summary">Description App Example 1</span>
<span class="package-license">GPL-3.0-only</span>
</div>
</div>
</a>
</div>
</div>
</body>
</html>
"""
resp = mock.Mock(text=html)
results = fdroid.response(resp)
self.assertEqual(type(results), list)
self.assertEqual(len(results), 1)
self.assertEqual(results[0]['url'], 'https://example.com/app.url')
self.assertEqual(results[0]['title'], 'App Example 1')
self.assertEqual(results[0]['content'], 'Description App Example 1 - GPL-3.0-only')
self.assertEqual(results[0]['img_src'], 'https://example.com/appexample.logo.png')
from collections import defaultdict
import mock
from searx.engines import flickr
from searx.testing import SearxTestCase
class TestFlickrEngine(SearxTestCase):
def test_request(self):
query = 'test_query'
dicto = defaultdict(dict)
dicto['pageno'] = 0
params = flickr.request(query, dicto)
self.assertTrue('url' in params)
self.assertTrue(query in params['url'])
self.assertTrue('flickr.com' in params['url'])
def test_response(self):
self.assertRaises(AttributeError, flickr.response, None)
self.assertRaises(AttributeError, flickr.response, [])
self.assertRaises(AttributeError, flickr.response, '')
self.assertRaises(AttributeError, flickr.response, '[]')
response = mock.Mock(text='{}')
self.assertEqual(flickr.response(response), [])
response = mock.Mock(text='{"data": []}')
self.assertEqual(flickr.response(response), [])
json = r"""
{ "photos": { "page": 1, "pages": "41001", "perpage": 100, "total": "4100032",
"photo": [
{ "id": "15751017054", "owner": "66847915@N08",
"secret": "69c22afc40", "server": "7285", "farm": 8,
"title": "Photo title", "ispublic": 1,
"isfriend": 0, "isfamily": 0,
"description": { "_content": "Description" },
"ownername": "Owner",
"url_o": "https:\/\/farm8.staticflickr.com\/7285\/15751017054_9178e0f963_o.jpg",
"height_o": "2100", "width_o": "2653",
"url_n": "https:\/\/farm8.staticflickr.com\/7285\/15751017054_69c22afc40_n.jpg",
"height_n": "253", "width_n": "320",
"url_z": "https:\/\/farm8.staticflickr.com\/7285\/15751017054_69c22afc40_z.jpg",
"height_z": "507", "width_z": "640" }
] }, "stat": "ok" }
"""
response = mock.Mock(text=json)
results = flickr.response(response)
self.assertEqual(type(results), list)
self.assertEqual(len(results), 1)
self.assertEqual(results[0]['title'], 'Photo title')
self.assertEqual(results[0]['url'], 'https://www.flickr.com/photos/66847915@N08/15751017054')
self.assertTrue('o.jpg' in results[0]['img_src'])
self.assertTrue('n.jpg' in results[0]['thumbnail_src'])
self.assertTrue('Owner' in results[0]['author'])
self.assertTrue('Description' in results[0]['content'])
json = r"""
{ "photos": { "page": 1, "pages": "41001", "perpage": 100, "total": "4100032",
"photo": [
{ "id": "15751017054", "owner": "66847915@N08",
"secret": "69c22afc40", "server": "7285", "farm": 8,
"title": "Photo title", "ispublic": 1,
"isfriend": 0, "isfamily": 0,
"description": { "_content": "Description" },
"ownername": "Owner",
"url_z": "https:\/\/farm8.staticflickr.com\/7285\/15751017054_69c22afc40_z.jpg",
"height_z": "507", "width_z": "640" }
] }, "stat": "ok" }
"""
response = mock.Mock(text=json)
results = flickr.response(response)
self.assertEqual(type(results), list)
self.assertEqual(len(results), 1)
self.assertEqual(results[0]['title'], 'Photo title')
self.assertEqual(results[0]['url'], 'https://www.flickr.com/photos/66847915@N08/15751017054')
self.assertTrue('z.jpg' in results[0]['img_src'])
self.assertTrue('z.jpg' in results[0]['thumbnail_src'])
self.assertTrue('Owner' in results[0]['author'])
self.assertTrue('Description' in results[0]['content'])
json = r"""
{ "photos": { "page": 1, "pages": "41001", "perpage": 100, "total": "4100032",
"photo": [
{ "id": "15751017054", "owner": "66847915@N08",
"secret": "69c22afc40", "server": "7285", "farm": 8,
"title": "Photo title", "ispublic": 1,
"isfriend": 0, "isfamily": 0,
"description": { "_content": "Description" },
"ownername": "Owner",
"url_o": "https:\/\/farm8.staticflickr.com\/7285\/15751017054_9178e0f963_o.jpg",
"height_o": "2100", "width_o": "2653" }
] }, "stat": "ok" }
"""
response = mock.Mock(text=json)
results = flickr.response(response)
self.assertEqual(type(results), list)
self.assertEqual(len(results), 1)
self.assertEqual(results[0]['title'], 'Photo title')
self.assertEqual(results[0]['url'], 'https://www.flickr.com/photos/66847915@N08/15751017054')
self.assertTrue('o.jpg' in results[0]['img_src'])
self.assertTrue('o.jpg' in results[0]['thumbnail_src'])
self.assertTrue('Owner' in results[0]['author'])
self.assertTrue('Description' in results[0]['content'])
json = r"""
{ "photos": { "page": 1, "pages": "41001", "perpage": 100, "total": "4100032",
"photo": [
{ "id": "15751017054", "owner": "66847915@N08",
"secret": "69c22afc40", "server": "7285", "farm": 8,
"title": "Photo title", "ispublic": 1,
"isfriend": 0, "isfamily": 0,
"description": { "_content": "Description" },
"ownername": "Owner",
"url_n": "https:\/\/farm8.staticflickr.com\/7285\/15751017054_69c22afc40_n.jpg",
"height_n": "253", "width_n": "320" }
] }, "stat": "ok" }
"""
response = mock.Mock(text=json)
results = flickr.response(response)
self.assertEqual(type(results), list)
self.assertEqual(len(results), 0)
json = """
{ "photos": { "page": 1, "pages": "41001", "perpage": 100, "total": "4100032",
"toto": [] }, "stat": "ok" }
"""
response = mock.Mock(text=json)
results = flickr.response(response)
self.assertEqual(type(results), list)
self.assertEqual(len(results), 0)
json = r"""
{"toto":[
{"id":200,"name":"Artist Name",
"link":"http:\/\/www.flickr.com\/artist\/1217","type":"artist"}
]}
"""
response = mock.Mock(text=json)
results = flickr.response(response)
self.assertEqual(type(results), list)
self.assertEqual(len(results), 0)
This diff is collapsed.
# -*- coding: utf-8 -*-
from collections import defaultdict
import mock
from searx.engines import framalibre
from searx.testing import SearxTestCase
class TestFramalibreEngine(SearxTestCase):
def test_request(self):
query = 'test_query'
dicto = defaultdict(dict)
dicto['pageno'] = 0
params = framalibre.request(query, dicto)
self.assertTrue('url' in params)
self.assertTrue(query in params['url'])
self.assertTrue('framalibre.org' in params['url'])
def test_response(self):
self.assertRaises(AttributeError, framalibre.response, None)
self.assertRaises(AttributeError, framalibre.response, [])
self.assertRaises(AttributeError, framalibre.response, '')
self.assertRaises(AttributeError, framalibre.response, '[]')
response = mock.Mock(text='{}')
self.assertEqual(framalibre.response(response), [])
response = mock.Mock(text='{"data": []}')
self.assertEqual(framalibre.response(response), [])
html = u"""
<div class="nodes-list-row">
<div id="node-431"
class="node node-logiciel-annuaires node-promoted node-teaser node-teaser node-sheet clearfix nodes-list"
about="/content/gogs" typeof="sioc:Item foaf:Document">
<header class="media">
<div class="media-left">
<div class="field field-name-field-logo field-type-image field-label-hidden">
<div class="field-items">
<div class="field-item even">
<a href="/content/gogs">
<img class="media-object img-responsive" typeof="foaf:Image"
src="https://framalibre.org/sites/default/files/styles/teaser_logo/public/leslogos/gogs-lg.png?itok=rrCxKKBy"
width="70" height="70" alt="" />
</a>
</div>
</div>
</div>
</div>
<div class="media-body">
<h3 class="node-title"><a href="/content/gogs">Gogs</a></h3>
<span property="dc:title" content="Gogs" class="rdf-meta element-hidden"></span>
<div class="field field-name-field-annuaires field-type-taxonomy-term-reference field-label-hidden">
<div class="field-items">
<div class="field-item even">
<a href="/annuaires/cloudwebapps"
typeof="skos:Concept" property="rdfs:label skos:prefLabel"
datatype="" class="label label-primary">Cloud/webApps</a>
</div>
</div>
</div>
</div>
</header>
<div class="content">
<div class="field field-name-field-votre-appr-ciation field-type-fivestar field-label-hidden">
<div class="field-items">
<div class="field-item even">
</div>
</div>
</div>
<div class="field field-name-body field-type-text-with-summary field-label-hidden">
<div class="field-items">
<div class="field-item even" property="content:encoded">
<p>Gogs est une interface web basée sur git et une bonne alternative à GitHub.</p>
</div>
</div>
</div>
</div>
<footer>
<a href="/content/gogs" class="read-more btn btn-default btn-sm">Voir la notice</a>
<div class="field field-name-field-lien-officiel field-type-link-field field-label-hidden">
<div class="field-items">
<div class="field-item even">
<a href="https://gogs.io/" target="_blank" title="Voir le site officiel">
<span class="glyphicon glyphicon-globe"></span>
<span class="sr-only">Lien officiel</span>
</a>
</div>
</div>
</div>
</footer>
</div>
</div>
"""
response = mock.Mock(text=html)
results = framalibre.response(response)
self.assertEqual(type(results), list)
self.assertEqual(len(results), 1)
self.assertEqual(results[0]['title'], 'Gogs')
self.assertEqual(results[0]['url'],
'https://framalibre.org/content/gogs')
self.assertEqual(results[0]['content'],
u"Gogs est une interface web basée sur git et une bonne alternative à GitHub.")
# -*- coding: utf-8 -*-
from collections import defaultdict
import mock
from searx.engines import frinkiac
from searx.testing import SearxTestCase
class TestFrinkiacEngine(SearxTestCase):
def test_request(self):
query = 'test_query'
request_dict = defaultdict(dict)
params = frinkiac.request(query, request_dict)
self.assertTrue('url' in params)
def test_response(self):
self.assertRaises(AttributeError, frinkiac.response, None)
self.assertRaises(AttributeError, frinkiac.response, [])
self.assertRaises(AttributeError, frinkiac.response, '')
self.assertRaises(AttributeError, frinkiac.response, '[]')
text = """
[{"Id":770931,
"Episode":"S06E18",
"Timestamp":534616,
"Filename":""},
{"Id":1657080,
"Episode":"S12E14",
"Timestamp":910868,
"Filename":""},
{"Id":1943753,
"Episode":"S14E21",
"Timestamp":773439,
"Filename":""},
{"Id":107835,
"Episode":"S02E03",
"Timestamp":531709,
"Filename":""}]
"""
response = mock.Mock(text=text)
results = frinkiac.response(response)
self.assertEqual(type(results), list)
self.assertEqual(len(results), 4)
self.assertEqual(results[0]['title'], u'S06E18')
self.assertIn('p=caption', results[0]['url'])
self.assertIn('e=S06E18', results[0]['url'])
self.assertIn('t=534616', results[0]['url'])
self.assertEqual(results[0]['thumbnail_src'], 'https://frinkiac.com/img/S06E18/534616/medium.jpg')
self.assertEqual(results[0]['img_src'], 'https://frinkiac.com/img/S06E18/534616.jpg')
from collections import defaultdict
import mock
from datetime import datetime
from searx.engines import genius
from searx.testing import SearxTestCase
class TestGeniusEngine(SearxTestCase):
def test_request(self):
query = 'test_query'
dicto = defaultdict(dict)
dicto['pageno'] = 1
params = genius.request(query, dicto)
self.assertTrue('url' in params)
self.assertTrue(query in params['url'])
self.assertTrue('genius.com' in params['url'])
def test_response(self):
json_empty = """
{
"meta": {
"status": 200
},
"response": {
"sections": [
{
"type": "top_hit",
"hits": []
},
{
"type": "song",
"hits": []
},
{
"type": "lyric",
"hits": []
},
{
"type": "artist",
"hits": []
},
{
"type": "album",
"hits": []
},
{
"type": "tag",
"hits": []
},
{
"type": "video",
"hits": []
},
{
"type": "article",
"hits": []
},
{
"type": "user",
"hits": []
}
]
}
}
"""
resp = mock.Mock(text=json_empty)
self.assertEqual(genius.response(resp), [])
json = """
{
"meta": {
"status": 200
},
"response": {
"sections": [
{
"type": "lyric",
"hits": [
{
"highlights": [
{
"property": "lyrics",
"value": "Sample lyrics",
"snippet": true,
"ranges": []
}
],
"index": "lyric",
"type": "song",
"result": {
"_type": "song",
"annotation_count": 45,
"api_path": "/songs/52916",
"full_title": "J't'emmerde by MC Jean Gab'1",
"header_image_thumbnail_url": "https://images.genius.com/xxx.300x300x1.jpg",
"header_image_url": "https://images.genius.com/ef9f736a86df3c3b1772f3fb7fbdb21c.1000x1000x1.jpg",
"id": 52916,
"instrumental": false,
"lyrics_owner_id": 15586,
"lyrics_state": "complete",
"lyrics_updated_at": 1498744545,
"path": "/Mc-jean-gab1-jtemmerde-lyrics",
"pyongs_count": 4,
"song_art_image_thumbnail_url": "https://images.genius.com/xxx.300x300x1.jpg",
"stats": {
"hot": false,
"unreviewed_annotations": 0,
"pageviews": 62490
},
"title": "J't'emmerde",
"title_with_featured": "J't'emmerde",
"updated_by_human_at": 1498744546,
"url": "https://genius.com/Mc-jean-gab1-jtemmerde-lyrics",
"primary_artist": {
"_type": "artist",
"api_path": "/artists/12691",
"header_image_url": "https://images.genius.com/c7847662a58f8c2b0f02a6e217d60907.960x657x1.jpg",
"id": 12691,
"image_url": "https://s3.amazonaws.com/rapgenius/Mc-jean-gab1.jpg",
"index_character": "m",
"is_meme_verified": false,
"is_verified": false,
"name": "MC Jean Gab'1",
"slug": "Mc-jean-gab1",
"url": "https://genius.com/artists/Mc-jean-gab1"
}
}
}
]
},
{
"type": "artist",
"hits": [
{
"highlights": [],
"index": "artist",
"type": "artist",
"result": {
"_type": "artist",
"api_path": "/artists/191580",
"header_image_url": "https://assets.genius.com/images/default_avatar_300.png?1503090542",
"id": 191580,
"image_url": "https://assets.genius.com/images/default_avatar_300.png?1503090542",
"index_character": "a",
"is_meme_verified": false,
"is_verified": false,
"name": "ASDF Guy",
"slug": "Asdf-guy",
"url": "https://genius.com/artists/Asdf-guy"
}
}
]
},
{
"type": "album",
"hits": [
{
"highlights": [],
"index": "album",
"type": "album",
"result": {
"_type": "album",
"api_path": "/albums/132332",
"cover_art_thumbnail_url": "https://images.genius.com/xxx.300x300x1.jpg",
"cover_art_url": "https://images.genius.com/xxx.600x600x1.jpg",
"full_title": "ASD by A Skylit Drive",
"id": 132332,
"name": "ASD",
"name_with_artist": "ASD (artist: A Skylit Drive)",
"release_date_components": {
"year": 2015,
"month": null,
"day": null
},
"url": "https://genius.com/albums/A-skylit-drive/Asd",
"artist": {
"_type": "artist",
"api_path": "/artists/48712",
"header_image_url": "https://images.genius.com/814c1551293172c56306d0e310c6aa89.620x400x1.jpg",
"id": 48712,
"image_url": "https://images.genius.com/814c1551293172c56306d0e310c6aa89.620x400x1.jpg",
"index_character": "s",
"is_meme_verified": false,
"is_verified": false,
"name": "A Skylit Drive",
"slug": "A-skylit-drive",
"url": "https://genius.com/artists/A-skylit-drive"
}
}
}
]
}
]
}
}
"""
resp = mock.Mock(text=json)
results = genius.response(resp)
self.assertEqual(len(results), 3)
self.assertEqual(type(results), list)
# check lyric parsing
r = results[0]
self.assertEqual(r['url'], 'https://genius.com/Mc-jean-gab1-jtemmerde-lyrics')
self.assertEqual(r['title'], "J't'emmerde by MC Jean Gab'1")
self.assertEqual(r['content'], "Sample lyrics")
self.assertEqual(r['template'], 'videos.html')
self.assertEqual(r['thumbnail'], 'https://images.genius.com/xxx.300x300x1.jpg')
created = datetime.fromtimestamp(1498744545)
self.assertEqual(r['publishedDate'], created)
# check artist parsing
r = results[1]
self.assertEqual(r['url'], 'https://genius.com/artists/Asdf-guy')
self.assertEqual(r['title'], "ASDF Guy")
self.assertEqual(r['content'], None)
self.assertEqual(r['template'], 'videos.html')
self.assertEqual(r['thumbnail'], 'https://assets.genius.com/images/default_avatar_300.png?1503090542')
# check album parsing
r = results[2]
self.assertEqual(r['url'], 'https://genius.com/albums/A-skylit-drive/Asd')
self.assertEqual(r['title'], "ASD by A Skylit Drive")
self.assertEqual(r['content'], "Released: 2015")
self.assertEqual(r['template'], 'videos.html')
self.assertEqual(r['thumbnail'], 'https://images.genius.com/xxx.600x600x1.jpg')
from collections import defaultdict
import mock
from searx.engines import gigablast
from searx.testing import SearxTestCase
class TestGigablastEngine(SearxTestCase):
def test_request(self):
query = 'test_query'
dicto = defaultdict(dict)
dicto['pageno'] = 0
dicto['safesearch'] = 0
dicto['language'] = 'all'
params = gigablast.request(query, dicto)
self.assertTrue('url' in params)
self.assertTrue(query in params['url'])
self.assertTrue('gigablast.com' in params['url'])
self.assertTrue('xx' in params['url'])
dicto['language'] = 'en-US'
params = gigablast.request(query, dicto)
self.assertTrue('en' in params['url'])
self.assertFalse('en-US' in params['url'])
def test_response(self):
self.assertRaises(AttributeError, gigablast.response, None)
self.assertRaises(AttributeError, gigablast.response, [])
self.assertRaises(AttributeError, gigablast.response, '')
self.assertRaises(AttributeError, gigablast.response, '[]')
response = mock.Mock(text='{"results": []}')
self.assertEqual(gigablast.response(response), [])
json = """{"results": [
{
"title":"South by Southwest 2016",
"dmozEntry":{
"dmozCatId":1041152,
"directCatId":1,
"dmozCatStr":"Top: Regional: North America: United States",
"dmozTitle":"South by Southwest (SXSW)",
"dmozSum":"Annual music, film, and interactive conference.",
"dmozAnchor":""
},
"dmozEntry":{
"dmozCatId":763945,
"directCatId":1,
"dmozCatStr":"Top: Regional: North America: United States",
"dmozTitle":"South by Southwest (SXSW)",
"dmozSum":"",
"dmozAnchor":"www.sxsw.com"
},
"dmozEntry":{
"dmozCatId":761446,
"directCatId":1,
"dmozCatStr":"Top: Regional: North America: United States",
"dmozTitle":"South by Southwest (SXSW)",
"dmozSum":"Music, film, and interactive conference and festival.",
"dmozAnchor":""
},
"indirectDmozCatId":1041152,
"indirectDmozCatId":763945,
"indirectDmozCatId":761446,
"contentType":"html",
"sum":"This should be the content.",
"url":"www.sxsw.com",
"hopCount":0,
"size":" 102k",
"sizeInBytes":104306,
"bytesUsedToComputeSummary":70000,
"docId":269411794364,
"docScore":586571136.000000,
"summaryGenTimeMS":12,
"summaryTagdbLookupTimeMS":0,
"summaryTitleRecLoadTimeMS":1,
"site":"www.sxsw.com",
"spidered":1452203608,
"firstIndexedDateUTC":1444167123,
"contentHash32":2170650347,
"language":"English",
"langAbbr":"en"
}
]}
"""
response = mock.Mock(text=json)
results = gigablast.response(response)
self.assertEqual(type(results), list)
self.assertEqual(len(results), 1)
self.assertEqual(results[0]['title'], 'South by Southwest 2016')
self.assertEqual(results[0]['url'], 'www.sxsw.com')
self.assertEqual(results[0]['content'], 'This should be the content.')
def test_fetch_supported_languages(self):
html = """<html></html>"""
response = mock.Mock(text=html)
results = gigablast._fetch_supported_languages(response)
self.assertEqual(type(results), list)
self.assertEqual(len(results), 0)
html = """
<html>
<body>
<span id="menu2">
<a href="/search?&rxikd=1&qlang=xx"></a>
<a href="/search?&rxikd=1&qlang=en"></a>
<a href="/search?&rxikd=1&prepend=gblang%3Aen"></a>
<a href="/search?&rxikd=1&qlang=zh_"></a>
<a href="/search?&rxikd=1&prepend=gblang%3Azh_tw"></a>
</span>
</body>
</html>
"""
response = mock.Mock(text=html)
languages = gigablast._fetch_supported_languages(response)
self.assertEqual(type(languages), list)
self.assertEqual(len(languages), 2)
self.assertIn('en', languages)
self.assertIn('zh-TW', languages)
from collections import defaultdict
import mock
from searx.engines import github
from searx.testing import SearxTestCase
class TestGitHubEngine(SearxTestCase):
def test_request(self):
query = 'test_query'
params = github.request(query, defaultdict(dict))
self.assertTrue('url' in params)
self.assertTrue(query in params['url'])
self.assertTrue('github.com' in params['url'])
self.assertEqual(params['headers']['Accept'], github.accept_header)
def test_response(self):
self.assertRaises(AttributeError, github.response, None)
self.assertRaises(AttributeError, github.response, [])
self.assertRaises(AttributeError, github.response, '')
self.assertRaises(AttributeError, github.response, '[]')
response = mock.Mock(text='{}')
self.assertEqual(github.response(response), [])
response = mock.Mock(text='{"items": []}')
self.assertEqual(github.response(response), [])
json = """
{
"items": [
{
"name": "title",
"html_url": "url",
"description": ""
}
]
}
"""
response = mock.Mock(text=json)
results = github.response(response)
self.assertEqual(type(results), list)
self.assertEqual(len(results), 1)
self.assertEqual(results[0]['title'], 'title')
self.assertEqual(results[0]['url'], 'url')
self.assertEqual(results[0]['content'], '')
json = """
{
"items": [
{
"name": "title",
"html_url": "url",
"description": "desc"
}
]
}
"""
response = mock.Mock(text=json)
results = github.response(response)
self.assertEqual(results[0]['content'], "desc")
# -*- coding: utf-8 -*-
from collections import defaultdict
import mock
import lxml
from searx.engines import google
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&gws_rd=cr')
response.search_params = mock.Mock()
response.search_params.get = mock.Mock(return_value='www.google.com')
return response
def test_request(self):
google.supported_languages = ['en', 'fr', 'zh-CN', 'iw']
google.language_aliases = {'he': 'iw'}
query = 'test_query'
dicto = defaultdict(dict)
dicto['pageno'] = 1
dicto['language'] = 'fr-FR'
dicto['time_range'] = ''
params = google.request(query, dicto)
self.assertIn('url', params)
self.assertIn(query, params['url'])
self.assertIn('google.fr', params['url'])
self.assertIn('fr', params['url'])
self.assertIn('fr', params['headers']['Accept-Language'])
dicto['language'] = 'en-US'
params = google.request(query, dicto)
self.assertIn('google.com', params['url'])
self.assertIn('en', params['url'])
self.assertIn('en', params['headers']['Accept-Language'])
dicto['language'] = 'zh'
params = google.request(query, dicto)
self.assertIn('google.com', params['url'])
self.assertIn('zh-CN', params['url'])
self.assertIn('zh-CN', params['headers']['Accept-Language'])
dicto['language'] = 'he'
params = google.request(query, dicto)
self.assertIn('google.com', params['url'])
self.assertIn('iw', params['url'])
self.assertIn('iw', params['headers']['Accept-Language'])
def test_response(self):
self.assertRaises(AttributeError, google.response, None)
self.assertRaises(AttributeError, google.response, [])
self.assertRaises(AttributeError, google.response, '')
self.assertRaises(AttributeError, google.response, '[]')
response = self.mock_response('<html></html>')
self.assertEqual(google.response(response), [])
html = """
<div class="ZINbbc xpd O9g5cc uUPGi">
<div>
<div class="kCrYT">
<a href="/url?q=http://this.should.be.the.link/">
<div class="BNeawe">
<b>This</b> is <b>the</b> title
</div>
<div class="BNeawe">
http://website
</div>
</a>
</div>
<div class="kCrYT">
<div>
<div class="BNeawe">
<div>
<div class="BNeawe">
This should be the content.
</div>
</div>
</div>
</div>
</div>
</div>
</p>
<div class="ZINbbc xpd O9g5cc uUPGi">
<div>
<div class="kCrYT">
<span>
<div class="BNeawe">
Related searches
</div>
</span>
</div>
<div class="rVLSBd">
<a>
<div>
<div class="BNeawe">
suggestion title
</div>
</div>
</a>
</div>
</div>
</p>
"""
response = self.mock_response(html)
results = google.response(response)
self.assertEqual(type(results), list)
self.assertEqual(len(results), 2)
self.assertEqual(results[0]['title'], 'This is 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]['suggestion'], 'suggestion title')
html = """
<li class="b_algo" u="0|5109|4755453613245655|UAGjXgIrPH5yh-o5oNHRx_3Zta87f_QO">
</li>
"""
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>
<div>
<a href="http://www.google.com/url?q=http://this.is.the.url/">
<img style="margin:3px 0;margin-right:6px;padding:0" height="90"
src="https://this.is.the.image/image.jpg" width="60" align="middle" alt="" border="0">
</a>
</div>
</li>
"""
dom = lxml.html.fromstring(html)
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/')
self.assertEqual(results[0]['title'], '')
self.assertEqual(results[0]['content'], '')
self.assertEqual(results[0]['img_src'], 'https://this.is.the.image/image.jpg')
def test_fetch_supported_languages(self):
html = """<html></html>"""
response = mock.Mock(text=html)
languages = google._fetch_supported_languages(response)
self.assertEqual(type(languages), dict)
self.assertEqual(len(languages), 0)
html = u"""
<html>
<body>
<div id="langSec">
<div>
<input name="lr" data-name="english" value="lang_en" />
<input name="lr" data-name="中文 (简体)" value="lang_zh-CN" />
<input name="lr" data-name="中文 (繁體)" value="lang_zh-TW" />
</div>
</div>
</body>
</html>
"""
response = mock.Mock(text=html)
languages = google._fetch_supported_languages(response)
self.assertEqual(type(languages), dict)
self.assertEqual(len(languages), 3)
self.assertIn('en', languages)
self.assertIn('zh-CN', languages)
self.assertIn('zh-TW', languages)
self.assertEquals(type(languages['en']), dict)
self.assertEquals(type(languages['zh-CN']), dict)
self.assertEquals(type(languages['zh-TW']), dict)
self.assertIn('name', languages['en'])
self.assertIn('name', languages['zh-CN'])
self.assertIn('name', languages['zh-TW'])
self.assertEquals(languages['en']['name'], 'English')
self.assertEquals(languages['zh-CN']['name'], u'中文 (简体)')
self.assertEquals(languages['zh-TW']['name'], u'中文 (繁體)')
from collections import defaultdict
import mock
from searx.engines import google_images
from searx.testing import SearxTestCase
class TestGoogleImagesEngine(SearxTestCase):
def test_request(self):
query = 'test_query'
dicto = defaultdict(dict)
dicto['pageno'] = 1
dicto['safesearch'] = 1
dicto['time_range'] = ''
params = google_images.request(query, dicto)
self.assertIn('url', params)
self.assertIn(query, params['url'])
dicto['safesearch'] = 0
params = google_images.request(query, dicto)
self.assertNotIn('safe', params['url'])
def test_response(self):
self.assertRaises(AttributeError, google_images.response, None)
self.assertRaises(AttributeError, google_images.response, [])
self.assertRaises(AttributeError, google_images.response, '')
self.assertRaises(AttributeError, google_images.response, '[]')
# -*- coding: utf-8 -*-
from collections import defaultdict
import mock
from searx.engines import google_news
from searx.testing import SearxTestCase
class TestGoogleNewsEngine(SearxTestCase):
def test_request(self):
google_news.supported_languages = ['en-US', 'fr-FR']
google_news.language_aliases = {}
query = 'test_query'
dicto = defaultdict(dict)
dicto['pageno'] = 1
dicto['language'] = 'fr-FR'
dicto['time_range'] = 'w'
params = google_news.request(query, dicto)
self.assertIn('url', params)
self.assertIn(query, params['url'])
self.assertIn('fr', params['url'])
dicto['language'] = 'all'
params = google_news.request(query, dicto)
self.assertIn('url', params)
self.assertNotIn('fr', params['url'])
def test_response(self):
self.assertRaises(AttributeError, google_news.response, None)
self.assertRaises(AttributeError, google_news.response, [])
self.assertRaises(AttributeError, google_news.response, '')
self.assertRaises(AttributeError, google_news.response, '[]')
response = mock.Mock(text='{}')
self.assertEqual(google_news.response(response), [])
response = mock.Mock(text='{"data": []}')
self.assertEqual(google_news.response(response), [])
html = u"""
<h2 class="hd">Search Results</h2>
<div data-async-context="query:searx" id="ires">
<div eid="oC2oWcGXCafR6ASkwoCwDA" id="rso">
<div class="_NId">
<!--m-->
<div class="g _cy">
<div class="ts _JGs _JHs _tJs _KGs _jHs">
<div class="_hJs">
<h3 class="r _gJs">
<a class="l lLrAF" href="https://example.com/" onmousedown="return rwt(this,'','','','11','AFQjCNEyehpzD5cJK1KUfXBx9RmsbqqG9g','','0ahUKEwjB58OR54HWAhWnKJoKHSQhAMY4ChCpAggiKAAwAA','','',event)">Example title</a>
</h3>
<div class="slp">
<span class="_OHs _PHs">
Mac &amp; i</span>
<span class="_QGs">
-</span>
<span class="f nsa _QHs">
Mar 21, 2016</span>
</div>
<div class="st">Example description</div>
</div>
</div>
</div>
<div class="g _cy">
<div class="ts _JGs _JHs _oGs _KGs _jHs">
<a class="top _xGs _SHs" href="https://example2.com/" onmousedown="return rwt(this,'','','','12','AFQjCNHObfH7sYmLWI1SC-YhWXKZFRzRjw','','0ahUKEwjB58OR54HWAhWnKJoKHSQhAMY4ChC8iAEIJDAB','','',event)">
<img class="th _RGs" src="https://example2.com/image.jpg" alt="Story image for searx from Golem.de" onload="typeof google==='object'&&google.aft&&google.aft(this)">
</a>
<div class="_hJs">
<h3 class="r _gJs">
<a class="l lLrAF" href="https://example2.com/" onmousedown="return rwt(this,'','','','12','AFQjCNHObfH7sYmLWI1SC-YhWXKZFRzRjw','','0ahUKEwjB58OR54HWAhWnKJoKHSQhAMY4ChCpAgglKAAwAQ','','',event)">Example title 2</a>
</h3>
<div class="slp">
<span class="_OHs _PHs">
Golem.de</span>
<span class="_QGs">
-</span>
<span class="f nsa _QHs">
Oct 4, 2016</span>
</div>
<div class="st">Example description 2</div>
</div>
</div>
</div>
</div>
</div>
</div>
""" # noqa
response = mock.Mock(text=html)
results = google_news.response(response)
self.assertEqual(type(results), list)
self.assertEqual(len(results), 2)
self.assertEqual(results[0]['title'], u'Example title')
self.assertEqual(results[0]['url'], 'https://example.com/')
self.assertEqual(results[0]['content'], 'Example description')
self.assertEqual(results[1]['title'], u'Example title 2')
self.assertEqual(results[1]['url'], 'https://example2.com/')
self.assertEqual(results[1]['content'], 'Example description 2')
self.assertEqual(results[1]['img_src'], 'https://example2.com/image.jpg')
from collections import defaultdict
import mock
from searx.engines import google_videos
from searx.testing import SearxTestCase
class TestGoogleVideosEngine(SearxTestCase):
def test_request(self):
query = 'test_query'
dicto = defaultdict(dict)
dicto['pageno'] = 1
dicto['safesearch'] = 1
dicto['time_range'] = ''
params = google_videos.request(query, dicto)
self.assertIn('url', params)
self.assertIn(query, params['url'])
dicto['safesearch'] = 0
params = google_videos.request(query, dicto)
self.assertNotIn('safe', params['url'])
def test_response(self):
self.assertRaises(AttributeError, google_videos.response, None)
self.assertRaises(AttributeError, google_videos.response, [])
self.assertRaises(AttributeError, google_videos.response, '')
self.assertRaises(AttributeError, google_videos.response, '[]')
html = r"""
<div>
<div>
<div class="g">
<div class="r">
<a href="url_1"><h3>Title 1</h3></a>
</div>
<div class="s">
<div>
<a>
<g-img>
<img id="vidthumb1">
</g-img>
</a>
</div>
</div>
<div>
<span class="st">Content 1</span>
</div>
</div>
<div class="g">
<div class="r">
<a href="url_2"><h3>Title 2</h3></a>
</div>
<div class="s">
<div>
<a>
<g-img>
<img id="vidthumb2">
</g-img>
</a>
</div>
</div>
<div>
<span class="st">Content 2</span>
</div>
</div>
</div>
</div>
<script>function _setImagesSrc(c,d,e){}</script>
"""
response = mock.Mock(text=html)
results = google_videos.response(response)
self.assertEqual(type(results), list)
self.assertEqual(len(results), 2)
self.assertEqual(results[0]['url'], u'url_1')
self.assertEqual(results[0]['title'], u'Title 1')
self.assertEqual(results[0]['content'], u'Content 1')
self.assertEqual(results[1]['url'], u'url_2')
self.assertEqual(results[1]['title'], u'Title 2')
self.assertEqual(results[1]['content'], u'Content 2')
from collections import defaultdict
import mock
from searx.engines import ina
from searx.testing import SearxTestCase
class TestInaEngine(SearxTestCase):
def test_request(self):
query = 'test_query'
dicto = defaultdict(dict)
dicto['pageno'] = 0
params = ina.request(query, dicto)
self.assertTrue('url' in params)
self.assertTrue(query in params['url'])
self.assertTrue('ina.fr' in params['url'])
def test_response(self):
self.assertRaises(AttributeError, ina.response, None)
self.assertRaises(AttributeError, ina.response, [])
self.assertRaises(AttributeError, ina.response, '')
self.assertRaises(AttributeError, ina.response, '[]')
response = mock.Mock(text='{}')
self.assertEqual(ina.response(response), [])
response = mock.Mock(text='{"data": []}')
self.assertEqual(ina.response(response), [])
json = """
{"content":"\\t<div class=\\"container\\">\\n\\t\\n\
<!-- DEBUT CONTENU PRINCIPAL -->\\n<div class=\\"row\\">\\n\
<div class=\\"search-results--list\\"><div class=\\"media\\">\\n\
\\t\\t\\t\\t<a class=\\"media-left media-video premium xiti_click_action\\" \
data-xiti-params=\\"recherche_v4::resultats_conference_de_presse_du_general_de_gaulle::N\\" \
href=\\"\\/video\\/CAF89035682\\/conference-de-presse-du-general-de-gaulle-video.html\\">\\n\
<img src=\\"https:\\/\\/www.ina.fr\\/images_v2\\/140x105\\/CAF89035682.jpeg\\" \
alt=\\"Conf\\u00e9rence de presse du G\\u00e9n\\u00e9ral de Gaulle \\">\\n\
\\t\\t\\t\\t\\t<\\/a>\\n\
\\t\\t\\t\\t\\t<div class=\\"media-body\\">\\n\\t\\t\\t\\t\\t\\t<h3 class=\\"h3--title media-heading\\">\\n\
\\t\\t\\t\\t\\t\\t\\t<a class=\\"xiti_click_action\\" \
data-xiti-params=\\"recherche_v4::resultats_conference_de_presse_du_general_de_gaulle::N\\" \
href=\\"\\/video\\/CAF89035682\\/conference-de-presse-du-general-de-gaulle-video.html\\">\
Conf\\u00e9rence de presse du G\\u00e9n\\u00e9ral de Gaulle <\\/a>\\n\
<\\/h3>\\n\
<div class=\\"media-body__info\\">\\n<span class=\\"broadcast\\">27\\/11\\/1967<\\/span>\\n\
<span class=\\"views\\">29321 vues<\\/span>\\n\
<span class=\\"duration\\">01h 33m 07s<\\/span>\\n\
<\\/div>\\n\
<p class=\\"media-body__summary\\">VERSION INTEGRALE DE LA CONFERENCE DE PRESSE DU GENERAL DE GAULLE . \
- PA le Pr\\u00e9sident DE GAULLE : il ouvre les bras et s'assied. DP journalis...<\\/p>\\n\
<\\/div>\\n<\\/div><!-- \\/.media -->\\n"
}
"""
response = mock.Mock(text=json)
results = ina.response(response)
self.assertEqual(type(results), list)
self.assertEqual(len(results), 1)
self.assertEqual(results[0]['title'], u'Conf\xe9rence de presse du G\xe9n\xe9ral de Gaulle')
self.assertEqual(results[0]['url'],
'https://www.ina.fr/video/CAF89035682/conference-de-presse-du-general-de-gaulle-video.html')
self.assertEqual(results[0]['content'],
u"VERSION INTEGRALE DE LA CONFERENCE DE PRESSE DU GENERAL DE GAULLE ."
u" - PA le Pr\u00e9sident DE GAULLE : il ouvre les bras et s'assied. DP journalis...")
This diff is collapsed.
# -*- coding: utf-8 -*-
from collections import defaultdict
import mock
from searx.engines import mediawiki
from searx.testing import SearxTestCase
class TestMediawikiEngine(SearxTestCase):
def test_request(self):
query = 'test_query'
dicto = defaultdict(dict)
dicto['pageno'] = 1
dicto['language'] = 'fr_FR'
params = mediawiki.request(query, dicto)
self.assertIn('url', params)
self.assertIn(query, params['url'])
self.assertIn('wikipedia.org', params['url'])
self.assertIn('fr', params['url'])
dicto['language'] = 'all'
params = mediawiki.request(query, dicto)
self.assertIn('en', params['url'])
mediawiki.base_url = "http://test.url/"
mediawiki.search_url = mediawiki.base_url +\
'w/api.php?action=query'\
'&list=search'\
'&{query}'\
'&srprop=timestamp'\
'&format=json'\
'&sroffset={offset}'\
'&srlimit={limit}' # noqa
params = mediawiki.request(query, dicto)
self.assertIn('test.url', params['url'])
def test_response(self):
dicto = defaultdict(dict)
dicto['language'] = 'fr'
mediawiki.base_url = "https://{language}.wikipedia.org/"
self.assertRaises(AttributeError, mediawiki.response, None)
self.assertRaises(AttributeError, mediawiki.response, [])
self.assertRaises(AttributeError, mediawiki.response, '')
self.assertRaises(AttributeError, mediawiki.response, '[]')
response = mock.Mock(text='{}', search_params=dicto)
self.assertEqual(mediawiki.response(response), [])
response = mock.Mock(text='{"data": []}', search_params=dicto)
self.assertEqual(mediawiki.response(response), [])
json = """
{
"query-continue": {
"search": {
"sroffset": 1
}
},
"query": {
"searchinfo": {
"totalhits": 29721
},
"search": [
{
"ns": 0,
"title": "This is the title étude",
"timestamp": "2014-12-19T17:42:52Z"
}
]
}
}
"""
response = mock.Mock(text=json, search_params=dicto)
results = mediawiki.response(response)
self.assertEqual(type(results), list)
self.assertEqual(len(results), 1)
self.assertEqual(results[0]['title'], u'This is the title étude')
self.assertIn('fr.wikipedia.org', results[0]['url'])
self.assertIn('This_is_the_title', results[0]['url'])
self.assertIn('%C3%A9tude', results[0]['url'])
self.assertEqual(results[0]['content'], '')
json = """
{
"query-continue": {
"search": {
"sroffset": 1
}
},
"query": {
"searchinfo": {
"totalhits": 29721
},
"search": [
]
}
}
"""
response = mock.Mock(text=json, search_params=dicto)
results = mediawiki.response(response)
self.assertEqual(type(results), list)
self.assertEqual(len(results), 0)
json = """
{
"query-continue": {
"search": {
"sroffset": 1
}
},
"query": {
}
}
"""
response = mock.Mock(text=json, search_params=dicto)
results = mediawiki.response(response)
self.assertEqual(type(results), list)
self.assertEqual(len(results), 0)
json = r"""
{"toto":[
{"id":200,"name":"Artist Name",
"link":"http:\/\/www.mediawiki.com\/artist\/1217","type":"artist"}
]}
"""
response = mock.Mock(text=json, search_params=dicto)
results = mediawiki.response(response)
self.assertEqual(type(results), list)
self.assertEqual(len(results), 0)
from collections import defaultdict
import mock
from searx.engines import mixcloud
from searx.testing import SearxTestCase
class TestMixcloudEngine(SearxTestCase):
def test_request(self):
query = 'test_query'
dicto = defaultdict(dict)
dicto['pageno'] = 0
params = mixcloud.request(query, dicto)
self.assertTrue('url' in params)
self.assertTrue(query in params['url'])
self.assertTrue('mixcloud.com' in params['url'])
def test_response(self):
self.assertRaises(AttributeError, mixcloud.response, None)
self.assertRaises(AttributeError, mixcloud.response, [])
self.assertRaises(AttributeError, mixcloud.response, '')
self.assertRaises(AttributeError, mixcloud.response, '[]')
response = mock.Mock(text='{}')
self.assertEqual(mixcloud.response(response), [])
response = mock.Mock(text='{"data": []}')
self.assertEqual(mixcloud.response(response), [])
json = """
{"data":[
{
"user": {
"url": "http://www.mixcloud.com/user/",
"username": "user",
"name": "User",
"key": "/user/"
},
"key": "/user/this-is-the-url/",
"created_time": "2014-11-14T13:30:02Z",
"audio_length": 3728,
"slug": "this-is-the-url",
"name": "Title of track",
"url": "http://www.mixcloud.com/user/this-is-the-url/",
"updated_time": "2014-11-14T13:14:10Z"
}
]}
"""
response = mock.Mock(text=json)
results = mixcloud.response(response)
self.assertEqual(type(results), list)
self.assertEqual(len(results), 1)
self.assertEqual(results[0]['title'], 'Title of track')
self.assertEqual(results[0]['url'], 'http://www.mixcloud.com/user/this-is-the-url/')
self.assertEqual(results[0]['content'], 'User')
self.assertTrue('http://www.mixcloud.com/user/this-is-the-url/' in results[0]['embedded'])
json = r"""
{"toto":[
{"id":200,"name":"Artist Name",
"link":"http:\/\/www.mixcloud.com\/artist\/1217","type":"artist"}
]}
"""
response = mock.Mock(text=json)
results = mixcloud.response(response)
self.assertEqual(type(results), list)
self.assertEqual(len(results), 0)
from collections import defaultdict
import mock
from searx.engines import nyaa
from searx.testing import SearxTestCase
class TestNyaaEngine(SearxTestCase):
def test_request(self):
query = 'test_query'
dic = defaultdict(dict)
dic['pageno'] = 1
params = nyaa.request(query, dic)
self.assertTrue('url' in params)
self.assertTrue(query in params['url'])
self.assertTrue('nyaa.si' in params['url'])
def test_response(self):
resp = mock.Mock(text='<html></html>')
self.assertEqual(nyaa.response(resp), [])
html = """
<table class="table table-bordered table-hover table-striped torrent-list">
<thead>
<tr>
<th class="hdr-category text-center" style="width:80px;">
<div>Category</div>
</th>
<th class="hdr-name" style="width:auto;">
<div>Name</div>
</th>
<th class="hdr-comments sorting text-center" title="Comments" style="width:50px;">
<a href="/?f=0&amp;c=0_0&amp;q=Death+Parade&amp;s=comments&amp;o=desc"></a>
<i class="fa fa-comments-o"></i>
</th>
<th class="hdr-link text-center" style="width:70px;">
<div>Link</div>
</th>
<th class="hdr-size sorting text-center" style="width:100px;">
<a href="/?f=0&amp;c=0_0&amp;q=Death+Parade&amp;s=size&amp;o=desc"></a>
<div>Size</div>
</th>
<th class="hdr-date sorting_desc text-center" title="In local time" style="width:140px;">
<a href="/?f=0&amp;c=0_0&amp;q=Death+Parade&amp;s=id&amp;o=asc"></a>
<div>Date</div>
</th>
<th class="hdr-seeders sorting text-center" title="Seeders" style="width:50px;">
<a href="/?f=0&amp;c=0_0&amp;q=Death+Parade&amp;s=seeders&amp;o=desc"></a>
<i class="fa fa-arrow-up" aria-hidden="true"></i>
</th>
<th class="hdr-leechers sorting text-center" title="Leechers" style="width:50px;">
<a href="/?f=0&amp;c=0_0&amp;q=Death+Parade&amp;s=leechers&amp;o=desc"></a>
<i class="fa fa-arrow-down" aria-hidden="true"></i>
</th>
<th class="hdr-downloads sorting text-center" title="Completed downloads" style="width:50px;">
<a href="/?f=0&amp;c=0_0&amp;q=Death+Parade&amp;s=downloads&amp;o=desc"></a>
<i class="fa fa-check" aria-hidden="true"></i>
</th>
</tr>
</thead>
<tbody>
<tr class="default">
<td style="padding:0 4px;">
<a href="/?c=1_2" title="Anime - English-translated">
<img src="/static/img/icons/nyaa/1_2.png" alt="Anime - English-translated">
</a>
</td>
<td colspan="2">
<a href="/view/1" title="Sample title 1">Sample title 1</a>
</td>
<td class="text-center" style="white-space: nowrap;">
<a href="/download/1.torrent"><i class="fa fa-fw fa-download"></i></a>
<a href="magnet:?xt=urn:btih:2"><i class="fa fa-fw fa-magnet"></i></a>
</td>
<td class="text-center">723.7 MiB</td>
<td class="text-center" data-timestamp="1503307456" title="1 week 3
days 9 hours 44 minutes 39 seconds ago">2017-08-21 11:24</td>
<td class="text-center" style="color: green;">1</td>
<td class="text-center" style="color: red;">3</td>
<td class="text-center">12</td>
</tr>
<tr class="default">
<td style="padding:0 4px;">
<a href="/?c=1_2" title="Anime - English-translated">
<img src="/static/img/icons/nyaa/1_2.png" alt="Anime - English-translated">
</a>
</td>
<td colspan="2">
<a href="/view/2" title="Sample title 2">Sample title 2</a>
</td>
<td class="text-center" style="white-space: nowrap;">
<a href="magnet:?xt=urn:btih:2"><i class="fa fa-fw fa-magnet"></i></a>
</td>
<td class="text-center">8.2 GiB</td>
<td class="text-center" data-timestamp="1491608400" title="4 months 3
weeks 4 days 19 hours 28 minutes 55 seconds ago">2017-04-08 01:40</td>
<td class="text-center" style="color: green;">10</td>
<td class="text-center" style="color: red;">1</td>
<td class="text-center">206</td>
</tr>
</tbody>
</table>
"""
resp = mock.Mock(text=html)
results = nyaa.response(resp)
self.assertEqual(type(results), list)
self.assertEqual(len(results), 2)
r = results[0]
self.assertTrue(r['url'].find('1') >= 0)
self.assertTrue(r['torrentfile'].find('1.torrent') >= 0)
self.assertTrue(r['content'].find('Anime - English-translated') >= 0)
self.assertTrue(r['content'].find('Downloaded 12 times.') >= 0)
self.assertEqual(r['title'], 'Sample title 1')
self.assertEqual(r['seed'], 1)
self.assertEqual(r['leech'], 3)
self.assertEqual(r['filesize'], 723700000)
r = results[1]
self.assertTrue(r['url'].find('2') >= 0)
self.assertTrue(r['magnetlink'].find('magnet:') >= 0)
# -*- coding: utf-8 -*-
from collections import defaultdict
import mock
from searx.engines import openstreetmap
from searx.testing import SearxTestCase
class TestOpenstreetmapEngine(SearxTestCase):
def test_request(self):
query = 'test_query'
dicto = defaultdict(dict)
dicto['pageno'] = 1
params = openstreetmap.request(query, dicto)
self.assertIn('url', params)
self.assertIn(query, params['url'])
self.assertIn('openstreetmap.org', params['url'])
def test_response(self):
self.assertRaises(AttributeError, openstreetmap.response, None)
self.assertRaises(AttributeError, openstreetmap.response, [])
self.assertRaises(AttributeError, openstreetmap.response, '')
self.assertRaises(AttributeError, openstreetmap.response, '[]')
response = mock.Mock(text='{}')
self.assertEqual(openstreetmap.response(response), [])
response = mock.Mock(text='{"data": []}')
self.assertEqual(openstreetmap.response(response), [])
json = """
[
{
"place_id": "127732055",
"licence": "Data © OpenStreetMap contributors, ODbL 1.0. http://www.openstreetmap.org/copyright",
"osm_type": "relation",
"osm_id": "7444",
"boundingbox": [
"48.8155755",
"48.902156",
"2.224122",
"2.4697602"
],
"lat": "48.8565056",
"lon": "2.3521334",
"display_name": "This is the title",
"class": "place",
"type": "city",
"importance": 0.96893459932191,
"icon": "https://nominatim.openstreetmap.org/images/mapicons/poi_place_city.p.20.png",
"address": {
"city": "Paris",
"county": "Paris",
"state": "Île-de-France",
"country": "France",
"country_code": "fr"
},
"geojson": {
"type": "Polygon",
"coordinates": [
[
[
2.224122,
48.854199
]
]
]
}
}
]
"""
response = mock.Mock(text=json)
results = openstreetmap.response(response)
self.assertEqual(type(results), list)
self.assertEqual(len(results), 1)
self.assertEqual(results[0]['title'], 'This is the title')
self.assertEqual(results[0]['url'], 'https://openstreetmap.org/relation/7444')
self.assertIn('coordinates', results[0]['geojson'])
self.assertEqual(results[0]['geojson']['coordinates'][0][0][0], 2.224122)
self.assertEqual(results[0]['geojson']['coordinates'][0][0][1], 48.854199)
self.assertEqual(results[0]['address'], None)
self.assertIn('48.8155755', results[0]['boundingbox'])
self.assertIn('48.902156', results[0]['boundingbox'])
self.assertIn('2.224122', results[0]['boundingbox'])
self.assertIn('2.4697602', results[0]['boundingbox'])
json = """
[
{
"place_id": "127732055",
"licence": "Data © OpenStreetMap contributors, ODbL 1.0. http://www.openstreetmap.org/copyright",
"osm_type": "relation",
"osm_id": "7444",
"boundingbox": [
"48.8155755",
"48.902156",
"2.224122",
"2.4697602"
],
"lat": "48.8565056",
"lon": "2.3521334",
"display_name": "This is the title",
"class": "tourism",
"type": "city",
"importance": 0.96893459932191,
"icon": "https://nominatim.openstreetmap.org/images/mapicons/poi_place_city.p.20.png",
"address": {
"city": "Paris",
"county": "Paris",
"state": "Île-de-France",
"country": "France",
"country_code": "fr",
"address29": "Address"
},
"geojson": {
"type": "Polygon",
"coordinates": [
[
[
2.224122,
48.854199
]
]
]
}
},
{
"place_id": "127732055",
"licence": "Data © OpenStreetMap contributors, ODbL 1.0. http://www.openstreetmap.org/copyright",
"osm_type": "relation",
"osm_id": "7444",
"boundingbox": [
"48.8155755",
"48.902156",
"2.224122",
"2.4697602"
],
"lat": "48.8565056",
"lon": "2.3521334",
"display_name": "This is the title",
"class": "tourism",
"type": "city",
"importance": 0.96893459932191,
"icon": "https://nominatim.openstreetmap.org/images/mapicons/poi_place_city.p.20.png",
"address": {
"city": "Paris",
"county": "Paris",
"state": "Île-de-France",
"country": "France",
"postcode": 75000,
"country_code": "fr"
},
"geojson": {
"type": "Polygon",
"coordinates": [
[
[
2.224122,
48.854199
]
]
]
}
},
{
"place_id": "127732055",
"licence": "Data © OpenStreetMap contributors, ODbL 1.0. http://www.openstreetmap.org/copyright",
"osm_type": "node",
"osm_id": "7444",
"boundingbox": [
"48.8155755",
"48.902156",
"2.224122",
"2.4697602"
],
"lat": "48.8565056",
"lon": "2.3521334",
"display_name": "This is the title",
"class": "tourism",
"type": "city",
"importance": 0.96893459932191,
"icon": "https://nominatim.openstreetmap.org/images/mapicons/poi_place_city.p.20.png",
"address": {
"city": "Paris",
"county": "Paris",
"state": "Île-de-France",
"country": "France",
"country_code": "fr",
"address29": "Address"
}
}
]
"""
response = mock.Mock(text=json)
results = openstreetmap.response(response)
self.assertEqual(type(results), list)
self.assertEqual(len(results), 3)
self.assertIn('48.8565056', results[2]['geojson']['coordinates'])
self.assertIn('2.3521334', results[2]['geojson']['coordinates'])
import mock
from collections import defaultdict
from searx.engines import pdbe
from searx.testing import SearxTestCase
class TestPdbeEngine(SearxTestCase):
def test_request(self):
query = 'test_query'
dicto = defaultdict(dict)
params = pdbe.request(query, dicto)
self.assertTrue('url' in params)
self.assertTrue('ebi.ac.uk' in params['url'])
self.assertTrue('data' in params)
self.assertTrue('q' in params['data'])
self.assertTrue(query in params['data']['q'])
self.assertTrue('wt' in params['data'])
self.assertTrue('json' in params['data']['wt'])
self.assertTrue('method' in params)
self.assertTrue(params['method'] == 'POST')
def test_response(self):
self.assertRaises(AttributeError, pdbe.response, None)
self.assertRaises(AttributeError, pdbe.response, [])
self.assertRaises(AttributeError, pdbe.response, '')
self.assertRaises(AttributeError, pdbe.response, '[]')
json = """
{
"response": {
"docs": [
{
"citation_title": "X-ray crystal structure of ferric Aplysia limacina myoglobin in different liganded states.",
"citation_year": 1993,
"entry_author_list": [
"Conti E, Moser C, Rizzi M, Mattevi A, Lionetti C, Coda A, Ascenzi P, Brunori M, Bolognesi M"
],
"journal": "J. Mol. Biol.",
"journal_page": "498-508",
"journal_volume": "233",
"pdb_id": "2fal",
"status": "REL",
"title": "X-RAY CRYSTAL STRUCTURE OF FERRIC APLYSIA LIMACINA MYOGLOBIN IN DIFFERENT LIGANDED STATES"
}
],
"numFound": 1,
"start": 0
},
"responseHeader": {
"QTime": 0,
"params": {
"q": "2fal",
"wt": "json"
},
"status": 0
}
}
"""
response = mock.Mock(text=json)
results = pdbe.response(response)
self.assertEqual(type(results), list)
self.assertEqual(len(results), 1)
self.assertEqual(results[0]['title'],
'X-RAY CRYSTAL STRUCTURE OF FERRIC APLYSIA LIMACINA MYOGLOBIN IN DIFFERENT LIGANDED STATES')
self.assertEqual(results[0]['url'], pdbe.pdbe_entry_url.format(pdb_id='2fal'))
self.assertEqual(results[0]['img_src'], pdbe.pdbe_preview_url.format(pdb_id='2fal'))
self.assertTrue('Conti E' in results[0]['content'])
self.assertTrue('X-ray crystal structure of ferric Aplysia limacina myoglobin in different liganded states.' in
results[0]['content'])
self.assertTrue('1993' in results[0]['content'])
# Testing proper handling of PDB entries marked as obsolete
json = """
{
"response": {
"docs": [
{
"citation_title": "Obsolete entry test",
"citation_year": 2016,
"entry_author_list": ["Doe J"],
"journal": "J. Obs.",
"journal_page": "1-2",
"journal_volume": "1",
"pdb_id": "xxxx",
"status": "OBS",
"title": "OBSOLETE ENTRY TEST",
"superseded_by": "yyyy"
}
],
"numFound": 1,
"start": 0
},
"responseHeader": {
"QTime": 0,
"params": {
"q": "xxxx",
"wt": "json"
},
"status": 0
}
}
"""
response = mock.Mock(text=json)
results = pdbe.response(response)
self.assertEqual(type(results), list)
self.assertEqual(len(results), 1)
self.assertEqual(results[0]['title'], 'OBSOLETE ENTRY TEST (OBSOLETE)')
self.assertTrue(results[0]['content'].startswith('This entry has been superseded by'))
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
from collections import defaultdict
import mock
from searx.engines import reddit
from searx.testing import SearxTestCase
from datetime import datetime
class TestRedditEngine(SearxTestCase):
def test_request(self):
query = 'test_query'
dic = defaultdict(dict)
params = reddit.request(query, dic)
self.assertTrue('url' in params)
self.assertTrue(query in params['url'])
self.assertTrue('reddit.com' in params['url'])
def test_response(self):
resp = mock.Mock(text='{}')
self.assertEqual(reddit.response(resp), [])
json = """
{
"kind": "Listing",
"data": {
"children": [{
"data": {
"url": "http://google2.com/",
"permalink": "http://google.com/",
"title": "Title number one",
"selftext": "Sample",
"created_utc": 1401219957.0,
"thumbnail": "http://image.com/picture.jpg"
}
}, {
"data": {
"url": "https://reddit2.com/",
"permalink": "https://reddit.com/",
"title": "Title number two",
"selftext": "Dominus vobiscum",
"created_utc": 1438792533.0,
"thumbnail": "self"
}
}]
}
}
"""
resp = mock.Mock(text=json)
results = reddit.response(resp)
self.assertEqual(len(results), 2)
self.assertEqual(type(results), list)
# testing first result (picture)
r = results[0]
self.assertEqual(r['url'], 'http://google.com/')
self.assertEqual(r['title'], 'Title number one')
self.assertEqual(r['template'], 'images.html')
self.assertEqual(r['img_src'], 'http://google2.com/')
self.assertEqual(r['thumbnail_src'], 'http://image.com/picture.jpg')
# testing second result (self-post)
r = results[1]
self.assertEqual(r['url'], 'https://reddit.com/')
self.assertEqual(r['title'], 'Title number two')
self.assertEqual(r['content'], 'Dominus vobiscum')
created = datetime.fromtimestamp(1438792533.0)
self.assertEqual(r['publishedDate'], created)
self.assertTrue('thumbnail_src' not in r)
self.assertTrue('img_src' not in r)
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
from collections import defaultdict
import mock
from searx.testing import SearxTestCase
from searx.engines import unsplash
class TestUnsplashEngine(SearxTestCase):
def test_request(self):
query = 'penguin'
_dict = defaultdict(dict)
_dict['pageno'] = 1
params = unsplash.request(query, _dict)
self.assertTrue('url' in params)
self.assertTrue(query in params['url'])
def test_response(self):
resp = mock.Mock(text='{}')
result = unsplash.response(resp)
self.assertEqual([], result)
resp.text = '{"results": []}'
result = unsplash.response(resp)
self.assertEqual([], result)
# Sourced from https://unsplash.com/napi/search/photos?query=penguin&xp=&per_page=20&page=2
with open('./tests/unit/engines/unsplash_fixture.json') as fixture:
resp.text = fixture.read()
result = unsplash.response(resp)
self.assertEqual(len(result), 2)
self.assertEqual(result[0]['title'], 'low angle photography of swimming penguin')
self.assertEqual(result[0]['url'], 'https://unsplash.com/photos/FY8d721UO_4')
self.assertEqual(result[0]['thumbnail_src'], 'https://images.unsplash.com/photo-1523557148507-1b77641c7e7c?ixlib=rb-0.3.5&q=80\
&fm=jpg&crop=entropy&cs=tinysrgb&w=200&fit=max')
self.assertEqual(result[0]['img_src'], 'https://images.unsplash.com/photo-1523557148507-1b77641c7e7c\
?ixlib=rb-0.3.5')
self.assertEqual(result[0]['content'], '')
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
from collections import defaultdict
import mock
from searx.engines import www1x
from searx.testing import SearxTestCase
class TestWww1xEngine(SearxTestCase):
def test_request(self):
query = 'test_query'
params = www1x.request(query, defaultdict(dict))
self.assertTrue('url' in params)
self.assertTrue(query in params['url'])
self.assertTrue('1x.com' in params['url'])
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment