invidious.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. """Invidious (Videos)
  3. If you want to use invidious with SearXNG you should setup one locally.
  4. No public instance offer a public API now
  5. - https://github.com/searxng/searxng/issues/2722#issuecomment-2884993248
  6. """
  7. from __future__ import annotations
  8. import time
  9. import random
  10. from urllib.parse import quote_plus, urlparse
  11. from dateutil import parser
  12. from searx.utils import humanize_number
  13. # about
  14. about = {
  15. "website": 'https://api.invidious.io/',
  16. "wikidata_id": 'Q79343316',
  17. "official_api_documentation": 'https://docs.invidious.io/api/',
  18. "use_official_api": True,
  19. "require_api_key": False,
  20. "results": 'JSON',
  21. }
  22. # engine dependent config
  23. categories = ["videos", "music"]
  24. paging = True
  25. time_range_support = True
  26. # base_url can be overwritten by a list of URLs in the settings.yml
  27. base_url: list | str = []
  28. def init(_):
  29. if not base_url:
  30. raise ValueError("missing invidious base_url")
  31. def request(query, params):
  32. time_range_dict = {
  33. "day": "today",
  34. "week": "week",
  35. "month": "month",
  36. "year": "year",
  37. }
  38. if isinstance(base_url, list):
  39. params["base_url"] = random.choice(base_url)
  40. else:
  41. params["base_url"] = base_url
  42. search_url = params["base_url"] + "/api/v1/search?q={query}"
  43. params["url"] = search_url.format(query=quote_plus(query)) + "&page={pageno}".format(pageno=params["pageno"])
  44. if params["time_range"] in time_range_dict:
  45. params["url"] += "&date={timerange}".format(timerange=time_range_dict[params["time_range"]])
  46. if params["language"] != "all":
  47. lang = params["language"].split("-")
  48. if len(lang) == 2:
  49. params["url"] += "&range={lrange}".format(lrange=lang[1])
  50. return params
  51. def response(resp):
  52. results = []
  53. search_results = resp.json()
  54. base_invidious_url = resp.search_params['base_url'] + "/watch?v="
  55. for result in search_results:
  56. rtype = result.get("type", None)
  57. if rtype == "video":
  58. videoid = result.get("videoId", None)
  59. if not videoid:
  60. continue
  61. url = base_invidious_url + videoid
  62. thumbs = result.get("videoThumbnails", [])
  63. thumb = next((th for th in thumbs if th["quality"] == "sddefault"), None)
  64. if thumb:
  65. thumbnail = thumb.get("url", "")
  66. else:
  67. thumbnail = ""
  68. # some instances return a partial thumbnail url
  69. # we check if the url is partial, and prepend the base_url if it is
  70. if thumbnail and not urlparse(thumbnail).netloc:
  71. thumbnail = resp.search_params['base_url'] + thumbnail
  72. publishedDate = parser.parse(time.ctime(result.get("published", 0)))
  73. length = time.gmtime(result.get("lengthSeconds"))
  74. if length.tm_hour:
  75. length = time.strftime("%H:%M:%S", length)
  76. else:
  77. length = time.strftime("%M:%S", length)
  78. results.append(
  79. {
  80. "url": url,
  81. "title": result.get("title", ""),
  82. "content": result.get("description", ""),
  83. "length": length,
  84. "views": humanize_number(result['viewCount']),
  85. "template": "videos.html",
  86. "author": result.get("author"),
  87. "publishedDate": publishedDate,
  88. "iframe_src": resp.search_params['base_url'] + '/embed/' + videoid,
  89. "thumbnail": thumbnail,
  90. }
  91. )
  92. return results