test_kickass.py 22 KB

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