invidious.py 3.2 KB

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