seekr.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. """seekr.com Seeker Score
  3. Seekr is a privately held search and content evaluation engine that prioritizes
  4. credibility over popularity.
  5. Configuration
  6. =============
  7. The engine has the following additional settings:
  8. - :py:obj:`seekr_category`
  9. - :py:obj:`api_key`
  10. This implementation is used by seekr engines in the :ref:`settings.yml
  11. <settings engine>`:
  12. .. code:: yaml
  13. - name: seekr news
  14. seekr_category: news
  15. ...
  16. - name: seekr images
  17. seekr_category: images
  18. ...
  19. - name: seekr videos
  20. seekr_category: videos
  21. ...
  22. Known Quirks
  23. ============
  24. The implementation to support :py:obj:`paging <searx.enginelib.Engine.paging>`
  25. is based on the *nextpage* method of Seekr's REST API. This feature is *next
  26. page driven* and plays well with the :ref:`infinite_scroll <settings ui>`
  27. setting in SearXNG but it does not really fit into SearXNG's UI to select a page
  28. by number.
  29. Implementations
  30. ===============
  31. """
  32. from datetime import datetime
  33. from json import loads
  34. from urllib.parse import urlencode
  35. from flask_babel import gettext
  36. about = {
  37. "website": 'https://seekr.com/',
  38. "official_api_documentation": None,
  39. "use_official_api": False,
  40. "require_api_key": True,
  41. "results": 'JSON',
  42. "language": 'en',
  43. }
  44. base_url = "https://api.seekr.com"
  45. paging = True
  46. api_key = "srh1-22fb-sekr"
  47. """API key / reversed engineered / is still the same one since 2022."""
  48. seekr_category: str = 'unset'
  49. """Search category, any of ``news``, ``videos`` or ``images``."""
  50. def init(engine_settings):
  51. # global paging
  52. if engine_settings['seekr_category'] not in ['news', 'videos', 'images']:
  53. raise ValueError(f"Unsupported seekr category: {engine_settings['seekr_category']}")
  54. def request(query, params):
  55. if not query:
  56. return None
  57. args = {
  58. 'query': query,
  59. 'apiKey': api_key,
  60. }
  61. api_url = base_url + '/engine'
  62. if seekr_category == 'news':
  63. api_url += '/v2/newssearch'
  64. elif seekr_category == 'images':
  65. api_url += '/imagetab'
  66. elif seekr_category == 'videos':
  67. api_url += '/videotab'
  68. params['url'] = f"{api_url}?{urlencode(args)}"
  69. if params['pageno'] > 1:
  70. nextpage = params['engine_data'].get('nextpage')
  71. if nextpage:
  72. params['url'] = nextpage
  73. return params
  74. def _images_response(json):
  75. search_results = json.get('expertResponses')
  76. if search_results:
  77. search_results = search_results[0].get('advice')
  78. else: # response from a 'nextResultSet'
  79. search_results = json.get('advice')
  80. results = []
  81. if not search_results:
  82. return results
  83. for result in search_results['results']:
  84. summary = loads(result['summary'])
  85. results.append(
  86. {
  87. 'template': 'images.html',
  88. 'url': summary['refererurl'],
  89. 'title': result['title'],
  90. 'img_src': result['url'],
  91. 'resolution': f"{summary['width']}x{summary['height']}",
  92. 'thumbnail_src': 'https://media.seekr.com/engine/rp/' + summary['tg'] + '/?src= ' + result['thumbnail'],
  93. }
  94. )
  95. if search_results.get('nextResultSet'):
  96. results.append(
  97. {
  98. "engine_data": search_results.get('nextResultSet'),
  99. "key": "nextpage",
  100. }
  101. )
  102. return results
  103. def _videos_response(json):
  104. search_results = json.get('expertResponses')
  105. if search_results:
  106. search_results = search_results[0].get('advice')
  107. else: # response from a 'nextResultSet'
  108. search_results = json.get('advice')
  109. results = []
  110. if not search_results:
  111. return results
  112. for result in search_results['results']:
  113. summary = loads(result['summary'])
  114. results.append(
  115. {
  116. 'template': 'videos.html',
  117. 'url': result['url'],
  118. 'title': result['title'],
  119. 'thumbnail': 'https://media.seekr.com/engine/rp/' + summary['tg'] + '/?src= ' + result['thumbnail'],
  120. }
  121. )
  122. if search_results.get('nextResultSet'):
  123. results.append(
  124. {
  125. "engine_data": search_results.get('nextResultSet'),
  126. "key": "nextpage",
  127. }
  128. )
  129. return results
  130. def _news_response(json):
  131. search_results = json.get('expertResponses')
  132. if search_results:
  133. search_results = search_results[0]['advice']['categorySearchResult']['searchResult']
  134. else: # response from a 'nextResultSet'
  135. search_results = json.get('advice')
  136. results = []
  137. if not search_results:
  138. return results
  139. for result in search_results['results']:
  140. results.append(
  141. {
  142. 'url': result['url'],
  143. 'title': result['title'],
  144. 'content': result['summary'] or result["topCategory"] or result["displayUrl"] or '',
  145. 'thumbnail': result.get('thumbnail', ''),
  146. 'publishedDate': datetime.strptime(result['pubDate'][:19], '%Y-%m-%d %H:%M:%S'),
  147. 'metadata': gettext("Language") + ': ' + result.get('language', ''),
  148. }
  149. )
  150. if search_results.get('nextResultSet'):
  151. results.append(
  152. {
  153. "engine_data": search_results.get('nextResultSet'),
  154. "key": "nextpage",
  155. }
  156. )
  157. return results
  158. def response(resp):
  159. json = resp.json()
  160. if seekr_category == "videos":
  161. return _videos_response(json)
  162. if seekr_category == "images":
  163. return _images_response(json)
  164. if seekr_category == "news":
  165. return _news_response(json)
  166. raise ValueError(f"Unsupported seekr category: {seekr_category}")