invidious.py 3.2 KB

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