test_youtube_noapi.py 8.2KB

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