mrs.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. """Matrix Rooms Search - a fully-featured, standalone, matrix rooms search service.
  3. Configuration
  4. =============
  5. The engine has the following mandatory settings:
  6. - :py:obj:`base_url`
  7. .. code:: yaml
  8. - name: MRS
  9. engine: mrs
  10. base_url: https://mrs-host
  11. ...
  12. Implementation
  13. ==============
  14. """
  15. from urllib.parse import quote_plus
  16. about = {
  17. "website": 'https://matrixrooms.info',
  18. "wikidata_id": None,
  19. "official_api_documentation": 'https://gitlab.com/etke.cc/mrs/api/-/blob/main/openapi.yml?ref_type=heads',
  20. "use_official_api": True,
  21. "require_api_key": False,
  22. "results": 'JSON',
  23. }
  24. paging = True
  25. categories = ['social media']
  26. base_url = ""
  27. matrix_url = "https://matrix.to"
  28. page_size = 20
  29. def init(engine_settings): # pylint: disable=unused-argument
  30. """The ``base_url`` must be set in the configuration, if ``base_url`` is not
  31. set, a :py:obj:`ValueError` is raised during initialization.
  32. """
  33. if not base_url:
  34. raise ValueError('engine MRS, base_url is unset')
  35. def request(query, params):
  36. params['url'] = f"{base_url}/search/{quote_plus(query)}/{page_size}/{(params['pageno']-1)*page_size}"
  37. return params
  38. def response(resp):
  39. results = []
  40. for result in resp.json():
  41. results.append(
  42. {
  43. 'url': matrix_url + '/#/' + result['alias'],
  44. 'title': result['name'],
  45. 'content': result['topic']
  46. + f" // {result['members']} members"
  47. + f" // {result['alias']}"
  48. + f" // {result['server']}",
  49. 'thumbnail': result['avatar_url'],
  50. }
  51. )
  52. return results