Kaynağa Gözat

fix kickass torrents engine

Noémi Ványi 8 yıl önce
ebeveyn
işleme
53c9fde992
4 değiştirilmiş dosya ile 45 ekleme ve 48 silme
  1. 11
    35
      searx/engines/kickass.py
  2. 6
    0
      searx/settings.yml
  3. 15
    0
      searx/utils.py
  4. 13
    13
      tests/unit/engines/test_kickass.py

+ 11
- 35
searx/engines/kickass.py Dosyayı Görüntüle

@@ -16,13 +16,14 @@ from urllib import quote
16 16
 from lxml import html
17 17
 from operator import itemgetter
18 18
 from searx.engines.xpath import extract_text
19
+from searx.utils import get_torrent_size, convert_str_to_int
19 20
 
20 21
 # engine dependent config
21 22
 categories = ['videos', 'music', 'files']
22 23
 paging = True
23 24
 
24 25
 # search-url
25
-url = 'https://kickass.to/'
26
+url = 'https://kickass.cd/'
26 27
 search_url = url + 'search/{search_term}/{pageno}/'
27 28
 
28 29
 # specific xpath variables
@@ -57,41 +58,16 @@ def response(resp):
57 58
         href = urljoin(url, link.attrib['href'])
58 59
         title = extract_text(link)
59 60
         content = escape(extract_text(result.xpath(content_xpath)))
60
-        seed = result.xpath('.//td[contains(@class, "green")]/text()')[0]
61
-        leech = result.xpath('.//td[contains(@class, "red")]/text()')[0]
62
-        filesize = result.xpath('.//td[contains(@class, "nobr")]/text()')[0]
63
-        filesize_multiplier = result.xpath('.//td[contains(@class, "nobr")]//span/text()')[0]
64
-        files = result.xpath('.//td[contains(@class, "center")][2]/text()')[0]
65
-
66
-        # convert seed to int if possible
67
-        if seed.isdigit():
68
-            seed = int(seed)
69
-        else:
70
-            seed = 0
61
+        seed = extract_text(result.xpath('.//td[contains(@class, "green")]'))
62
+        leech = extract_text(result.xpath('.//td[contains(@class, "red")]'))
63
+        filesize_info = extract_text(result.xpath('.//td[contains(@class, "nobr")]'))
64
+        files = extract_text(result.xpath('.//td[contains(@class, "center")][2]'))
71 65
 
72
-        # convert leech to int if possible
73
-        if leech.isdigit():
74
-            leech = int(leech)
75
-        else:
76
-            leech = 0
77
-
78
-        # convert filesize to byte if possible
79
-        try:
80
-            filesize = float(filesize)
81
-
82
-            # convert filesize to byte
83
-            if filesize_multiplier == 'TB':
84
-                filesize = int(filesize * 1024 * 1024 * 1024 * 1024)
85
-            elif filesize_multiplier == 'GB':
86
-                filesize = int(filesize * 1024 * 1024 * 1024)
87
-            elif filesize_multiplier == 'MB':
88
-                filesize = int(filesize * 1024 * 1024)
89
-            elif filesize_multiplier == 'KB':
90
-                filesize = int(filesize * 1024)
91
-        except:
92
-            filesize = None
93
-
94
-        # convert files to int if possible
66
+        seed = convert_str_to_int(seed)
67
+        leech = convert_str_to_int(leech)
68
+
69
+        filesize, filesize_multiplier = filesize_info.split()
70
+        filesize = get_torrent_size(filesize, filesize_multiplier)
95 71
         if files.isdigit():
96 72
             files = int(files)
97 73
         else:

+ 6
- 0
searx/settings.yml Dosyayı Görüntüle

@@ -307,6 +307,12 @@ engines:
307 307
     timeout : 6.0
308 308
     disabled : True
309 309
 
310
+  - name: kickass
311
+    engine : kickass
312
+    shortcut : kc
313
+    timeout : 4.0
314
+    disabled : True
315
+
310 316
   - name : microsoft academic
311 317
     engine : json_engine
312 318
     paging : True

+ 15
- 0
searx/utils.py Dosyayı Görüntüle

@@ -252,12 +252,27 @@ def get_torrent_size(filesize, filesize_multiplier):
252 252
             filesize = int(filesize * 1024 * 1024)
253 253
         elif filesize_multiplier == 'KB':
254 254
             filesize = int(filesize * 1024)
255
+        elif filesize_multiplier == 'TiB':
256
+            filesize = int(filesize * 1000 * 1000 * 1000 * 1000)
257
+        elif filesize_multiplier == 'GiB':
258
+            filesize = int(filesize * 1000 * 1000 * 1000)
259
+        elif filesize_multiplier == 'MiB':
260
+            filesize = int(filesize * 1000 * 1000)
261
+        elif filesize_multiplier == 'KiB':
262
+            filesize = int(filesize * 1000)
255 263
     except:
256 264
         filesize = None
257 265
 
258 266
     return filesize
259 267
 
260 268
 
269
+def convert_str_to_int(number_str):
270
+    if number_str.isdigit():
271
+        return int(number_str)
272
+    else:
273
+        return 0
274
+
275
+
261 276
 def is_valid_lang(lang):
262 277
     is_abbr = (len(lang) == 2)
263 278
     if is_abbr:

+ 13
- 13
tests/unit/engines/test_kickass.py Dosyayı Görüntüle

@@ -14,7 +14,7 @@ class TestKickassEngine(SearxTestCase):
14 14
         params = kickass.request(query, dicto)
15 15
         self.assertIn('url', params)
16 16
         self.assertIn(query, params['url'])
17
-        self.assertIn('kickass.to', params['url'])
17
+        self.assertIn('kickass.cd', params['url'])
18 18
         self.assertFalse(params['verify'])
19 19
 
20 20
     def test_response(self):
@@ -84,7 +84,7 @@ class TestKickassEngine(SearxTestCase):
84 84
                         </span>
85 85
                     </div>
86 86
                 </td>
87
-                <td class="nobr center">449 <span>bytes</span></td>
87
+                <td class="nobr center">449 bytes</td>
88 88
                 <td class="center">4</td>
89 89
                 <td class="center">2&nbsp;years</td>
90 90
                 <td class="green center">10</td>
@@ -97,7 +97,7 @@ class TestKickassEngine(SearxTestCase):
97 97
         self.assertEqual(type(results), list)
98 98
         self.assertEqual(len(results), 1)
99 99
         self.assertEqual(results[0]['title'], 'This should be the title')
100
-        self.assertEqual(results[0]['url'], 'https://kickass.to/url.html')
100
+        self.assertEqual(results[0]['url'], 'https://kickass.cd/url.html')
101 101
         self.assertEqual(results[0]['content'], 'Posted by riri in Other &gt; Unsorted')
102 102
         self.assertEqual(results[0]['seed'], 10)
103 103
         self.assertEqual(results[0]['leech'], 1)
@@ -191,7 +191,7 @@ class TestKickassEngine(SearxTestCase):
191 191
                         </span>
192 192
                     </div>
193 193
                 </td>
194
-                <td class="nobr center">1 <span>KB</span></td>
194
+                <td class="nobr center">1 KiB</td>
195 195
                 <td class="center">4</td>
196 196
                 <td class="center">2&nbsp;years</td>
197 197
                 <td class="green center">10</td>
@@ -235,7 +235,7 @@ class TestKickassEngine(SearxTestCase):
235 235
                         </span>
236 236
                     </div>
237 237
                 </td>
238
-                <td class="nobr center">1 <span>MB</span></td>
238
+                <td class="nobr center">1 MiB</td>
239 239
                 <td class="center">4</td>
240 240
                 <td class="center">2&nbsp;years</td>
241 241
                 <td class="green center">9</td>
@@ -279,7 +279,7 @@ class TestKickassEngine(SearxTestCase):
279 279
                         </span>
280 280
                     </div>
281 281
                 </td>
282
-                <td class="nobr center">1 <span>GB</span></td>
282
+                <td class="nobr center">1 GiB</td>
283 283
                 <td class="center">4</td>
284 284
                 <td class="center">2&nbsp;years</td>
285 285
                 <td class="green center">8</td>
@@ -323,7 +323,7 @@ class TestKickassEngine(SearxTestCase):
323 323
                         </span>
324 324
                     </div>
325 325
                 </td>
326
-                <td class="nobr center">1 <span>TB</span></td>
326
+                <td class="nobr center">1 TiB</td>
327 327
                 <td class="center">4</td>
328 328
                 <td class="center">2&nbsp;years</td>
329 329
                 <td class="green center">7</td>
@@ -367,7 +367,7 @@ class TestKickassEngine(SearxTestCase):
367 367
                         </span>
368 368
                     </div>
369 369
                 </td>
370
-                <td class="nobr center">z <span>bytes</span></td>
370
+                <td class="nobr center">z bytes</td>
371 371
                 <td class="center">r</td>
372 372
                 <td class="center">2&nbsp;years</td>
373 373
                 <td class="green center">a</td>
@@ -380,17 +380,17 @@ class TestKickassEngine(SearxTestCase):
380 380
         self.assertEqual(type(results), list)
381 381
         self.assertEqual(len(results), 5)
382 382
         self.assertEqual(results[0]['title'], 'This should be the title')
383
-        self.assertEqual(results[0]['url'], 'https://kickass.to/url.html')
383
+        self.assertEqual(results[0]['url'], 'https://kickass.cd/url.html')
384 384
         self.assertEqual(results[0]['content'], 'Posted by riri in Other &gt; Unsorted')
385 385
         self.assertEqual(results[0]['seed'], 10)
386 386
         self.assertEqual(results[0]['leech'], 1)
387 387
         self.assertEqual(results[0]['files'], 4)
388 388
         self.assertEqual(results[0]['magnetlink'], 'magnet:?xt=urn:btih:MAGNETURL&dn=test')
389 389
         self.assertEqual(results[0]['torrentfile'], 'http://torcache.net/torrent/53917.torrent?title=test')
390
-        self.assertEqual(results[0]['filesize'], 1024)
391
-        self.assertEqual(results[1]['filesize'], 1048576)
392
-        self.assertEqual(results[2]['filesize'], 1073741824)
393
-        self.assertEqual(results[3]['filesize'], 1099511627776)
390
+        self.assertEqual(results[0]['filesize'], 1000)
391
+        self.assertEqual(results[1]['filesize'], 1000000)
392
+        self.assertEqual(results[2]['filesize'], 1000000000)
393
+        self.assertEqual(results[3]['filesize'], 1000000000000)
394 394
         self.assertEqual(results[4]['seed'], 0)
395 395
         self.assertEqual(results[4]['leech'], 0)
396 396
         self.assertEqual(results[4]['files'], None)