seekr.py 5.7 KB

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