From 50006e423c2ecb2766f8c5e1270da32104221578 Mon Sep 17 00:00:00 2001 From: simon Date: Tue, 11 Jan 2022 16:53:02 +0700 Subject: [PATCH] implement channel list and subscribe api --- tubearchivist/api/urls.py | 6 ++++++ tubearchivist/api/views.py | 38 +++++++++++++++++++++++++++++++++++++- 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/tubearchivist/api/urls.py b/tubearchivist/api/urls.py index e7c08a8..d39dc30 100644 --- a/tubearchivist/api/urls.py +++ b/tubearchivist/api/urls.py @@ -1,6 +1,7 @@ """all api urls""" from api.views import ( + ChannelApiListView, ChannelApiView, DownloadApiListView, DownloadApiView, @@ -15,6 +16,11 @@ urlpatterns = [ VideoApiView.as_view(), name="api-video", ), + path( + "channel/", + ChannelApiListView.as_view(), + name="api-channel-list", + ), path( "channel//", ChannelApiView.as_view(), diff --git a/tubearchivist/api/views.py b/tubearchivist/api/views.py index 41df3db..ccbc22f 100644 --- a/tubearchivist/api/views.py +++ b/tubearchivist/api/views.py @@ -3,7 +3,7 @@ import requests from home.src.config import AppConfig from home.src.helper import UrlListParser -from home.tasks import extrac_dl +from home.tasks import extrac_dl, subscribe_to from rest_framework.authentication import ( SessionAuthentication, TokenAuthentication, @@ -92,6 +92,42 @@ class ChannelApiView(ApiBaseView): return Response(self.response, status=self.status_code) +class ChannelApiListView(ApiBaseView): + """resolves to /api/channel/ + GET: returns list of channels + POST: edit a list of channels + """ + + search_base = "/ta_channel/_search/" + + def get(self, request): + # pylint: disable=unused-argument + """get request""" + data = {"query": {"match_all": {}}} + self.config_builder() + self.get_document_list(data) + self.get_paginate() + + return Response(self.response) + + @staticmethod + def post(request): + """subscribe to list of channels""" + data = request.data + try: + to_add = data["data"] + except KeyError: + message = "missing expected data key" + print(message) + return Response({"message": message}, status=400) + + pending = [i["channel_id"] for i in to_add if i["channel_subscribed"]] + url_str = " ".join(pending) + subscribe_to.delay(url_str) + + return Response(data) + + class PlaylistApiView(ApiBaseView): """resolves to /api/playlist// GET: returns metadata dict of playlist