From 53c9fde992741dea936d9ca92d13e5a8a30b1c47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?No=C3=A9mi=20V=C3=A1nyi?= Date: Tue, 11 Oct 2016 19:31:42 +0200 Subject: [PATCH] fix kickass torrents engine --- searx/engines/kickass.py | 44 +++++++----------------------- searx/settings.yml | 6 ++++ searx/utils.py | 15 ++++++++++ tests/unit/engines/test_kickass.py | 26 +++++++++--------- 4 files changed, 44 insertions(+), 47 deletions(-) diff --git a/searx/engines/kickass.py b/searx/engines/kickass.py index 4c5d24008..9cd8284da 100644 --- a/searx/engines/kickass.py +++ b/searx/engines/kickass.py @@ -16,13 +16,14 @@ from urllib import quote from lxml import html from operator import itemgetter from searx.engines.xpath import extract_text +from searx.utils import get_torrent_size, convert_str_to_int # engine dependent config categories = ['videos', 'music', 'files'] paging = True # search-url -url = 'https://kickass.to/' +url = 'https://kickass.cd/' search_url = url + 'search/{search_term}/{pageno}/' # specific xpath variables @@ -57,41 +58,16 @@ def response(resp): href = urljoin(url, link.attrib['href']) 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] - filesize_multiplier = result.xpath('.//td[contains(@class, "nobr")]//span/text()')[0] - files = result.xpath('.//td[contains(@class, "center")][2]/text()')[0] + seed = extract_text(result.xpath('.//td[contains(@class, "green")]')) + leech = extract_text(result.xpath('.//td[contains(@class, "red")]')) + filesize_info = extract_text(result.xpath('.//td[contains(@class, "nobr")]')) + files = extract_text(result.xpath('.//td[contains(@class, "center")][2]')) - # convert seed to int if possible - if seed.isdigit(): - seed = int(seed) - else: - seed = 0 + seed = convert_str_to_int(seed) + leech = convert_str_to_int(leech) - # convert leech to int if possible - if leech.isdigit(): - leech = int(leech) - else: - leech = 0 - - # convert filesize to byte if possible - try: - filesize = float(filesize) - - # convert filesize to byte - if filesize_multiplier == 'TB': - filesize = int(filesize * 1024 * 1024 * 1024 * 1024) - elif filesize_multiplier == 'GB': - filesize = int(filesize * 1024 * 1024 * 1024) - elif filesize_multiplier == 'MB': - filesize = int(filesize * 1024 * 1024) - elif filesize_multiplier == 'KB': - filesize = int(filesize * 1024) - except: - filesize = None - - # convert files to int if possible + filesize, filesize_multiplier = filesize_info.split() + filesize = get_torrent_size(filesize, filesize_multiplier) if files.isdigit(): files = int(files) else: diff --git a/searx/settings.yml b/searx/settings.yml index 6b1233324..ba7ae428b 100644 --- a/searx/settings.yml +++ b/searx/settings.yml @@ -307,6 +307,12 @@ engines: timeout : 6.0 disabled : True + - name: kickass + engine : kickass + shortcut : kc + timeout : 4.0 + disabled : True + - name : microsoft academic engine : json_engine paging : True diff --git a/searx/utils.py b/searx/utils.py index b3806d3fd..5039fa975 100644 --- a/searx/utils.py +++ b/searx/utils.py @@ -252,12 +252,27 @@ def get_torrent_size(filesize, filesize_multiplier): filesize = int(filesize * 1024 * 1024) elif filesize_multiplier == 'KB': filesize = int(filesize * 1024) + elif filesize_multiplier == 'TiB': + filesize = int(filesize * 1000 * 1000 * 1000 * 1000) + elif filesize_multiplier == 'GiB': + filesize = int(filesize * 1000 * 1000 * 1000) + elif filesize_multiplier == 'MiB': + filesize = int(filesize * 1000 * 1000) + elif filesize_multiplier == 'KiB': + filesize = int(filesize * 1000) except: filesize = None return filesize +def convert_str_to_int(number_str): + if number_str.isdigit(): + return int(number_str) + else: + return 0 + + def is_valid_lang(lang): is_abbr = (len(lang) == 2) if is_abbr: diff --git a/tests/unit/engines/test_kickass.py b/tests/unit/engines/test_kickass.py index 4cfcaa63c..96c17911c 100644 --- a/tests/unit/engines/test_kickass.py +++ b/tests/unit/engines/test_kickass.py @@ -14,7 +14,7 @@ class TestKickassEngine(SearxTestCase): params = kickass.request(query, dicto) self.assertIn('url', params) self.assertIn(query, params['url']) - self.assertIn('kickass.to', params['url']) + self.assertIn('kickass.cd', params['url']) self.assertFalse(params['verify']) def test_response(self): @@ -84,7 +84,7 @@ class TestKickassEngine(SearxTestCase): - 449 bytes + 449 bytes 4 2 years 10 @@ -97,7 +97,7 @@ class TestKickassEngine(SearxTestCase): 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.to/url.html') + self.assertEqual(results[0]['url'], 'https://kickass.cd/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) @@ -191,7 +191,7 @@ class TestKickassEngine(SearxTestCase): - 1 KB + 1 KiB 4 2 years 10 @@ -235,7 +235,7 @@ class TestKickassEngine(SearxTestCase): - 1 MB + 1 MiB 4 2 years 9 @@ -279,7 +279,7 @@ class TestKickassEngine(SearxTestCase): - 1 GB + 1 GiB 4 2 years 8 @@ -323,7 +323,7 @@ class TestKickassEngine(SearxTestCase): - 1 TB + 1 TiB 4 2 years 7 @@ -367,7 +367,7 @@ class TestKickassEngine(SearxTestCase): - z bytes + z bytes r 2 years a @@ -380,17 +380,17 @@ class TestKickassEngine(SearxTestCase): 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.to/url.html') + self.assertEqual(results[0]['url'], 'https://kickass.cd/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[0]['filesize'], 1000) + self.assertEqual(results[1]['filesize'], 1000000) + self.assertEqual(results[2]['filesize'], 1000000000) + self.assertEqual(results[3]['filesize'], 1000000000000) self.assertEqual(results[4]['seed'], 0) self.assertEqual(results[4]['leech'], 0) self.assertEqual(results[4]['files'], None)