piped.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. """
  10. from __future__ import annotations
  11. import time
  12. import random
  13. from urllib.parse import urlencode
  14. from dateutil import parser
  15. # about
  16. about = {
  17. "website": 'https://github.com/TeamPiped/Piped/',
  18. "wikidata_id": 'Q107565255',
  19. "official_api_documentation": 'https://docs.piped.video/docs/api-documentation/',
  20. "use_official_api": True,
  21. "require_api_key": False,
  22. "results": 'JSON',
  23. }
  24. # engine dependent config
  25. categories = ["videos", "music"]
  26. paging = False
  27. # search-url
  28. backend_url: list|str = "https://pipedapi.kavin.rocks"
  29. """Piped-Backend_: The core component behind Piped. The value is an URL or a
  30. list of URLs. In the latter case instance will be selected randomly. For a
  31. complete list of offical instances see Piped-Instances (`JSON
  32. <https://piped-instances.kavin.rocks/>`__)
  33. .. _Piped-Instances: https://github.com/TeamPiped/Piped/wiki/Instances
  34. .. _Piped-Backend: https://github.com/TeamPiped/Piped-Backend
  35. """
  36. frontend_url: str = "https://piped.video"
  37. """Piped-Frontend_: URL to use as link and for embeds.
  38. .. _Piped-Frontend: https://github.com/TeamPiped/Piped
  39. """
  40. content_filter = 'videos'
  41. """Content filter ``music_albums`` or ``videos``"""
  42. def request(query, params):
  43. if isinstance(backend_url, list):
  44. base_url = random.choice(backend_url)
  45. else:
  46. base_url = backend_url
  47. query = urlencode({'q': query})
  48. params["url"] = base_url + f"/search?{query}&filter={content_filter}"
  49. return params
  50. def response(resp):
  51. results = []
  52. search_results = resp.json()["items"]
  53. for result in search_results:
  54. publishedDate = parser.parse(time.ctime(result.get("uploaded", 0) / 1000))
  55. results.append(
  56. {
  57. # the api url differs from the frontend, hence use piped.video as default
  58. "url": frontend_url + result.get("url", ""),
  59. "title": result.get("title", ""),
  60. "content": result.get("shortDescription", ""),
  61. "template": "videos.html",
  62. "publishedDate": publishedDate,
  63. "iframe_src": frontend_url + '/embed' + result.get("url", ""),
  64. "thumbnail": result.get("thumbnail", ""),
  65. }
  66. )
  67. return results