summaryrefslogtreecommitdiff
path: root/searx/engines/invidious.py
diff options
context:
space:
mode:
authorcy8aer <cybaer42@web.de>2019-08-02 13:25:25 +0200
committerAlexandre Flament <alex@al-f.net>2019-08-02 13:25:25 +0200
commit4dc792e1e2d3771b6d6620f5d564ea091597c4dc (patch)
tree61a15842069923e990db2c80d4698300cb775d94 /searx/engines/invidious.py
parent02bbbf59d6a0fba85b48ba0ab805d5457db1025d (diff)
downloadsearxng-4dc792e1e2d3771b6d6620f5d564ea091597c4dc.tar.gz
searxng-4dc792e1e2d3771b6d6620f5d564ea091597c4dc.zip
[enh] add invidious engine. (#1657)
closes #1372
Diffstat (limited to 'searx/engines/invidious.py')
-rw-r--r--searx/engines/invidious.py100
1 files changed, 100 insertions, 0 deletions
diff --git a/searx/engines/invidious.py b/searx/engines/invidious.py
new file mode 100644
index 000000000..8d81691fc
--- /dev/null
+++ b/searx/engines/invidious.py
@@ -0,0 +1,100 @@
+# Invidious (Videos)
+#
+# @website https://invidio.us/
+# @provide-api yes (https://github.com/omarroth/invidious/wiki/API)
+#
+# @using-api yes
+# @results JSON
+# @stable yes
+# @parse url, title, content, publishedDate, thumbnail, embedded
+
+from searx.url_utils import quote_plus
+from dateutil import parser
+import time
+
+# engine dependent config
+categories = ["videos", "music"]
+paging = True
+language_support = True
+time_range_support = True
+
+# search-url
+base_url = "https://invidio.us/"
+
+
+# do search-request
+def request(query, params):
+ time_range_dict = {
+ "day": "today",
+ "week": "week",
+ "month": "month",
+ "year": "year",
+ }
+ search_url = base_url + "api/v1/search?q={query}"
+ params["url"] = search_url.format(
+ query=quote_plus(query)
+ ) + "&page={pageno}".format(pageno=params["pageno"])
+
+ if params["time_range"] in time_range_dict:
+ params["url"] += "&date={timerange}".format(
+ timerange=time_range_dict[params["time_range"]]
+ )
+
+ if params["language"] != "all":
+ lang = params["language"].split("-")
+ if len(lang) == 2:
+ params["url"] += "&range={lrange}".format(lrange=lang[1])
+
+ return params
+
+
+# get response from search-request
+def response(resp):
+ results = []
+
+ search_results = resp.json()
+ embedded_url = (
+ '<iframe width="540" height="304" '
+ + 'data-src="'
+ + base_url
+ + 'embed/{videoid}" '
+ + 'frameborder="0" allowfullscreen></iframe>'
+ )
+
+ base_invidious_url = base_url + "watch?v="
+
+ for result in search_results:
+ rtype = result.get("type", None)
+ if rtype == "video":
+ videoid = result.get("videoId", None)
+ if not videoid:
+ continue
+
+ url = base_invidious_url + videoid
+ embedded = embedded_url.format(videoid=videoid)
+ thumbs = result.get("videoThumbnails", [])
+ thumb = next(
+ (th for th in thumbs if th["quality"] == "sddefault"), None
+ )
+ if thumb:
+ thumbnail = thumb.get("url", "")
+ else:
+ thumbnail = ""
+
+ publishedDate = parser.parse(
+ time.ctime(result.get("published", 0))
+ )
+
+ results.append(
+ {
+ "url": url,
+ "title": result.get("title", ""),
+ "content": result.get("description", ""),
+ "template": "videos.html",
+ "publishedDate": publishedDate,
+ "embedded": embedded,
+ "thumbnail": thumbnail,
+ }
+ )
+
+ return results