Kaynağa Gözat

Merge pull request #1009 from misnyo/torrentz

[fix]torrentz search engine fixed for new version
Adam Tauber 7 yıl önce
ebeveyn
işleme
489bf0f6fd
3 değiştirilmiş dosya ile 42 ekleme ve 34 silme
  1. 22
    16
      searx/engines/torrentz.py
  2. 6
    0
      searx/settings.yml
  3. 14
    18
      tests/unit/engines/test_torrentz.py

+ 22
- 16
searx/engines/torrentz.py Dosyayı Görüntüle

@@ -1,7 +1,7 @@
1 1
 """
2
- Torrentz.eu (BitTorrent meta-search engine)
2
+ Torrentz2.eu (BitTorrent meta-search engine)
3 3
 
4
- @website      https://torrentz.eu/
4
+ @website      https://torrentz2.eu/
5 5
  @provide-api  no
6 6
 
7 7
  @using-api    no
@@ -14,24 +14,24 @@
14 14
 import re
15 15
 from lxml import html
16 16
 from datetime import datetime
17
-from searx.engines.nyaa import int_or_zero, get_filesize_mul
18 17
 from searx.engines.xpath import extract_text
19 18
 from searx.url_utils import urlencode
19
+from searx.utils import get_torrent_size
20 20
 
21 21
 # engine dependent config
22 22
 categories = ['files', 'videos', 'music']
23 23
 paging = True
24 24
 
25 25
 # search-url
26
-# https://torrentz.eu/search?f=EXAMPLE&p=6
27
-base_url = 'https://torrentz.eu/'
26
+# https://torrentz2.eu/search?f=EXAMPLE&p=6
27
+base_url = 'https://torrentz2.eu/'
28 28
 search_url = base_url + 'search?{query}'
29 29
 
30 30
 
31 31
 # do search-request
32 32
 def request(query, params):
33 33
     page = params['pageno'] - 1
34
-    query = urlencode({'q': query, 'p': page})
34
+    query = urlencode({'f': query, 'p': page})
35 35
     params['url'] = search_url.format(query=query)
36 36
     return params
37 37
 
@@ -54,22 +54,29 @@ def response(resp):
54 54
         # extract url and remove a slash in the beginning
55 55
         link = links[0].attrib.get('href').lstrip('/')
56 56
 
57
-        seed = result.xpath('./dd/span[@class="u"]/text()')[0].replace(',', '')
58
-        leech = result.xpath('./dd/span[@class="d"]/text()')[0].replace(',', '')
57
+        seed = 0
58
+        leech = 0
59
+        try:
60
+            seed = int(result.xpath('./dd/span[4]/text()')[0].replace(',', ''))
61
+            leech = int(result.xpath('./dd/span[5]/text()')[0].replace(',', ''))
62
+        except:
63
+            pass
59 64
 
60 65
         params = {
61 66
             'url': base_url + link,
62 67
             'title': title,
63
-            'seed': int_or_zero(seed),
64
-            'leech': int_or_zero(leech),
68
+            'seed': seed,
69
+            'leech': leech,
65 70
             'template': 'torrent.html'
66 71
         }
67 72
 
68 73
         # let's try to calculate the torrent size
69 74
         try:
70
-            size_str = result.xpath('./dd/span[@class="s"]/text()')[0]
71
-            size, suffix = size_str.split()
72
-            params['filesize'] = int(size) * get_filesize_mul(suffix)
75
+            filesize_info = result.xpath('./dd/span[3]/text()')[0]
76
+            filesize, filesize_multiplier = filesize_info.split()
77
+            filesize = get_torrent_size(filesize, filesize_multiplier)
78
+
79
+            params['filesize'] = filesize
73 80
         except:
74 81
             pass
75 82
 
@@ -80,9 +87,8 @@ def response(resp):
80 87
 
81 88
         # extract and convert creation date
82 89
         try:
83
-            date_str = result.xpath('./dd/span[@class="a"]/span')[0].attrib.get('title')
84
-            # Fri, 25 Mar 2016 16:29:01
85
-            date = datetime.strptime(date_str, '%a, %d %b %Y %H:%M:%S')
90
+            date_ts = result.xpath('./dd/span[2]')[0].attrib.get('title')
91
+            date = datetime.fromtimestamp(float(date_ts))
86 92
             params['publishedDate'] = date
87 93
         except:
88 94
             pass

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

@@ -552,6 +552,12 @@ engines:
552 552
     timeout : 6.0
553 553
     disabled : True
554 554
 
555
+  - name : torrentz
556
+    engine : torrentz
557
+    shortcut : tor
558
+    url: https://torrentz2.eu/
559
+    timeout : 3.0
560
+
555 561
   - name : twitter
556 562
     engine : twitter
557 563
     shortcut : tw

+ 14
- 18
tests/unit/engines/test_torrentz.py Dosyayı Görüntüle

@@ -14,7 +14,7 @@ class TestTorrentzEngine(SearxTestCase):
14 14
         params = torrentz.request(query, dic)
15 15
         self.assertTrue('url' in params)
16 16
         self.assertTrue(query in params['url'])
17
-        self.assertTrue('torrentz.eu' in params['url'])
17
+        self.assertTrue('torrentz2.eu' in params['url'])
18 18
 
19 19
     def test_response(self):
20 20
         resp = mock.Mock(text='<html></html>')
@@ -30,13 +30,11 @@ class TestTorrentzEngine(SearxTestCase):
30 30
               books ebooks
31 31
             </dt>
32 32
             <dd>
33
-              <span class="v">1</span>
34
-              <span class="a">
35
-                <span title="Sun, 22 Nov 2015 03:01:42">4 months</span>
36
-              </span>
37
-              <span class="s">30 MB</span>
38
-              <span class="u">14</span>
39
-              <span class="d">1</span>
33
+              <span>1</span>
34
+              <span title="1503595924">5 hours</span>
35
+              <span>30 MB</span>
36
+              <span>14</span>
37
+              <span>1</span>
40 38
             </dd>
41 39
           </dl>
42 40
 
@@ -48,13 +46,11 @@ class TestTorrentzEngine(SearxTestCase):
48 46
               books ebooks
49 47
             </dt>
50 48
             <dd>
51
-              <span class="v">1</span>
52
-              <span class="a">
53
-                <span title="Sun, 2124091j0j190gm42">4 months</span>
54
-              </span>
55
-              <span class="s">30MB</span>
56
-              <span class="u">5,555</span>
57
-              <span class="d">1,234,567</span>
49
+              <span>1</span>
50
+              <span title="1503595924 aaa">5 hours</span>
51
+              <span>30MB</span>
52
+              <span>5,555</span>
53
+              <span>1,234,567</span>
58 54
             </dd>
59 55
           </dl>
60 56
         </div>
@@ -68,10 +64,10 @@ class TestTorrentzEngine(SearxTestCase):
68 64
 
69 65
         # testing against the first result
70 66
         r = results[0]
71
-        self.assertEqual(r['url'], 'https://torrentz.eu/4362e08b1d80e1820fb2550b752f9f3126fe76d6')
67
+        self.assertEqual(r['url'], 'https://torrentz2.eu/4362e08b1d80e1820fb2550b752f9f3126fe76d6')
72 68
         self.assertEqual(r['title'], 'Completely valid info books ebooks')
73 69
         # 22 Nov 2015 03:01:42
74
-        self.assertEqual(r['publishedDate'], datetime(2015, 11, 22, 3, 1, 42))
70
+        self.assertEqual(r['publishedDate'], datetime.fromtimestamp(1503595924))
75 71
         self.assertEqual(r['seed'], 14)
76 72
         self.assertEqual(r['leech'], 1)
77 73
         self.assertEqual(r['filesize'], 30 * 1024 * 1024)
@@ -79,7 +75,7 @@ class TestTorrentzEngine(SearxTestCase):
79 75
 
80 76
         # testing against the second result
81 77
         r = results[1]
82
-        self.assertEqual(r['url'], 'https://torrentz.eu/poaskdpokaspod')
78
+        self.assertEqual(r['url'], 'https://torrentz2.eu/poaskdpokaspod')
83 79
         self.assertEqual(r['title'], 'Invalid hash and date and filesize books ebooks')
84 80
         self.assertEqual(r['seed'], 5555)
85 81
         self.assertEqual(r['leech'], 1234567)