123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. # -*- coding: utf-8 -*-
  2. from collections import defaultdict
  3. import mock
  4. from searx.engines import kickass
  5. from searx.testing import SearxTestCase
  6. class TestKickassEngine(SearxTestCase):
  7. def test_request(self):
  8. query = 'test_query'
  9. dicto = defaultdict(dict)
  10. dicto['pageno'] = 1
  11. params = kickass.request(query, dicto)
  12. self.assertIn('url', params)
  13. self.assertIn(query, params['url'])
  14. self.assertIn('kickass.to', params['url'])
  15. self.assertFalse(params['verify'])
  16. def test_response(self):
  17. self.assertRaises(AttributeError, kickass.response, None)
  18. self.assertRaises(AttributeError, kickass.response, [])
  19. self.assertRaises(AttributeError, kickass.response, '')
  20. self.assertRaises(AttributeError, kickass.response, '[]')
  21. response = mock.Mock(text='<html></html>')
  22. self.assertEqual(kickass.response(response), [])
  23. html = """
  24. <table cellpadding="0" cellspacing="0" class="data" style="width: 100%">
  25. <tr class="firstr">
  26. <th class="width100perc nopad">torrent name</th>
  27. <th class="center">
  28. <a href="/search/test/?field=size&sorder=desc" rel="nofollow">size</a>
  29. </th>
  30. <th class="center"><span class="files">
  31. <a href="/search/test/?field=files_count&sorder=desc" rel="nofollow">files</a></span>
  32. </th>
  33. <th class="center"><span>
  34. <a href="/search/test/?field=time_add&sorder=desc" rel="nofollow">age</a></span>
  35. </th>
  36. <th class="center"><span class="seed">
  37. <a href="/search/test/?field=seeders&sorder=desc" rel="nofollow">seed</a></span>
  38. </th>
  39. <th class="lasttd nobr center">
  40. <a href="/search/test/?field=leechers&sorder=desc" rel="nofollow">leech</a>
  41. </th>
  42. </tr>
  43. <tr class="even" id="torrent_test6478745">
  44. <td>
  45. <div class="iaconbox center floatright">
  46. <a rel="6478745,0" class="icommentjs icon16" href="/test-t6478745.html#comment">
  47. <em style="font-size: 12px; margin: 0 4px 0 4px;" class="iconvalue">3</em>
  48. <i class="ka ka-comment"></i>
  49. </a>
  50. <a class="iverify icon16" href="/test-t6478745.html" title="Verified Torrent">
  51. <i class="ka ka16 ka-verify ka-green"></i>
  52. </a>
  53. <a href="#" onclick="_scq.push([]); return false;" class="partner1Button idownload icon16">
  54. <i class="ka ka16 ka-arrow-down partner1Button"></i>
  55. </a>
  56. <a title="Torrent magnet link"
  57. href="magnet:?xt=urn:btih:MAGNETURL&dn=test" class="imagnet icon16">
  58. <i class="ka ka16 ka-magnet"></i>
  59. </a>
  60. <a title="Download torrent file"
  61. href="http://torcache.net/torrent/53917.torrent?title=test" class="idownload icon16">
  62. <i class="ka ka16 ka-arrow-down"></i>
  63. </a>
  64. </div>
  65. <div class="torrentname">
  66. <a href="/test-t6478745.html" class="torType txtType"></a>
  67. <a href="/test-t6478745.html" class="normalgrey font12px plain bold"></a>
  68. <div class="markeredBlock torType txtType">
  69. <a href="/url.html" class="cellMainLink">
  70. <strong class="red">This should be the title</strong>
  71. </a>
  72. <span class="font11px lightgrey block">
  73. Posted by <i class="ka ka-verify" style="font-size: 16px;color:orange;"></i>
  74. <a class="plain" href="/user/riri/">riri</a> in
  75. <span id="cat_6478745">
  76. <strong><a href="/other/">Other</a> > <a href="/unsorted/">Unsorted</a></strong>
  77. </span>
  78. </span>
  79. </div>
  80. </td>
  81. <td class="nobr center">449 <span>bytes</span></td>
  82. <td class="center">4</td>
  83. <td class="center">2&nbsp;years</td>
  84. <td class="green center">10</td>
  85. <td class="red lasttd center">1</td>
  86. </tr>
  87. </table>
  88. """
  89. response = mock.Mock(text=html)
  90. results = kickass.response(response)
  91. self.assertEqual(type(results), list)
  92. self.assertEqual(len(results), 1)
  93. self.assertEqual(results[0]['title'], 'This should be the title')
  94. self.assertEqual(results[0]['url'], 'https://kickass.to/url.html')
  95. self.assertEqual(results[0]['content'], 'Posted by riri in Other &gt; Unsorted')
  96. self.assertEqual(results[0]['seed'], 10)
  97. self.assertEqual(results[0]['leech'], 1)
  98. self.assertEqual(results[0]['filesize'], 449)
  99. self.assertEqual(results[0]['files'], 4)
  100. self.assertEqual(results[0]['magnetlink'], 'magnet:?xt=urn:btih:MAGNETURL&dn=test')
  101. self.assertEqual(results[0]['torrentfile'], 'http://torcache.net/torrent/53917.torrent?title=test')
  102. html = """
  103. <table cellpadding="0" cellspacing="0" class="data" style="width: 100%">
  104. <tr class="firstr">
  105. <th class="width100perc nopad">torrent name</th>
  106. <th class="center">
  107. <a href="/search/test/?field=size&sorder=desc" rel="nofollow">size</a>
  108. </th>
  109. <th class="center"><span class="files">
  110. <a href="/search/test/?field=files_count&sorder=desc" rel="nofollow">files</a></span>
  111. </th>
  112. <th class="center"><span>
  113. <a href="/search/test/?field=time_add&sorder=desc" rel="nofollow">age</a></span>
  114. </th>
  115. <th class="center"><span class="seed">
  116. <a href="/search/test/?field=seeders&sorder=desc" rel="nofollow">seed</a></span>
  117. </th>
  118. <th class="lasttd nobr center">
  119. <a href="/search/test/?field=leechers&sorder=desc" rel="nofollow">leech</a>
  120. </th>
  121. </tr>
  122. </table>
  123. """
  124. response = mock.Mock(text=html)
  125. results = kickass.response(response)
  126. self.assertEqual(type(results), list)
  127. self.assertEqual(len(results), 0)
  128. html = """
  129. <table cellpadding="0" cellspacing="0" class="data" style="width: 100%">
  130. <tr class="firstr">
  131. <th class="width100perc nopad">torrent name</th>
  132. <th class="center">
  133. <a href="/search/test/?field=size&sorder=desc" rel="nofollow">size</a>
  134. </th>
  135. <th class="center"><span class="files">
  136. <a href="/search/test/?field=files_count&sorder=desc" rel="nofollow">files</a></span>
  137. </th>
  138. <th class="center"><span>
  139. <a href="/search/test/?field=time_add&sorder=desc" rel="nofollow">age</a></span>
  140. </th>
  141. <th class="center"><span class="seed">
  142. <a href="/search/test/?field=seeders&sorder=desc" rel="nofollow">seed</a></span>
  143. </th>
  144. <th class="lasttd nobr center">
  145. <a href="/search/test/?field=leechers&sorder=desc" rel="nofollow">leech</a>
  146. </th>
  147. </tr>
  148. <tr class="even" id="torrent_test6478745">
  149. <td>
  150. <div class="iaconbox center floatright">
  151. <a rel="6478745,0" class="icommentjs icon16" href="/test-t6478745.html#comment">
  152. <em style="font-size: 12px; margin: 0 4px 0 4px;" class="iconvalue">3</em>
  153. <i class="ka ka-comment"></i>
  154. </a>
  155. <a class="iverify icon16" href="/test-t6478745.html" title="Verified Torrent">
  156. <i class="ka ka16 ka-verify ka-green"></i>
  157. </a>
  158. <a href="#" onclick="_scq.push([]); return false;" class="partner1Button idownload icon16">
  159. <i class="ka ka16 ka-arrow-down partner1Button"></i>
  160. </a>
  161. <a title="Torrent magnet link"
  162. href="magnet:?xt=urn:btih:MAGNETURL&dn=test" class="imagnet icon16">
  163. <i class="ka ka16 ka-magnet"></i>
  164. </a>
  165. <a title="Download torrent file"
  166. href="http://torcache.net/torrent/53917.torrent?title=test" class="idownload icon16">
  167. <i class="ka ka16 ka-arrow-down"></i>
  168. </a>
  169. </div>
  170. <div class="torrentname">
  171. <a href="/test-t6478745.html" class="torType txtType"></a>
  172. <a href="/test-t6478745.html" class="normalgrey font12px plain bold"></a>
  173. <div class="markeredBlock torType txtType">
  174. <a href="/url.html" class="cellMainLink">
  175. <strong class="red">This should be the title</strong>
  176. </a>
  177. <span class="font11px lightgrey block">
  178. Posted by <i class="ka ka-verify" style="font-size: 16px;color:orange;"></i>
  179. <a class="plain" href="/user/riri/">riri</a> in
  180. <span id="cat_6478745">
  181. <strong><a href="/other/">Other</a> > <a href="/unsorted/">Unsorted</a></strong>
  182. </span>
  183. </span>
  184. </div>
  185. </td>
  186. <td class="nobr center">1 <span>KB</span></td>
  187. <td class="center">4</td>
  188. <td class="center">2&nbsp;years</td>
  189. <td class="green center">10</td>
  190. <td class="red lasttd center">1</td>
  191. </tr>
  192. <tr class="even" id="torrent_test6478745">
  193. <td>
  194. <div class="iaconbox center floatright">
  195. <a rel="6478745,0" class="icommentjs icon16" href="/test-t6478745.html#comment">
  196. <em style="font-size: 12px; margin: 0 4px 0 4px;" class="iconvalue">3</em>
  197. <i class="ka ka-comment"></i>
  198. </a>
  199. <a class="iverify icon16" href="/test-t6478745.html" title="Verified Torrent">
  200. <i class="ka ka16 ka-verify ka-green"></i>
  201. </a>
  202. <a href="#" onclick="_scq.push([]); return false;" class="partner1Button idownload icon16">
  203. <i class="ka ka16 ka-arrow-down partner1Button"></i>
  204. </a>
  205. <a title="Torrent magnet link"
  206. href="magnet:?xt=urn:btih:MAGNETURL&dn=test" class="imagnet icon16">
  207. <i class="ka ka16 ka-magnet"></i>
  208. </a>
  209. <a title="Download torrent file"
  210. href="http://torcache.net/torrent/53917.torrent?title=test" class="idownload icon16">
  211. <i class="ka ka16 ka-arrow-down"></i>
  212. </a>
  213. </div>
  214. <div class="torrentname">
  215. <a href="/test-t6478745.html" class="torType txtType"></a>
  216. <a href="/test-t6478745.html" class="normalgrey font12px plain bold"></a>
  217. <div class="markeredBlock torType txtType">
  218. <a href="/url.html" class="cellMainLink">
  219. <strong class="red">This should be the title</strong>
  220. </a>
  221. <span class="font11px lightgrey block">
  222. Posted by <i class="ka ka-verify" style="font-size: 16px;color:orange;"></i>
  223. <a class="plain" href="/user/riri/">riri</a> in
  224. <span id="cat_6478745">
  225. <strong><a href="/other/">Other</a> > <a href="/unsorted/">Unsorted</a></strong>
  226. </span>
  227. </span>
  228. </div>
  229. </td>
  230. <td class="nobr center">1 <span>MB</span></td>
  231. <td class="center">4</td>
  232. <td class="center">2&nbsp;years</td>
  233. <td class="green center">9</td>
  234. <td class="red lasttd center">1</td>
  235. </tr>
  236. <tr class="even" id="torrent_test6478745">
  237. <td>
  238. <div class="iaconbox center floatright">
  239. <a rel="6478745,0" class="icommentjs icon16" href="/test-t6478745.html#comment">
  240. <em style="font-size: 12px; margin: 0 4px 0 4px;" class="iconvalue">3</em>
  241. <i class="ka ka-comment"></i>
  242. </a>
  243. <a class="iverify icon16" href="/test-t6478745.html" title="Verified Torrent">
  244. <i class="ka ka16 ka-verify ka-green"></i>
  245. </a>
  246. <a href="#" onclick="_scq.push([]); return false;" class="partner1Button idownload icon16">
  247. <i class="ka ka16 ka-arrow-down partner1Button"></i>
  248. </a>
  249. <a title="Torrent magnet link"
  250. href="magnet:?xt=urn:btih:MAGNETURL&dn=test" class="imagnet icon16">
  251. <i class="ka ka16 ka-magnet"></i>
  252. </a>
  253. <a title="Download torrent file"
  254. href="http://torcache.net/torrent/53917.torrent?title=test" class="idownload icon16">
  255. <i class="ka ka16 ka-arrow-down"></i>
  256. </a>
  257. </div>
  258. <div class="torrentname">
  259. <a href="/test-t6478745.html" class="torType txtType"></a>
  260. <a href="/test-t6478745.html" class="normalgrey font12px plain bold"></a>
  261. <div class="markeredBlock torType txtType">
  262. <a href="/url.html" class="cellMainLink">
  263. <strong class="red">This should be the title</strong>
  264. </a>
  265. <span class="font11px lightgrey block">
  266. Posted by <i class="ka ka-verify" style="font-size: 16px;color:orange;"></i>
  267. <a class="plain" href="/user/riri/">riri</a> in
  268. <span id="cat_6478745">
  269. <strong><a href="/other/">Other</a> > <a href="/unsorted/">Unsorted</a></strong>
  270. </span>
  271. </span>
  272. </div>
  273. </td>
  274. <td class="nobr center">1 <span>GB</span></td>
  275. <td class="center">4</td>
  276. <td class="center">2&nbsp;years</td>
  277. <td class="green center">8</td>
  278. <td class="red lasttd center">1</td>
  279. </tr>
  280. <tr class="even" id="torrent_test6478745">
  281. <td>
  282. <div class="iaconbox center floatright">
  283. <a rel="6478745,0" class="icommentjs icon16" href="/test-t6478745.html#comment">
  284. <em style="font-size: 12px; margin: 0 4px 0 4px;" class="iconvalue">3</em>
  285. <i class="ka ka-comment"></i>
  286. </a>
  287. <a class="iverify icon16" href="/test-t6478745.html" title="Verified Torrent">
  288. <i class="ka ka16 ka-verify ka-green"></i>
  289. </a>
  290. <a href="#" onclick="_scq.push([]); return false;" class="partner1Button idownload icon16">
  291. <i class="ka ka16 ka-arrow-down partner1Button"></i>
  292. </a>
  293. <a title="Torrent magnet link"
  294. href="magnet:?xt=urn:btih:MAGNETURL&dn=test" class="imagnet icon16">
  295. <i class="ka ka16 ka-magnet"></i>
  296. </a>
  297. <a title="Download torrent file"
  298. href="http://torcache.net/torrent/53917.torrent?title=test" class="idownload icon16">
  299. <i class="ka ka16 ka-arrow-down"></i>
  300. </a>
  301. </div>
  302. <div class="torrentname">
  303. <a href="/test-t6478745.html" class="torType txtType"></a>
  304. <a href="/test-t6478745.html" class="normalgrey font12px plain bold"></a>
  305. <div class="markeredBlock torType txtType">
  306. <a href="/url.html" class="cellMainLink">
  307. <strong class="red">This should be the title</strong>
  308. </a>
  309. <span class="font11px lightgrey block">
  310. Posted by <i class="ka ka-verify" style="font-size: 16px;color:orange;"></i>
  311. <a class="plain" href="/user/riri/">riri</a> in
  312. <span id="cat_6478745">
  313. <strong><a href="/other/">Other</a> > <a href="/unsorted/">Unsorted</a></strong>
  314. </span>
  315. </span>
  316. </div>
  317. </td>
  318. <td class="nobr center">1 <span>TB</span></td>
  319. <td class="center">4</td>
  320. <td class="center">2&nbsp;years</td>
  321. <td class="green center">7</td>
  322. <td class="red lasttd center">1</td>
  323. </tr>
  324. <tr class="even" id="torrent_test6478745">
  325. <td>
  326. <div class="iaconbox center floatright">
  327. <a rel="6478745,0" class="icommentjs icon16" href="/test-t6478745.html#comment">
  328. <em style="font-size: 12px; margin: 0 4px 0 4px;" class="iconvalue">3</em>
  329. <i class="ka ka-comment"></i>
  330. </a>
  331. <a class="iverify icon16" href="/test-t6478745.html" title="Verified Torrent">
  332. <i class="ka ka16 ka-verify ka-green"></i>
  333. </a>
  334. <a href="#" onclick="_scq.push([]); return false;" class="partner1Button idownload icon16">
  335. <i class="ka ka16 ka-arrow-down partner1Button"></i>
  336. </a>
  337. <a title="Torrent magnet link"
  338. href="magnet:?xt=urn:btih:MAGNETURL&dn=test" class="imagnet icon16">
  339. <i class="ka ka16 ka-magnet"></i>
  340. </a>
  341. <a title="Download torrent file"
  342. href="http://torcache.net/torrent/53917.torrent?title=test" class="idownload icon16">
  343. <i class="ka ka16 ka-arrow-down"></i>
  344. </a>
  345. </div>
  346. <div class="torrentname">
  347. <a href="/test-t6478745.html" class="torType txtType"></a>
  348. <a href="/test-t6478745.html" class="normalgrey font12px plain bold"></a>
  349. <div class="markeredBlock torType txtType">
  350. <a href="/url.html" class="cellMainLink">
  351. <strong class="red">This should be the title</strong>
  352. </a>
  353. <span class="font11px lightgrey block">
  354. Posted by <i class="ka ka-verify" style="font-size: 16px;color:orange;"></i>
  355. <a class="plain" href="/user/riri/">riri</a> in
  356. <span id="cat_6478745">
  357. <strong><a href="/other/">Other</a> > <a href="/unsorted/">Unsorted</a></strong>
  358. </span>
  359. </span>
  360. </div>
  361. </td>
  362. <td class="nobr center">z <span>bytes</span></td>
  363. <td class="center">r</td>
  364. <td class="center">2&nbsp;years</td>
  365. <td class="green center">a</td>
  366. <td class="red lasttd center">t</td>
  367. </tr>
  368. </table>
  369. """
  370. response = mock.Mock(text=html)
  371. results = kickass.response(response)
  372. self.assertEqual(type(results), list)
  373. self.assertEqual(len(results), 5)
  374. self.assertEqual(results[0]['title'], 'This should be the title')
  375. self.assertEqual(results[0]['url'], 'https://kickass.to/url.html')
  376. self.assertEqual(results[0]['content'], 'Posted by riri in Other &gt; Unsorted')
  377. self.assertEqual(results[0]['seed'], 10)
  378. self.assertEqual(results[0]['leech'], 1)
  379. self.assertEqual(results[0]['files'], 4)
  380. self.assertEqual(results[0]['magnetlink'], 'magnet:?xt=urn:btih:MAGNETURL&dn=test')
  381. self.assertEqual(results[0]['torrentfile'], 'http://torcache.net/torrent/53917.torrent?title=test')
  382. self.assertEqual(results[0]['filesize'], 1024)
  383. self.assertEqual(results[1]['filesize'], 1048576)
  384. self.assertEqual(results[2]['filesize'], 1073741824)
  385. self.assertEqual(results[3]['filesize'], 1099511627776)
  386. self.assertEqual(results[4]['seed'], 0)
  387. self.assertEqual(results[4]['leech'], 0)
  388. self.assertEqual(results[4]['files'], None)
  389. self.assertEqual(results[4]['filesize'], None)