invidious.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. # about
  9. about = {
  10. "website": 'https://instances.invidio.us/',
  11. "wikidata_id": 'Q79343316',
  12. "official_api_documentation": 'https://github.com/omarroth/invidious/wiki/API',
  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. language_support = True
  21. time_range_support = True
  22. # search-url
  23. base_url = "https://invidio.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. search_url = base_url + "api/v1/search?q={query}"
  33. params["url"] = search_url.format(
  34. query=quote_plus(query)
  35. ) + "&page={pageno}".format(pageno=params["pageno"])
  36. if params["time_range"] in time_range_dict:
  37. params["url"] += "&date={timerange}".format(
  38. timerange=time_range_dict[params["time_range"]]
  39. )
  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. + base_url
  53. + 'embed/{videoid}" '
  54. + 'frameborder="0" allowfullscreen></iframe>'
  55. )
  56. base_invidious_url = 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(
  67. (th for th in thumbs if th["quality"] == "sddefault"), None
  68. )
  69. if thumb:
  70. thumbnail = thumb.get("url", "")
  71. else:
  72. thumbnail = ""
  73. publishedDate = parser.parse(
  74. time.ctime(result.get("published", 0))
  75. )
  76. length = time.gmtime(result.get("lengthSeconds"))
  77. if length.tm_hour:
  78. length = time.strftime("%H:%M:%S", length)
  79. else:
  80. length = time.strftime("%M:%S", length)
  81. results.append(
  82. {
  83. "url": url,
  84. "title": result.get("title", ""),
  85. "content": result.get("description", ""),
  86. 'length': length,
  87. "template": "videos.html",
  88. "author": result.get("author"),
  89. "publishedDate": publishedDate,
  90. "embedded": embedded,
  91. "thumbnail": thumbnail,
  92. }
  93. )
  94. return results