invidious.py 3.2 KB

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