diff --git a/searx/engines/kickass.py b/searx/engines/kickass.py
index ac349283d..8b89e1f47 100644
--- a/searx/engines/kickass.py
+++ b/searx/engines/kickass.py
@@ -13,6 +13,7 @@ from cgi import escape
from urllib import quote
from lxml import html
from operator import itemgetter
+from searx.engines.xpath import extract_text
# engine dependent config
categories = ['videos', 'music', 'files']
@@ -56,9 +57,8 @@ def response(resp):
for result in search_res[1:]:
link = result.xpath('.//a[@class="cellMainLink"]')[0]
href = urljoin(url, link.attrib['href'])
- title = ' '.join(link.xpath('.//text()'))
- content = escape(html.tostring(result.xpath(content_xpath)[0],
- method="text"))
+ title = extract_text(link)
+ content = escape(extract_text(result.xpath(content_xpath)))
seed = result.xpath('.//td[contains(@class, "green")]/text()')[0]
leech = result.xpath('.//td[contains(@class, "red")]/text()')[0]
filesize = result.xpath('.//td[contains(@class, "nobr")]/text()')[0]
@@ -88,7 +88,7 @@ def response(resp):
filesize = int(filesize * 1024 * 1024 * 1024)
elif filesize_multiplier == 'MB':
filesize = int(filesize * 1024 * 1024)
- elif filesize_multiplier == 'kb':
+ elif filesize_multiplier == 'KB':
filesize = int(filesize * 1024)
except:
filesize = None
diff --git a/searx/tests/engines/test_kickass.py b/searx/tests/engines/test_kickass.py
new file mode 100644
index 000000000..3c20a97e7
--- /dev/null
+++ b/searx/tests/engines/test_kickass.py
@@ -0,0 +1,398 @@
+# -*- coding: utf-8 -*-
+from collections import defaultdict
+import mock
+from searx.engines import kickass
+from searx.testing import SearxTestCase
+
+
+class TestKickassEngine(SearxTestCase):
+
+ def test_request(self):
+ query = 'test_query'
+ dicto = defaultdict(dict)
+ dicto['pageno'] = 1
+ params = kickass.request(query, dicto)
+ self.assertIn('url', params)
+ self.assertIn(query, params['url'])
+ self.assertIn('kickass.so', params['url'])
+ self.assertIn('verify', params)
+ self.assertFalse(params['verify'])
+
+ def test_response(self):
+ self.assertRaises(AttributeError, kickass.response, None)
+ self.assertRaises(AttributeError, kickass.response, [])
+ self.assertRaises(AttributeError, kickass.response, '')
+ self.assertRaises(AttributeError, kickass.response, '[]')
+
+ response = mock.Mock(text='')
+ self.assertEqual(kickass.response(response), [])
+
+ html = """
+
+
+ torrent name |
+
+ size
+ |
+
+ files
+ |
+
+ age
+ |
+
+ seed
+ |
+
+ leech
+ |
+
+
+
+
+ |
+ 449 bytes |
+ 4 |
+ 2 years |
+ 10 |
+ 1 |
+
+
+ """
+ response = mock.Mock(text=html)
+ results = kickass.response(response)
+ self.assertEqual(type(results), list)
+ self.assertEqual(len(results), 1)
+ self.assertEqual(results[0]['title'], 'This should be the title')
+ self.assertEqual(results[0]['url'], 'https://kickass.so/url.html')
+ self.assertEqual(results[0]['content'], 'Posted by riri in Other > Unsorted')
+ self.assertEqual(results[0]['seed'], 10)
+ self.assertEqual(results[0]['leech'], 1)
+ self.assertEqual(results[0]['filesize'], 449)
+ self.assertEqual(results[0]['files'], 4)
+ self.assertEqual(results[0]['magnetlink'], 'magnet:?xt=urn:btih:MAGNETURL&dn=test')
+ self.assertEqual(results[0]['torrentfile'], 'http://torcache.net/torrent/53917.torrent?title=test')
+
+ html = """
+
+ """
+ response = mock.Mock(text=html)
+ results = kickass.response(response)
+ self.assertEqual(type(results), list)
+ self.assertEqual(len(results), 0)
+
+ html = """
+
+
+ torrent name |
+
+ size
+ |
+
+ files
+ |
+
+ age
+ |
+
+ seed
+ |
+
+ leech
+ |
+
+
+
+
+ |
+ 1 KB |
+ 4 |
+ 2 years |
+ 10 |
+ 1 |
+
+
+
+
+ |
+ 1 MB |
+ 4 |
+ 2 years |
+ 9 |
+ 1 |
+
+
+
+
+ |
+ 1 GB |
+ 4 |
+ 2 years |
+ 8 |
+ 1 |
+
+
+
+
+ |
+ 1 TB |
+ 4 |
+ 2 years |
+ 7 |
+ 1 |
+
+
+
+
+ |
+ z bytes |
+ r |
+ 2 years |
+ a |
+ t |
+
+
+ """
+ response = mock.Mock(text=html)
+ results = kickass.response(response)
+ self.assertEqual(type(results), list)
+ self.assertEqual(len(results), 5)
+ self.assertEqual(results[0]['title'], 'This should be the title')
+ self.assertEqual(results[0]['url'], 'https://kickass.so/url.html')
+ self.assertEqual(results[0]['content'], 'Posted by riri in Other > Unsorted')
+ self.assertEqual(results[0]['seed'], 10)
+ self.assertEqual(results[0]['leech'], 1)
+ self.assertEqual(results[0]['files'], 4)
+ self.assertEqual(results[0]['magnetlink'], 'magnet:?xt=urn:btih:MAGNETURL&dn=test')
+ self.assertEqual(results[0]['torrentfile'], 'http://torcache.net/torrent/53917.torrent?title=test')
+ self.assertEqual(results[0]['filesize'], 1024)
+ self.assertEqual(results[1]['filesize'], 1048576)
+ self.assertEqual(results[2]['filesize'], 1073741824)
+ self.assertEqual(results[3]['filesize'], 1099511627776)
+ self.assertEqual(results[4]['seed'], 0)
+ self.assertEqual(results[4]['leech'], 0)
+ self.assertEqual(results[4]['files'], None)
+ self.assertEqual(results[4]['filesize'], None)
diff --git a/searx/tests/test_engines.py b/searx/tests/test_engines.py
index b07444e42..66f8fbff7 100644
--- a/searx/tests/test_engines.py
+++ b/searx/tests/test_engines.py
@@ -9,6 +9,7 @@ from searx.tests.engines.test_digg import * # noqa
from searx.tests.engines.test_dummy import * # noqa
from searx.tests.engines.test_flickr import * # noqa
from searx.tests.engines.test_github import * # noqa
+from searx.tests.engines.test_kickass import * # noqa
from searx.tests.engines.test_mixcloud import * # noqa
from searx.tests.engines.test_searchcode_code import * # noqa
from searx.tests.engines.test_searchcode_doc import * # noqa