mrs.py 1.7 KB

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