lemmy.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. """This engine uses the Lemmy API (https://lemmy.ml/api/v3/search), which is
  3. documented at `lemmy-js-client`_ / `Interface Search`_. Since Lemmy is
  4. federated, results are from many different, independent lemmy instances, and not
  5. only the official one.
  6. .. _lemmy-js-client: https://join-lemmy.org/api/modules.html
  7. .. _Interface Search: https://join-lemmy.org/api/interfaces/Search.html
  8. Configuration
  9. =============
  10. The engine has the following additional settings:
  11. - :py:obj:`base_url`
  12. - :py:obj:`lemmy_type`
  13. This implementation is used by different lemmy engines in the :ref:`settings.yml
  14. <settings engine>`:
  15. .. code:: yaml
  16. - name: lemmy communities
  17. lemmy_type: Communities
  18. ...
  19. - name: lemmy users
  20. lemmy_type: Users
  21. ...
  22. - name: lemmy posts
  23. lemmy_type: Posts
  24. ...
  25. - name: lemmy comments
  26. lemmy_type: Comments
  27. ...
  28. Implementations
  29. ===============
  30. """
  31. from datetime import datetime
  32. from urllib.parse import urlencode
  33. from flask_babel import gettext
  34. from searx.utils import markdown_to_text
  35. about = {
  36. "website": 'https://lemmy.ml/',
  37. "wikidata_id": 'Q84777032',
  38. "official_api_documentation": "https://join-lemmy.org/api/",
  39. "use_official_api": True,
  40. "require_api_key": False,
  41. "results": 'JSON',
  42. }
  43. paging = True
  44. categories = ['social media']
  45. base_url = "https://lemmy.ml/"
  46. """By default, https://lemmy.ml is used for providing the results. If you want
  47. to use a different lemmy instance, you can specify ``base_url``.
  48. """
  49. lemmy_type = "Communities"
  50. """Any of ``Communities``, ``Users``, ``Posts``, ``Comments``"""
  51. def request(query, params):
  52. args = {
  53. 'q': query,
  54. 'page': params['pageno'],
  55. 'type_': lemmy_type,
  56. }
  57. params['url'] = f"{base_url}api/v3/search?{urlencode(args)}"
  58. return params
  59. def _get_communities(json):
  60. results = []
  61. for result in json["communities"]:
  62. counts = result['counts']
  63. metadata = (
  64. f"{gettext('subscribers')}: {counts.get('subscribers', 0)}"
  65. f" | {gettext('posts')}: {counts.get('posts', 0)}"
  66. f" | {gettext('active users')}: {counts.get('users_active_half_year', 0)}"
  67. )
  68. results.append(
  69. {
  70. 'url': result['community']['actor_id'],
  71. 'title': result['community']['title'],
  72. 'content': markdown_to_text(result['community'].get('description', '')),
  73. 'thumbnail': result['community'].get('icon', result['community'].get('banner')),
  74. 'publishedDate': datetime.strptime(counts['published'][:19], '%Y-%m-%dT%H:%M:%S'),
  75. 'metadata': metadata,
  76. }
  77. )
  78. return results
  79. def _get_users(json):
  80. results = []
  81. for result in json["users"]:
  82. results.append(
  83. {
  84. 'url': result['person']['actor_id'],
  85. 'title': result['person']['name'],
  86. 'content': markdown_to_text(result['person'].get('bio', '')),
  87. }
  88. )
  89. return results
  90. def _get_posts(json):
  91. results = []
  92. for result in json["posts"]:
  93. user = result['creator'].get('display_name', result['creator']['name'])
  94. thumbnail = None
  95. if result['post'].get('thumbnail_url'):
  96. thumbnail = result['post']['thumbnail_url'] + '?format=webp&thumbnail=208'
  97. metadata = (
  98. f"&#x25B2; {result['counts']['upvotes']} &#x25BC; {result['counts']['downvotes']}"
  99. f" | {gettext('user')}: {user}"
  100. f" | {gettext('comments')}: {result['counts']['comments']}"
  101. f" | {gettext('community')}: {result['community']['title']}"
  102. )
  103. content = result['post'].get('body', '').strip()
  104. if content:
  105. content = markdown_to_text(content)
  106. results.append(
  107. {
  108. 'url': result['post']['ap_id'],
  109. 'title': result['post']['name'],
  110. 'content': content,
  111. 'thumbnail': thumbnail,
  112. 'publishedDate': datetime.strptime(result['post']['published'][:19], '%Y-%m-%dT%H:%M:%S'),
  113. 'metadata': metadata,
  114. }
  115. )
  116. return results
  117. def _get_comments(json):
  118. results = []
  119. for result in json["comments"]:
  120. user = result['creator'].get('display_name', result['creator']['name'])
  121. content = result['comment'].get('content', '').strip()
  122. if content:
  123. content = markdown_to_text(content)
  124. metadata = (
  125. f"&#x25B2; {result['counts']['upvotes']} &#x25BC; {result['counts']['downvotes']}"
  126. f" | {gettext('user')}: {user}"
  127. f" | {gettext('community')}: {result['community']['title']}"
  128. )
  129. results.append(
  130. {
  131. 'url': result['comment']['ap_id'],
  132. 'title': result['post']['name'],
  133. 'content': markdown_to_text(result['comment']['content']),
  134. 'publishedDate': datetime.strptime(result['comment']['published'][:19], '%Y-%m-%dT%H:%M:%S'),
  135. 'metadata': metadata,
  136. }
  137. )
  138. return results
  139. def response(resp):
  140. json = resp.json()
  141. if lemmy_type == "Communities":
  142. return _get_communities(json)
  143. if lemmy_type == "Users":
  144. return _get_users(json)
  145. if lemmy_type == "Posts":
  146. return _get_posts(json)
  147. if lemmy_type == "Comments":
  148. return _get_comments(json)
  149. raise ValueError(f"Unsupported lemmy type: {lemmy_type}")