瀏覽代碼

[fix] piratebay: missing torrent link

Adam Tauber 10 年之前
父節點
當前提交
52d243f7af
共有 2 個檔案被更改,包括 37 行新增4 行删除
  1. 7
    3
      searx/engines/piratebay.py
  2. 30
    1
      searx/tests/engines/test_piratebay.py

+ 7
- 3
searx/engines/piratebay.py 查看文件

@@ -1,4 +1,4 @@
1
-## Piratebay (Videos, Music, Files)
1
+#  Piratebay (Videos, Music, Files)
2 2
 #
3 3
 # @website     https://thepiratebay.se
4 4
 # @provide-api no (nothing found)
@@ -78,7 +78,11 @@ def response(resp):
78 78
             leech = 0
79 79
 
80 80
         magnetlink = result.xpath(magnet_xpath)[0]
81
-        torrentfile = result.xpath(torrent_xpath)[0]
81
+        torrentfile_links = result.xpath(torrent_xpath)
82
+        if torrentfile_links:
83
+            torrentfile_link = torrentfile_links[0].attrib.get('href')
84
+        else:
85
+            torrentfile_link = None
82 86
 
83 87
         # append result
84 88
         results.append({'url': href,
@@ -87,7 +91,7 @@ def response(resp):
87 91
                         'seed': seed,
88 92
                         'leech': leech,
89 93
                         'magnetlink': magnetlink.attrib.get('href'),
90
-                        'torrentfile': torrentfile.attrib.get('href'),
94
+                        'torrentfile': torrentfile_link,
91 95
                         'template': 'torrent.html'})
92 96
 
93 97
     # return results sorted by seeder

+ 30
- 1
searx/tests/engines/test_piratebay.py 查看文件

@@ -65,12 +65,39 @@ class TestPiratebayEngine(SearxTestCase):
65 65
                 <td align="right">13</td>
66 66
                 <td align="right">334</td>
67 67
             </tr>
68
+            <tr>
69
+                <td class="vertTh">
70
+                    <center>
71
+                        <a href="#" title="More from this category">Anime</a><br/>
72
+                        (<a href="#" title="More from this category">Anime</a>)
73
+                    </center>
74
+                </td>
75
+                <td>
76
+                    <div class="detName">
77
+                        <a href="/this.is.the.link" class="detLink" title="Title">
78
+                            This is the title
79
+                        </a>
80
+                    </div>
81
+                    <a href="magnet:?xt=urn:btih:MAGNETLINK" title="Download this torrent using magnet">
82
+                        <img src="/static/img/icon-magnet.gif" alt="Magnet link"/>
83
+                    </a>
84
+                    <a href="/user/HorribleSubs">
85
+                        <img src="/static/img/vip.gif" alt="VIP" title="VIP" style="width:11px;" border='0'/>
86
+                    </a>
87
+                    <img src="/static/img/11x11p.png"/>
88
+                    <font class="detDesc">
89
+                        This is the content <span>and should be</span> OK
90
+                    </font>
91
+                </td>
92
+                <td align="right">13</td>
93
+                <td align="right">334</td>
94
+            </tr>
68 95
         </table>
69 96
         """
70 97
         response = mock.Mock(text=html)
71 98
         results = piratebay.response(response)
72 99
         self.assertEqual(type(results), list)
73
-        self.assertEqual(len(results), 1)
100
+        self.assertEqual(len(results), 2)
74 101
         self.assertEqual(results[0]['title'], 'This is the title')
75 102
         self.assertEqual(results[0]['url'], 'https://thepiratebay.se/this.is.the.link')
76 103
         self.assertEqual(results[0]['content'], 'This is the content and should be OK')
@@ -79,6 +106,8 @@ class TestPiratebayEngine(SearxTestCase):
79 106
         self.assertEqual(results[0]['magnetlink'], 'magnet:?xt=urn:btih:MAGNETLINK')
80 107
         self.assertEqual(results[0]['torrentfile'], 'http://torcache.net/torrent/TORRENTFILE.torrent')
81 108
 
109
+        self.assertEqual(results[1]['torrentfile'], None)
110
+
82 111
         html = """
83 112
         <table id="searchResult">
84 113
             <tr>