invidious.py 2.7 KB

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