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. 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(
  40. query=quote_plus(query)
  41. ) + "&page={pageno}".format(pageno=params["pageno"])
  42. if params["time_range"] in time_range_dict:
  43. params["url"] += "&date={timerange}".format(
  44. timerange=time_range_dict[params["time_range"]]
  45. )
  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. # get response from search-request
  52. def response(resp):
  53. results = []
  54. search_results = resp.json()
  55. embedded_url = (
  56. '<iframe width="540" height="304" '
  57. + 'data-src="'
  58. + base_url_rand
  59. + 'embed/{videoid}" '
  60. + 'frameborder="0" allowfullscreen></iframe>'
  61. )
  62. base_invidious_url = base_url_rand + "watch?v="
  63. for result in search_results:
  64. rtype = result.get("type", None)
  65. if rtype == "video":
  66. videoid = result.get("videoId", None)
  67. if not videoid:
  68. continue
  69. url = base_invidious_url + videoid
  70. embedded = embedded_url.format(videoid=videoid)
  71. thumbs = result.get("videoThumbnails", [])
  72. thumb = next(
  73. (th for th in thumbs if th["quality"] == "sddefault"), None
  74. )
  75. if thumb:
  76. thumbnail = thumb.get("url", "")
  77. else:
  78. thumbnail = ""
  79. publishedDate = parser.parse(
  80. time.ctime(result.get("published", 0))
  81. )
  82. length = time.gmtime(result.get("lengthSeconds"))
  83. if length.tm_hour:
  84. length = time.strftime("%H:%M:%S", length)
  85. else:
  86. length = time.strftime("%M:%S", length)
  87. results.append(
  88. {
  89. "url": url,
  90. "title": result.get("title", ""),
  91. "content": result.get("description", ""),
  92. 'length': length,
  93. "template": "videos.html",
  94. "author": result.get("author"),
  95. "publishedDate": publishedDate,
  96. "embedded": embedded,
  97. "thumbnail": thumbnail,
  98. }
  99. )
  100. return results