1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
# -*- coding: utf-8 -*-
from collections import defaultdict
import mock
from searx.engines import piratebay
from searx.testing import SearxTestCase
class TestPiratebayEngine(SearxTestCase):
def test_request(self):
query = 'test_query'
dicto = defaultdict(dict)
dicto['pageno'] = 1
dicto['category'] = 'Toto'
params = piratebay.request(query, dicto)
self.assertIn('url', params)
self.assertIn(query, params['url'])
self.assertIn('piratebay.cr', params['url'])
self.assertIn('0', params['url'])
dicto['category'] = 'music'
params = piratebay.request(query, dicto)
self.assertIn('100', params['url'])
def test_response(self):
self.assertRaises(AttributeError, piratebay.response, None)
self.assertRaises(AttributeError, piratebay.response, [])
self.assertRaises(AttributeError, piratebay.response, '')
self.assertRaises(AttributeError, piratebay.response, '[]')
response = mock.Mock(text='<html></html>')
self.assertEqual(piratebay.response(response), [])
html = """
<table id="searchResult">
<tr>
</tr>
<tr>
<td class="vertTh">
<center>
<a href="#" title="More from this category">Anime</a><br/>
(<a href="#" title="More from this category">Anime</a>)
</center>
</td>
<td>
<div class="detName">
<a href="/this.is.the.link" class="detLink" title="Title">
This is the title
</a>
</div>
<a href="magnet:?xt=urn:btih:MAGNETLINK" title="Download this torrent using magnet">
<img src="/static/img/icon-magnet.gif" alt="Magnet link"/>
</a>
<a href="http://torcache.net/torrent/TORRENTFILE.torrent" title="Download this torrent">
<img src="/static/img/dl.gif" class="dl" alt="Download"/>
</a>
<a href="/user/HorribleSubs">
<img src="/static/img/vip.gif" alt="VIP" title="VIP" style="width:11px;" border='0'/>
</a>
<img src="/static/img/11x11p.png"/>
<font class="detDesc">
This is the content <span>and should be</span> OK
</font>
</td>
<td align="right">13</td>
<td align="right">334</td>
</tr>
</table>
"""
response = mock.Mock(text=html)
results = piratebay.response(response)
self.assertEqual(type(results), list)
self.assertEqual(len(results), 1)
self.assertEqual(results[0]['title'], 'This is the title')
self.assertEqual(results[0]['url'], 'https://thepiratebay.cr/this.is.the.link')
self.assertEqual(results[0]['content'], 'This is the content and should be OK')
self.assertEqual(results[0]['seed'], 13)
self.assertEqual(results[0]['leech'], 334)
self.assertEqual(results[0]['magnetlink'], 'magnet:?xt=urn:btih:MAGNETLINK')
self.assertEqual(results[0]['torrentfile'], 'http://torcache.net/torrent/TORRENTFILE.torrent')
html = """
<table id="searchResult">
<tr>
</tr>
<tr>
<td class="vertTh">
<center>
<a href="#" title="More from this category">Anime</a><br/>
(<a href="#" title="More from this category">Anime</a>)
</center>
</td>
<td>
<div class="detName">
<a href="/this.is.the.link" class="detLink" title="Title">
This is the title
</a>
</div>
<a href="magnet:?xt=urn:btih:MAGNETLINK" title="Download this torrent using magnet">
<img src="/static/img/icon-magnet.gif" alt="Magnet link"/>
</a>
<a href="http://torcache.net/torrent/TORRENTFILE.torrent" title="Download this torrent">
<img src="/static/img/dl.gif" class="dl" alt="Download"/>
</a>
<a href="/user/HorribleSubs">
<img src="/static/img/vip.gif" alt="VIP" title="VIP" style="width:11px;" border='0'/>
</a>
<img src="/static/img/11x11p.png"/>
<font class="detDesc">
This is the content <span>and should be</span> OK
</font>
</td>
<td align="right">s</td>
<td align="right">d</td>
</tr>
</table>
"""
response = mock.Mock(text=html)
results = piratebay.response(response)
self.assertEqual(type(results), list)
self.assertEqual(len(results), 1)
self.assertEqual(results[0]['title'], 'This is the title')
self.assertEqual(results[0]['url'], 'https://thepiratebay.cr/this.is.the.link')
self.assertEqual(results[0]['content'], 'This is the content and should be OK')
self.assertEqual(results[0]['seed'], 0)
self.assertEqual(results[0]['leech'], 0)
self.assertEqual(results[0]['magnetlink'], 'magnet:?xt=urn:btih:MAGNETLINK')
self.assertEqual(results[0]['torrentfile'], 'http://torcache.net/torrent/TORRENTFILE.torrent')
html = """
<table id="searchResult">
</table>
"""
response = mock.Mock(text=html)
results = piratebay.response(response)
self.assertEqual(type(results), list)
self.assertEqual(len(results), 0)
|