From 84dd940ac48d33c253dbabe8de1a1617004c8566 Mon Sep 17 00:00:00 2001 From: Cadence Ember Date: Sat, 8 Aug 2020 02:51:01 +1200 Subject: [PATCH] Add searches --- index.py | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/index.py b/index.py index 4e45181..74077e5 100644 --- a/index.py +++ b/index.py @@ -25,11 +25,15 @@ def length_text_to_seconds(text): class Second(object): def _cp_dispatch(self, vpath): - if vpath[:2] == ["api", "v1"] and len(vpath) >= 4: - endpoints = ["channels", "videos"] + if vpath[:2] == ["api", "v1"]: + endpoints = [ + ["channels", 1, 2], + ["videos", 1, 1], + ["search", 0, 0] + ] for e in endpoints: - if vpath[2] == e: - vpath[:3] = [e] + if vpath[2] == e[0] and len(vpath) >= e[1]+3 and len(vpath) <= e[2]+3: + vpath[:3] = [e[0]] return self return vpath @@ -266,5 +270,29 @@ class Second(object): "identifier": "CHANNEL_DOES_NOT_EXIST" } + @cherrypy.expose + @cherrypy.tools.json_out() + def search(self, *, q, sort_by): + info = ytdl.extract_info("ytsearchall:{}".format(q), download=False) + return list({ + "type": "video", + "title": video["title"], + "videoId": video["id"], + "author": None, + "authorId": None, + "authorUrl": None, + "videoThumbnails": [], + "description": None, + "descriptionHtml": None, + "viewCount": None, + "published": None, + "publishedText": None, + "lengthSeconds": None, + "liveNow": None, + "paid": None, + "premium": None, + "isUpcoming": None + } for video in info["entries"] if "title" in video) + cherrypy.config.update({"server.socket_port": 3000}) cherrypy.quickstart(Second())