piped.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. # lint: pylint
  3. """An alternative privacy-friendly YouTube frontend which is efficient by
  4. design. `Piped’s architecture`_ consists of 3 components:
  5. - :py:obj:`backend <backend_url>`
  6. - :py:obj:`frontend <frontend_url>`
  7. - proxy
  8. .. _Piped’s architecture: https://docs.piped.video/docs/architecture/
  9. Configuration
  10. =============
  11. The :py:obj:`backend_url` and :py:obj:`frontend_url` has to be set in the engine
  12. named `piped` and are used by all piped engines
  13. .. code:: yaml
  14. - name: piped
  15. engine: piped
  16. piped_filter: videos
  17. ...
  18. frontend_url: https://..
  19. backend_url:
  20. - https://..
  21. - https://..
  22. - name: piped.music
  23. engine: piped
  24. network: piped
  25. shortcut: ppdm
  26. piped_filter: music_songs
  27. ...
  28. """
  29. from __future__ import annotations
  30. import time
  31. import random
  32. from urllib.parse import urlencode
  33. import datetime
  34. from dateutil import parser
  35. # about
  36. about = {
  37. "website": 'https://github.com/TeamPiped/Piped/',
  38. "wikidata_id": 'Q107565255',
  39. "official_api_documentation": 'https://docs.piped.video/docs/api-documentation/',
  40. "use_official_api": True,
  41. "require_api_key": False,
  42. "results": 'JSON',
  43. }
  44. # engine dependent config
  45. categories = []
  46. paging = False
  47. # search-url
  48. backend_url: list | str = "https://pipedapi.kavin.rocks"
  49. """Piped-Backend_: The core component behind Piped. The value is an URL or a
  50. list of URLs. In the latter case instance will be selected randomly. For a
  51. complete list of offical instances see Piped-Instances (`JSON
  52. <https://piped-instances.kavin.rocks/>`__)
  53. .. _Piped-Instances: https://github.com/TeamPiped/Piped/wiki/Instances
  54. .. _Piped-Backend: https://github.com/TeamPiped/Piped-Backend
  55. """
  56. frontend_url: str = "https://piped.video"
  57. """Piped-Frontend_: URL to use as link and for embeds.
  58. .. _Piped-Frontend: https://github.com/TeamPiped/Piped
  59. """
  60. piped_filter = 'all'
  61. """Content filter ``music_songs`` or ``videos``"""
  62. def _backend_url() -> str:
  63. from searx.engines import engines # pylint: disable=import-outside-toplevel
  64. url = engines['piped'].backend_url # type: ignore
  65. if isinstance(url, list):
  66. url = random.choice(url)
  67. return url
  68. def _frontend_url() -> str:
  69. from searx.engines import engines # pylint: disable=import-outside-toplevel
  70. return engines['piped'].frontend_url # type: ignore
  71. def request(query, params):
  72. query = urlencode({'q': query})
  73. params["url"] = _backend_url() + f"/search?{query}&filter={piped_filter}"
  74. return params
  75. def response(resp):
  76. results = []
  77. search_results = resp.json()["items"]
  78. for result in search_results:
  79. publishedDate = parser.parse(time.ctime(result.get("uploaded", 0) / 1000))
  80. item = {
  81. # the api url differs from the frontend, hence use piped.video as default
  82. "url": _frontend_url() + result.get("url", ""),
  83. "title": result.get("title", ""),
  84. "publishedDate": publishedDate,
  85. "iframe_src": _frontend_url() + '/embed' + result.get("url", ""),
  86. }
  87. if piped_filter == 'videos':
  88. item["template"] = "videos.html"
  89. item["content"] = result.get("shortDescription", "")
  90. item["thumbnail"] = result.get("thumbnail", "")
  91. elif piped_filter == 'music_songs':
  92. item["template"] = "default.html"
  93. item["img_src"] = result.get("thumbnail", "")
  94. item["content"] = result.get("uploaderName", "")
  95. length = result.get("duration")
  96. if length:
  97. item["length"] = datetime.timedelta(seconds=length)
  98. results.append(item)
  99. return results