invidious.py 2.8 KB

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