lemmy.py 5.6 KB

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