123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. # -*- coding: utf-8 -*-
  2. from collections import defaultdict
  3. import mock
  4. from searx.engines import btdigg
  5. from searx.testing import SearxTestCase
  6. class TestBtdiggEngine(SearxTestCase):
  7. def test_request(self):
  8. query = 'test_query'
  9. dicto = defaultdict(dict)
  10. dicto['pageno'] = 0
  11. params = btdigg.request(query, dicto)
  12. self.assertIn('url', params)
  13. self.assertIn(query, params['url'])
  14. self.assertIn('btdigg.org', params['url'])
  15. def test_response(self):
  16. self.assertRaises(AttributeError, btdigg.response, None)
  17. self.assertRaises(AttributeError, btdigg.response, [])
  18. self.assertRaises(AttributeError, btdigg.response, '')
  19. self.assertRaises(AttributeError, btdigg.response, '[]')
  20. response = mock.Mock(content='<html></html>')
  21. self.assertEqual(btdigg.response(response), [])
  22. html = """
  23. <div id="search_res">
  24. <table>
  25. <tr>
  26. <td class="idx">1</td>
  27. <td>
  28. <table class="torrent_name_tbl">
  29. <tr>
  30. <td class="torrent_name">
  31. <a href="/url">Should be the title</a>
  32. </td>
  33. </tr>
  34. </table>
  35. <table class="torrent_name_tbl">
  36. <tr>
  37. <td class="ttth">
  38. <a onclick="fclck(this.href)" href="magnet:?xt=urn:btih:magnet&amp;dn=Test"
  39. title="Télécharger des liens Magnet">[magnet]</a>
  40. </td>
  41. <td class="ttth">
  42. <a href="https://btcloud.io/manager?cmd=add&amp;info_hash=hash"
  43. target="_blank" title="Ajouter à BTCloud">[cloud]</a>
  44. </td>
  45. <td>
  46. <span class="attr_name">Taille:</span>
  47. <span class="attr_val">8 B</span>
  48. </td>
  49. <td>
  50. <span class="attr_name">Fichiers:</span>
  51. <span class="attr_val">710</span>
  52. </td>
  53. <td>
  54. <span class="attr_name">Téléchargements:</span>
  55. <span class="attr_val">5</span>
  56. </td>
  57. <td>
  58. <span class="attr_name">Temps:</span>
  59. <span class="attr_val">417.8&nbsp;jours</span>
  60. </td>
  61. <td>
  62. <span class="attr_name">Dernière&nbsp;mise&nbsp;à&nbsp;jour:</span>
  63. <span class="attr_val">5.3&nbsp;jours</span>
  64. </td>
  65. <td>
  66. <span class="attr_name">Faux:</span>
  67. <span class="attr_val">Aucun</span>
  68. </td>
  69. </tr>
  70. </table>
  71. <pre class="snippet">
  72. Content
  73. </pre>
  74. </td>
  75. </tr>
  76. </table>
  77. </div>
  78. """
  79. response = mock.Mock(content=html)
  80. results = btdigg.response(response)
  81. self.assertEqual(type(results), list)
  82. self.assertEqual(len(results), 1)
  83. self.assertEqual(results[0]['title'], 'Should be the title')
  84. self.assertEqual(results[0]['url'], 'https://btdigg.org/url')
  85. self.assertEqual(results[0]['content'], 'Content')
  86. self.assertEqual(results[0]['seed'], 5)
  87. self.assertEqual(results[0]['leech'], 0)
  88. self.assertEqual(results[0]['filesize'], 8)
  89. self.assertEqual(results[0]['files'], 710)
  90. self.assertEqual(results[0]['magnetlink'], 'magnet:?xt=urn:btih:magnet&dn=Test')
  91. html = """
  92. <div id="search_res">
  93. <table>
  94. </table>
  95. </div>
  96. """
  97. response = mock.Mock(content=html)
  98. results = btdigg.response(response)
  99. self.assertEqual(type(results), list)
  100. self.assertEqual(len(results), 0)
  101. html = """
  102. <div id="search_res">
  103. <table>
  104. <tr>
  105. <td class="idx">1</td>
  106. <td>
  107. <table class="torrent_name_tbl">
  108. <tr>
  109. <td class="torrent_name">
  110. <a href="/url">Should be the title</a>
  111. </td>
  112. </tr>
  113. </table>
  114. <table class="torrent_name_tbl">
  115. <tr>
  116. <td class="ttth">
  117. <a onclick="fclck(this.href)" href="magnet:?xt=urn:btih:magnet&amp;dn=Test"
  118. title="Télécharger des liens Magnet">[magnet]</a>
  119. </td>
  120. <td class="ttth">
  121. <a href="https://btcloud.io/manager?cmd=add&amp;info_hash=hash"
  122. target="_blank" title="Ajouter à BTCloud">[cloud]</a>
  123. </td>
  124. <td>
  125. <span class="attr_name">Taille:</span>
  126. <span class="attr_val">1 KB</span>
  127. </td>
  128. <td>
  129. <span class="attr_name">Fichiers:</span>
  130. <span class="attr_val">710</span>
  131. </td>
  132. <td>
  133. <span class="attr_name">Téléchargements:</span>
  134. <span class="attr_val">5</span>
  135. </td>
  136. <td>
  137. <span class="attr_name">Temps:</span>
  138. <span class="attr_val">417.8&nbsp;jours</span>
  139. </td>
  140. <td>
  141. <span class="attr_name">Dernière&nbsp;mise&nbsp;à&nbsp;jour:</span>
  142. <span class="attr_val">5.3&nbsp;jours</span>
  143. </td>
  144. <td>
  145. <span class="attr_name">Faux:</span>
  146. <span class="attr_val">Aucun</span>
  147. </td>
  148. </tr>
  149. </table>
  150. <pre class="snippet">
  151. Content
  152. </pre>
  153. </td>
  154. </tr>
  155. <tr>
  156. <td class="idx">1</td>
  157. <td>
  158. <table class="torrent_name_tbl">
  159. <tr>
  160. <td class="torrent_name">
  161. <a href="/url">Should be the title</a>
  162. </td>
  163. </tr>
  164. </table>
  165. <table class="torrent_name_tbl">
  166. <tr>
  167. <td class="ttth">
  168. <a onclick="fclck(this.href)" href="magnet:?xt=urn:btih:magnet&amp;dn=Test"
  169. title="Télécharger des liens Magnet">[magnet]</a>
  170. </td>
  171. <td class="ttth">
  172. <a href="https://btcloud.io/manager?cmd=add&amp;info_hash=hash"
  173. target="_blank" title="Ajouter à BTCloud">[cloud]</a>
  174. </td>
  175. <td>
  176. <span class="attr_name">Taille:</span>
  177. <span class="attr_val">1 MB</span>
  178. </td>
  179. <td>
  180. <span class="attr_name">Fichiers:</span>
  181. <span class="attr_val">a</span>
  182. </td>
  183. <td>
  184. <span class="attr_name">Téléchargements:</span>
  185. <span class="attr_val">4</span>
  186. </td>
  187. <td>
  188. <span class="attr_name">Temps:</span>
  189. <span class="attr_val">417.8&nbsp;jours</span>
  190. </td>
  191. <td>
  192. <span class="attr_name">Dernière&nbsp;mise&nbsp;à&nbsp;jour:</span>
  193. <span class="attr_val">5.3&nbsp;jours</span>
  194. </td>
  195. <td>
  196. <span class="attr_name">Faux:</span>
  197. <span class="attr_val">Aucun</span>
  198. </td>
  199. </tr>
  200. </table>
  201. <pre class="snippet">
  202. Content
  203. </pre>
  204. </td>
  205. </tr>
  206. <tr>
  207. <td class="idx">1</td>
  208. <td>
  209. <table class="torrent_name_tbl">
  210. <tr>
  211. <td class="torrent_name">
  212. <a href="/url">Should be the title</a>
  213. </td>
  214. </tr>
  215. </table>
  216. <table class="torrent_name_tbl">
  217. <tr>
  218. <td class="ttth">
  219. <a onclick="fclck(this.href)" href="magnet:?xt=urn:btih:magnet&amp;dn=Test"
  220. title="Télécharger des liens Magnet">[magnet]</a>
  221. </td>
  222. <td class="ttth">
  223. <a href="https://btcloud.io/manager?cmd=add&amp;info_hash=hash"
  224. target="_blank" title="Ajouter à BTCloud">[cloud]</a>
  225. </td>
  226. <td>
  227. <span class="attr_name">Taille:</span>
  228. <span class="attr_val">1 GB</span>
  229. </td>
  230. <td>
  231. <span class="attr_name">Fichiers:</span>
  232. <span class="attr_val">710</span>
  233. </td>
  234. <td>
  235. <span class="attr_name">Téléchargements:</span>
  236. <span class="attr_val">3</span>
  237. </td>
  238. <td>
  239. <span class="attr_name">Temps:</span>
  240. <span class="attr_val">417.8&nbsp;jours</span>
  241. </td>
  242. <td>
  243. <span class="attr_name">Dernière&nbsp;mise&nbsp;à&nbsp;jour:</span>
  244. <span class="attr_val">5.3&nbsp;jours</span>
  245. </td>
  246. <td>
  247. <span class="attr_name">Faux:</span>
  248. <span class="attr_val">Aucun</span>
  249. </td>
  250. </tr>
  251. </table>
  252. <pre class="snippet">
  253. Content
  254. </pre>
  255. </td>
  256. </tr>
  257. <tr>
  258. <td class="idx">1</td>
  259. <td>
  260. <table class="torrent_name_tbl">
  261. <tr>
  262. <td class="torrent_name">
  263. <a href="/url">Should be the title</a>
  264. </td>
  265. </tr>
  266. </table>
  267. <table class="torrent_name_tbl">
  268. <tr>
  269. <td class="ttth">
  270. <a onclick="fclck(this.href)" href="magnet:?xt=urn:btih:magnet&amp;dn=Test"
  271. title="Télécharger des liens Magnet">[magnet]</a>
  272. </td>
  273. <td class="ttth">
  274. <a href="https://btcloud.io/manager?cmd=add&amp;info_hash=hash"
  275. target="_blank" title="Ajouter à BTCloud">[cloud]</a>
  276. </td>
  277. <td>
  278. <span class="attr_name">Taille:</span>
  279. <span class="attr_val">1 TB</span>
  280. </td>
  281. <td>
  282. <span class="attr_name">Fichiers:</span>
  283. <span class="attr_val">710</span>
  284. </td>
  285. <td>
  286. <span class="attr_name">Téléchargements:</span>
  287. <span class="attr_val">2</span>
  288. </td>
  289. <td>
  290. <span class="attr_name">Temps:</span>
  291. <span class="attr_val">417.8&nbsp;jours</span>
  292. </td>
  293. <td>
  294. <span class="attr_name">Dernière&nbsp;mise&nbsp;à&nbsp;jour:</span>
  295. <span class="attr_val">5.3&nbsp;jours</span>
  296. </td>
  297. <td>
  298. <span class="attr_name">Faux:</span>
  299. <span class="attr_val">Aucun</span>
  300. </td>
  301. </tr>
  302. </table>
  303. <pre class="snippet">
  304. Content
  305. </pre>
  306. </td>
  307. </tr>
  308. <tr>
  309. <td class="idx">1</td>
  310. <td>
  311. <table class="torrent_name_tbl">
  312. <tr>
  313. <td class="torrent_name">
  314. <a href="/url">Should be the title</a>
  315. </td>
  316. </tr>
  317. </table>
  318. <table class="torrent_name_tbl">
  319. <tr>
  320. <td class="ttth">
  321. <a onclick="fclck(this.href)" href="magnet:?xt=urn:btih:magnet&amp;dn=Test"
  322. title="Télécharger des liens Magnet">[magnet]</a>
  323. </td>
  324. <td class="ttth">
  325. <a href="https://btcloud.io/manager?cmd=add&amp;info_hash=hash"
  326. target="_blank" title="Ajouter à BTCloud">[cloud]</a>
  327. </td>
  328. <td>
  329. <span class="attr_name">Taille:</span>
  330. <span class="attr_val">a TB</span>
  331. </td>
  332. <td>
  333. <span class="attr_name">Fichiers:</span>
  334. <span class="attr_val">710</span>
  335. </td>
  336. <td>
  337. <span class="attr_name">Téléchargements:</span>
  338. <span class="attr_val">z</span>
  339. </td>
  340. <td>
  341. <span class="attr_name">Temps:</span>
  342. <span class="attr_val">417.8&nbsp;jours</span>
  343. </td>
  344. <td>
  345. <span class="attr_name">Dernière&nbsp;mise&nbsp;à&nbsp;jour:</span>
  346. <span class="attr_val">5.3&nbsp;jours</span>
  347. </td>
  348. <td>
  349. <span class="attr_name">Faux:</span>
  350. <span class="attr_val">Aucun</span>
  351. </td>
  352. </tr>
  353. </table>
  354. <pre class="snippet">
  355. Content
  356. </pre>
  357. </td>
  358. </tr>
  359. </table>
  360. </div>
  361. """
  362. response = mock.Mock(content=html)
  363. results = btdigg.response(response)
  364. self.assertEqual(type(results), list)
  365. self.assertEqual(len(results), 5)
  366. self.assertEqual(results[0]['title'], 'Should be the title')
  367. self.assertEqual(results[0]['url'], 'https://btdigg.org/url')
  368. self.assertEqual(results[0]['content'], 'Content')
  369. self.assertEqual(results[0]['seed'], 5)
  370. self.assertEqual(results[0]['leech'], 0)
  371. self.assertEqual(results[0]['files'], 710)
  372. self.assertEqual(results[0]['magnetlink'], 'magnet:?xt=urn:btih:magnet&dn=Test')
  373. self.assertEqual(results[0]['filesize'], 1024)
  374. self.assertEqual(results[1]['filesize'], 1048576)
  375. self.assertEqual(results[2]['filesize'], 1073741824)
  376. self.assertEqual(results[3]['filesize'], 1099511627776)