invidious.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. # search-url
  23. base_url = ''
  24. base_url_rand = ''
  25. # do search-request
  26. def request(query, params):
  27. global base_url_rand
  28. time_range_dict = {
  29. "day": "today",
  30. "week": "week",
  31. "month": "month",
  32. "year": "year",
  33. }
  34. if isinstance(base_url, list):
  35. base_url_rand = random.choice(base_url)
  36. else:
  37. base_url_rand = base_url
  38. search_url = base_url_rand + "api/v1/search?q={query}"
  39. params["url"] = search_url.format(query=quote_plus(query)) + "&page={pageno}".format(pageno=params["pageno"])
  40. if params["time_range"] in time_range_dict:
  41. params["url"] += "&date={timerange}".format(timerange=time_range_dict[params["time_range"]])
  42. if params["language"] != "all":
  43. lang = params["language"].split("-")
  44. if len(lang) == 2:
  45. params["url"] += "&range={lrange}".format(lrange=lang[1])
  46. return params
  47. # get response from search-request
  48. def response(resp):
  49. results = []
  50. search_results = resp.json()
  51. embedded_url = (
  52. '<iframe width="540" height="304" '
  53. + 'data-src="'
  54. + base_url_rand
  55. + 'embed/{videoid}" '
  56. + 'frameborder="0" allowfullscreen></iframe>'
  57. )
  58. base_invidious_url = base_url_rand + "watch?v="
  59. for result in search_results:
  60. rtype = result.get("type", None)
  61. if rtype == "video":
  62. videoid = result.get("videoId", None)
  63. if not videoid:
  64. continue
  65. url = base_invidious_url + videoid
  66. embedded = embedded_url.format(videoid=videoid)
  67. thumbs = result.get("videoThumbnails", [])
  68. thumb = next((th for th in thumbs if th["quality"] == "sddefault"), None)
  69. if thumb:
  70. thumbnail = thumb.get("url", "")
  71. else:
  72. thumbnail = ""
  73. publishedDate = parser.parse(time.ctime(result.get("published", 0)))
  74. length = time.gmtime(result.get("lengthSeconds"))
  75. if length.tm_hour:
  76. length = time.strftime("%H:%M:%S", length)
  77. else:
  78. length = time.strftime("%M:%S", length)
  79. results.append(
  80. {
  81. "url": url,
  82. "title": result.get("title", ""),
  83. "content": result.get("description", ""),
  84. 'length': length,
  85. "template": "videos.html",
  86. "author": result.get("author"),
  87. "publishedDate": publishedDate,
  88. "embedded": embedded,
  89. "thumbnail": thumbnail,
  90. }
  91. )
  92. return results