invidious.py 3.2 KB

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