From f1e25c9a203e3c7313321eed0dc1717961ffb2eb Mon Sep 17 00:00:00 2001 From: Simon Date: Thu, 24 Aug 2023 22:46:35 +0700 Subject: [PATCH] [API] add channel search endpoint --- tubearchivist/api/urls.py | 5 +++++ tubearchivist/api/views.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/tubearchivist/api/urls.py b/tubearchivist/api/urls.py index 19c6e192..1af11133 100644 --- a/tubearchivist/api/urls.py +++ b/tubearchivist/api/urls.py @@ -41,6 +41,11 @@ urlpatterns = [ views.ChannelApiListView.as_view(), name="api-channel-list", ), + path( + "channel/search/", + views.ChannelApiSearchView.as_view(), + name="api-channel-search", + ), path( "channel//", views.ChannelApiView.as_view(), diff --git a/tubearchivist/api/views.py b/tubearchivist/api/views.py index 6ae35f4f..4b455b18 100644 --- a/tubearchivist/api/views.py +++ b/tubearchivist/api/views.py @@ -356,6 +356,36 @@ class ChannelApiListView(ApiBaseView): ) +class ChannelApiSearchView(ApiBaseView): + """resolves to /api/channel/search/ + search for channel + """ + + search_base = "ta_channel/_doc/" + + def get(self, request): + """handle get request, search with s parameter""" + + query = request.GET.get("q") + if not query: + message = "missing expected q parameter" + return Response({"message": message, "data": False}, status=400) + + try: + parsed = Parser(query).parse()[0] + except (ValueError, IndexError, AttributeError): + message = f"channel not found: {query}" + return Response({"message": message, "data": False}, status=404) + + if not parsed["type"] == "channel": + message = "expected type channel" + return Response({"message": message, "data": False}, status=400) + + self.get_document(parsed["url"]) + + return Response(self.response, status=self.status_code) + + class ChannelApiVideoView(ApiBaseView): """resolves to /api/channel//video GET: returns a list of videos of channel