Browse Source

Merge pull request #1009 from misnyo/torrentz

[fix]torrentz search engine fixed for new version
Adam Tauber 7 years ago
parent
commit
489bf0f6fd
3 changed files with 42 additions and 34 deletions
  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 View File

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
  @provide-api  no
5
  @provide-api  no
6
 
6
 
7
  @using-api    no
7
  @using-api    no
14
 import re
14
 import re
15
 from lxml import html
15
 from lxml import html
16
 from datetime import datetime
16
 from datetime import datetime
17
-from searx.engines.nyaa import int_or_zero, get_filesize_mul
18
 from searx.engines.xpath import extract_text
17
 from searx.engines.xpath import extract_text
19
 from searx.url_utils import urlencode
18
 from searx.url_utils import urlencode
19
+from searx.utils import get_torrent_size
20
 
20
 
21
 # engine dependent config
21
 # engine dependent config
22
 categories = ['files', 'videos', 'music']
22
 categories = ['files', 'videos', 'music']
23
 paging = True
23
 paging = True
24
 
24
 
25
 # search-url
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
 search_url = base_url + 'search?{query}'
28
 search_url = base_url + 'search?{query}'
29
 
29
 
30
 
30
 
31
 # do search-request
31
 # do search-request
32
 def request(query, params):
32
 def request(query, params):
33
     page = params['pageno'] - 1
33
     page = params['pageno'] - 1
34
-    query = urlencode({'q': query, 'p': page})
34
+    query = urlencode({'f': query, 'p': page})
35
     params['url'] = search_url.format(query=query)
35
     params['url'] = search_url.format(query=query)
36
     return params
36
     return params
37
 
37
 
54
         # extract url and remove a slash in the beginning
54
         # extract url and remove a slash in the beginning
55
         link = links[0].attrib.get('href').lstrip('/')
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
         params = {
65
         params = {
61
             'url': base_url + link,
66
             'url': base_url + link,
62
             'title': title,
67
             'title': title,
63
-            'seed': int_or_zero(seed),
64
-            'leech': int_or_zero(leech),
68
+            'seed': seed,
69
+            'leech': leech,
65
             'template': 'torrent.html'
70
             'template': 'torrent.html'
66
         }
71
         }
67
 
72
 
68
         # let's try to calculate the torrent size
73
         # let's try to calculate the torrent size
69
         try:
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
         except:
80
         except:
74
             pass
81
             pass
75
 
82
 
80
 
87
 
81
         # extract and convert creation date
88
         # extract and convert creation date
82
         try:
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
             params['publishedDate'] = date
92
             params['publishedDate'] = date
87
         except:
93
         except:
88
             pass
94
             pass

+ 6
- 0
searx/settings.yml View File

552
     timeout : 6.0
552
     timeout : 6.0
553
     disabled : True
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
   - name : twitter
561
   - name : twitter
556
     engine : twitter
562
     engine : twitter
557
     shortcut : tw
563
     shortcut : tw

+ 14
- 18
tests/unit/engines/test_torrentz.py View File

14
         params = torrentz.request(query, dic)
14
         params = torrentz.request(query, dic)
15
         self.assertTrue('url' in params)
15
         self.assertTrue('url' in params)
16
         self.assertTrue(query in params['url'])
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
     def test_response(self):
19
     def test_response(self):
20
         resp = mock.Mock(text='<html></html>')
20
         resp = mock.Mock(text='<html></html>')
30
               books ebooks
30
               books ebooks
31
             </dt>
31
             </dt>
32
             <dd>
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
             </dd>
38
             </dd>
41
           </dl>
39
           </dl>
42
 
40
 
48
               books ebooks
46
               books ebooks
49
             </dt>
47
             </dt>
50
             <dd>
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
             </dd>
54
             </dd>
59
           </dl>
55
           </dl>
60
         </div>
56
         </div>
68
 
64
 
69
         # testing against the first result
65
         # testing against the first result
70
         r = results[0]
66
         r = results[0]
71
-        self.assertEqual(r['url'], 'https://torrentz.eu/4362e08b1d80e1820fb2550b752f9f3126fe76d6')
67
+        self.assertEqual(r['url'], 'https://torrentz2.eu/4362e08b1d80e1820fb2550b752f9f3126fe76d6')
72
         self.assertEqual(r['title'], 'Completely valid info books ebooks')
68
         self.assertEqual(r['title'], 'Completely valid info books ebooks')
73
         # 22 Nov 2015 03:01:42
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
         self.assertEqual(r['seed'], 14)
71
         self.assertEqual(r['seed'], 14)
76
         self.assertEqual(r['leech'], 1)
72
         self.assertEqual(r['leech'], 1)
77
         self.assertEqual(r['filesize'], 30 * 1024 * 1024)
73
         self.assertEqual(r['filesize'], 30 * 1024 * 1024)
79
 
75
 
80
         # testing against the second result
76
         # testing against the second result
81
         r = results[1]
77
         r = results[1]
82
-        self.assertEqual(r['url'], 'https://torrentz.eu/poaskdpokaspod')
78
+        self.assertEqual(r['url'], 'https://torrentz2.eu/poaskdpokaspod')
83
         self.assertEqual(r['title'], 'Invalid hash and date and filesize books ebooks')
79
         self.assertEqual(r['title'], 'Invalid hash and date and filesize books ebooks')
84
         self.assertEqual(r['seed'], 5555)
80
         self.assertEqual(r['seed'], 5555)
85
         self.assertEqual(r['leech'], 1234567)
81
         self.assertEqual(r['leech'], 1234567)