test_youtube_noapi.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. # -*- coding: utf-8 -*-
  2. from collections import defaultdict
  3. import mock
  4. from searx.engines import youtube_noapi
  5. from searx.testing import SearxTestCase
  6. class TestYoutubeNoAPIEngine(SearxTestCase):
  7. def test_request(self):
  8. query = 'test_query'
  9. dicto = defaultdict(dict)
  10. dicto['pageno'] = 0
  11. params = youtube_noapi.request(query, dicto)
  12. self.assertIn('url', params)
  13. self.assertIn(query, params['url'])
  14. self.assertIn('youtube.com', params['url'])
  15. def test_response(self):
  16. self.assertRaises(AttributeError, youtube_noapi.response, None)
  17. self.assertRaises(AttributeError, youtube_noapi.response, [])
  18. self.assertRaises(AttributeError, youtube_noapi.response, '')
  19. self.assertRaises(AttributeError, youtube_noapi.response, '[]')
  20. response = mock.Mock(text='<html></html>')
  21. self.assertEqual(youtube_noapi.response(response), [])
  22. html = """
  23. <ol id="item-section-063864" class="item-section">
  24. <li>
  25. <div class="yt-lockup yt-lockup-tile yt-lockup-video vve-check clearfix yt-uix-tile"
  26. data-context-item-id="DIVZCPfAOeM"
  27. data-visibility-tracking="CBgQ3DAYACITCPGXnYau6sUCFZEIHAod-VQASCj0JECx_-GK5uqMpcIB">
  28. <div class="yt-lockup-dismissable"><div class="yt-lockup-thumbnail contains-addto">
  29. <a aria-hidden="true" href="/watch?v=DIVZCPfAOeM" class=" yt-uix-sessionlink pf-link"
  30. data-sessionlink="itct=CBgQ3DAYACITCPGXnYau6sUCFZEIHAod-VQASCj0JFIEdGVzdA">
  31. <div class="yt-thumb video-thumb"><img src="//i.ytimg.com/vi/DIVZCPfAOeM/mqdefault.jpg"
  32. width="196" height="110"/></div><span class="video-time" aria-hidden="true">11:35</span></a>
  33. <span class="thumb-menu dark-overflow-action-menu video-actions">
  34. </span>
  35. </div>
  36. <div class="yt-lockup-content">
  37. <h3 class="yt-lockup-title">
  38. <a href="/watch?v=DIVZCPfAOeM"
  39. class="yt-uix-tile-link yt-ui-ellipsis yt-ui-ellipsis-2 yt-uix-sessionlink spf-link"
  40. data-sessionlink="itct=CBgQ3DAYACITCPGXnYau6sUCFZEIHAod-VQASCj0JFIEdGVzdA"
  41. title="Top Speed Test Kawasaki Ninja H2 (Thailand) By. MEHAY SUPERBIKE"
  42. aria-describedby="description-id-259079" rel="spf-prefetch" dir="ltr">
  43. Title
  44. </a>
  45. <span class="accessible-description" id="description-id-259079"> - Durée : 11:35.</span>
  46. </h3>
  47. <div class="yt-lockup-byline">de
  48. <a href="/user/mheejapan" class=" yt-uix-sessionlink spf-link g-hovercard"
  49. data-sessionlink="itct=CBgQ3DAYACITCPGXnYau6sUCFZEIHAod-VQASCj0JA" data-ytid="UCzEesu54Hjs0uRKmpy66qeA"
  50. data-name="">MEHAY SUPERBIKE</a></div><div class="yt-lockup-meta">
  51. <ul class="yt-lockup-meta-info">
  52. <li>il y a 20 heures</li>
  53. <li>8 424 vues</li>
  54. </ul>
  55. </div>
  56. <div class="yt-lockup-description yt-ui-ellipsis yt-ui-ellipsis-2" dir="ltr">
  57. Description
  58. </div>
  59. <div class="yt-lockup-badges">
  60. <ul class="yt-badge-list ">
  61. <li class="yt-badge-item" >
  62. <span class="yt-badge">Nouveauté</span>
  63. </li>
  64. <li class="yt-badge-item" ><span class="yt-badge " >HD</span></li>
  65. </ul>
  66. </div>
  67. <div class="yt-lockup-action-menu yt-uix-menu-container">
  68. <div class="yt-uix-menu yt-uix-videoactionmenu hide-until-delayloaded"
  69. data-video-id="DIVZCPfAOeM" data-menu-content-id="yt-uix-videoactionmenu-menu">
  70. </div>
  71. </div>
  72. </div>
  73. </div>
  74. </div>
  75. </li>
  76. </ol>
  77. """
  78. response = mock.Mock(text=html)
  79. results = youtube_noapi.response(response)
  80. self.assertEqual(type(results), list)
  81. self.assertEqual(len(results), 1)
  82. self.assertEqual(results[0]['title'], 'Title')
  83. self.assertEqual(results[0]['url'], 'https://www.youtube.com/watch?v=DIVZCPfAOeM')
  84. self.assertEqual(results[0]['content'], 'Description')
  85. self.assertEqual(results[0]['thumbnail'], 'https://i.ytimg.com/vi/DIVZCPfAOeM/hqdefault.jpg')
  86. self.assertTrue('DIVZCPfAOeM' in results[0]['embedded'])
  87. html = """
  88. <ol id="item-section-063864" class="item-section">
  89. <li>
  90. <div class="yt-lockup yt-lockup-tile yt-lockup-video vve-check clearfix yt-uix-tile"
  91. data-context-item-id="DIVZCPfAOeM"
  92. data-visibility-tracking="CBgQ3DAYACITCPGXnYau6sUCFZEIHAod-VQASCj0JECx_-GK5uqMpcIB">
  93. <div class="yt-lockup-dismissable"><div class="yt-lockup-thumbnail contains-addto">
  94. <a aria-hidden="true" href="/watch?v=DIVZCPfAOeM" class=" yt-uix-sessionlink pf-link"
  95. data-sessionlink="itct=CBgQ3DAYACITCPGXnYau6sUCFZEIHAod-VQASCj0JFIEdGVzdA">
  96. <div class="yt-thumb video-thumb"><img src="//i.ytimg.com/vi/DIVZCPfAOeM/mqdefault.jpg"
  97. width="196" height="110"/></div><span class="video-time" aria-hidden="true">11:35</span></a>
  98. <span class="thumb-menu dark-overflow-action-menu video-actions">
  99. </span>
  100. </div>
  101. <div class="yt-lockup-content">
  102. <h3 class="yt-lockup-title">
  103. <span class="accessible-description" id="description-id-259079"> - Durée : 11:35.</span>
  104. </h3>
  105. <div class="yt-lockup-byline">de
  106. <a href="/user/mheejapan" class=" yt-uix-sessionlink spf-link g-hovercard"
  107. data-sessionlink="itct=CBgQ3DAYACITCPGXnYau6sUCFZEIHAod-VQASCj0JA" data-ytid="UCzEesu54Hjs0uRKmpy66qeA"
  108. data-name="">MEHAY SUPERBIKE</a></div><div class="yt-lockup-meta">
  109. <ul class="yt-lockup-meta-info">
  110. <li>il y a 20 heures</li>
  111. <li>8 424 vues</li>
  112. </ul>
  113. </div>
  114. <div class="yt-lockup-badges">
  115. <ul class="yt-badge-list ">
  116. <li class="yt-badge-item" >
  117. <span class="yt-badge">Nouveauté</span>
  118. </li>
  119. <li class="yt-badge-item" ><span class="yt-badge " >HD</span></li>
  120. </ul>
  121. </div>
  122. <div class="yt-lockup-action-menu yt-uix-menu-container">
  123. <div class="yt-uix-menu yt-uix-videoactionmenu hide-until-delayloaded"
  124. data-video-id="DIVZCPfAOeM" data-menu-content-id="yt-uix-videoactionmenu-menu">
  125. </div>
  126. </div>
  127. </div>
  128. </div>
  129. </div>
  130. </li>
  131. </ol>
  132. """
  133. response = mock.Mock(text=html)
  134. results = youtube_noapi.response(response)
  135. self.assertEqual(type(results), list)
  136. self.assertEqual(len(results), 1)
  137. html = """
  138. <ol id="item-section-063864" class="item-section">
  139. <li>
  140. </li>
  141. </ol>
  142. """
  143. response = mock.Mock(text=html)
  144. results = youtube_noapi.response(response)
  145. self.assertEqual(type(results), list)
  146. self.assertEqual(len(results), 0)