From 40bb3e880ecc18a180144051f32f11db0cf00597 Mon Sep 17 00:00:00 2001 From: simon Date: Sun, 17 Apr 2022 19:15:40 +0700 Subject: [PATCH 1/2] API: implement status update and delete of item in queue --- tubearchivist/api/README.md | 19 ++++++++++++++++++- tubearchivist/api/views.py | 29 ++++++++++++++++++++++++++++- 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/tubearchivist/api/README.md b/tubearchivist/api/README.md index f6e10f4..2067331 100644 --- a/tubearchivist/api/README.md +++ b/tubearchivist/api/README.md @@ -149,8 +149,25 @@ POST /api/download/ ``` ## Download Queue Item View -/api/download/\/ +GET /api/download/\/ +POST /api/download/\/ +Ignore video in download queue: +```json +{ + "status": "ignore" +} +``` + +Add to queue previously ignored video: +```json +{ + "status": "pending" +} +``` + +DELETE /api/download/\/ +Forget or delete from download queue ## Ping View Validate your connection with the API diff --git a/tubearchivist/api/views.py b/tubearchivist/api/views.py index 7cd7a2a..a610c56 100644 --- a/tubearchivist/api/views.py +++ b/tubearchivist/api/views.py @@ -1,11 +1,12 @@ """all API views""" from api.src.search_processor import SearchProcess +from home.src.download.queue import PendingInteract from home.src.es.connect import ElasticWrap from home.src.index.video import SponsorBlock from home.src.ta.config import AppConfig from home.src.ta.helper import UrlListParser -from home.src.ta.ta_redis import RedisArchivist +from home.src.ta.ta_redis import RedisArchivist, RedisQueue from home.tasks import extrac_dl, subscribe_to from rest_framework.authentication import ( SessionAuthentication, @@ -295,9 +296,12 @@ class PlaylistApiVideoView(ApiBaseView): class DownloadApiView(ApiBaseView): """resolves to /api/download// GET: returns metadata dict of an item in the download queue + POST: update status of item to pending or ignore + DELETE: forget from download queue """ search_base = "ta_download/_doc/" + valid_status = ["pending", "ignore"] def get(self, request, video_id): # pylint: disable=unused-argument @@ -305,6 +309,29 @@ class DownloadApiView(ApiBaseView): self.get_document(video_id) return Response(self.response, status=self.status_code) + def post(self, request, video_id): + """post to video to change status""" + item_status = request.data["status"] + if item_status not in self.valid_status: + message = f"{video_id}: invalid status {item_status}" + print(message) + return Response({"message": message}, status=400) + + print(f"{video_id}: change status to {item_status}") + PendingInteract(video_id=video_id, status=item_status).update_status() + RedisQueue().clear_item(video_id) + + return Response(request.data) + + @staticmethod + def delete(request, video_id): + # pylint: disable=unused-argument + """delete single video from queue""" + print(f"{video_id}: delete from queue") + PendingInteract(video_id=video_id).delete_item() + + return Response({"success": True}) + class DownloadApiListView(ApiBaseView): """resolves to /api/download/ From d086f63861b78b4f6a3309ce6a16c3ca015eb4ff Mon Sep 17 00:00:00 2001 From: simon Date: Sun, 17 Apr 2022 20:10:49 +0700 Subject: [PATCH 2/2] API: sort and query filter download view, delete by filter --- tubearchivist/api/README.md | 9 ++++++++- tubearchivist/api/views.py | 30 +++++++++++++++++++++++++++++- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/tubearchivist/api/README.md b/tubearchivist/api/README.md index 2067331..c635b11 100644 --- a/tubearchivist/api/README.md +++ b/tubearchivist/api/README.md @@ -136,7 +136,10 @@ POST /api/channel/ /api/playlist/\/video/ ## Download Queue List View -/api/download/ +GET /api/download/ + +Parameter: +- filter: pending, ignore ### Add list of videos to download queue POST /api/download/ @@ -148,6 +151,10 @@ POST /api/download/ } ``` +### Delete download queue items by filter +DELETE /api/download/?filter=ignore +DELETE /api/download/?filter=pending + ## Download Queue Item View GET /api/download/\/ POST /api/download/\/ diff --git a/tubearchivist/api/views.py b/tubearchivist/api/views.py index a610c56..a30484e 100644 --- a/tubearchivist/api/views.py +++ b/tubearchivist/api/views.py @@ -337,14 +337,28 @@ class DownloadApiListView(ApiBaseView): """resolves to /api/download/ GET: returns latest videos in the download queue POST: add a list of videos to download queue + DELETE: remove items based on query filter """ search_base = "ta_download/_search/" + valid_filter = ["pending", "ignore"] def get(self, request): # pylint: disable=unused-argument """get request""" - data = {"query": {"match_all": {}}} + query_filter = request.GET.get("filter", False) + data = { + "query": {"match_all": {}}, + "sort": [{"timestamp": {"order": "asc"}}], + } + if query_filter: + if query_filter not in self.valid_filter: + message = f"invalid url query filder: {query_filter}" + print(message) + return Response({"message": message}, status=400) + + data["query"] = {"term": {"status": {"value": query_filter}}} + self.get_document_list(data) self.get_paginate() return Response(self.response) @@ -374,6 +388,20 @@ class DownloadApiListView(ApiBaseView): return Response(data) + def delete(self, request): + """delete download queue""" + query_filter = request.GET.get("filter", False) + if query_filter not in self.valid_filter: + message = f"invalid url query filter: {query_filter}" + print(message) + return Response({"message": message}, status=400) + + message = f"delete queue by status: {query_filter}" + print(message) + PendingInteract(status=query_filter).delete_by_status() + + return Response({"message": message}) + class PingView(ApiBaseView): """resolves to /api/ping/