From 46229db64ce0cfc2340cd66b685ebe6ad5bd0584 Mon Sep 17 00:00:00 2001 From: simon Date: Mon, 2 Jan 2023 16:50:53 +0700 Subject: [PATCH] [API] add download now endpoint, fix vid_type --- tubearchivist/api/README.md | 7 +++++++ tubearchivist/api/views.py | 16 ++++++++++++---- tubearchivist/home/src/download/queue.py | 6 ++++++ tubearchivist/home/src/frontend/api_calls.py | 12 ------------ tubearchivist/home/tasks.py | 11 ++++++++--- tubearchivist/static/script.js | 4 ++-- 6 files changed, 35 insertions(+), 21 deletions(-) diff --git a/tubearchivist/api/README.md b/tubearchivist/api/README.md index 1a95fb8c..2328f685 100644 --- a/tubearchivist/api/README.md +++ b/tubearchivist/api/README.md @@ -221,6 +221,13 @@ Add to queue previously ignored video: } ``` +Download existing video now: +```json +{ + "status": "priority" +} +``` + DELETE /api/download/\/ Forget or delete from download queue diff --git a/tubearchivist/api/views.py b/tubearchivist/api/views.py index c99871dc..12b2e196 100644 --- a/tubearchivist/api/views.py +++ b/tubearchivist/api/views.py @@ -15,7 +15,7 @@ from home.src.index.video import SponsorBlock, YoutubeVideo from home.src.ta.config import AppConfig from home.src.ta.helper import UrlListParser from home.src.ta.ta_redis import RedisArchivist, RedisQueue -from home.tasks import check_reindex, extrac_dl, subscribe_to +from home.tasks import check_reindex, download_single, extrac_dl, subscribe_to from rest_framework.authentication import ( SessionAuthentication, TokenAuthentication, @@ -395,7 +395,7 @@ class DownloadApiView(ApiBaseView): """ search_base = "ta_download/_doc/" - valid_status = ["pending", "ignore"] + valid_status = ["pending", "ignore", "priority"] def get(self, request, video_id): # pylint: disable=unused-argument @@ -411,9 +411,17 @@ class DownloadApiView(ApiBaseView): print(message) return Response({"message": message}, status=400) + pending_video, status_code = PendingInteract(video_id).get_item() + if status_code == 404: + message = f"{video_id}: item not found {status_code}" + return Response({"message": message}, status=404) + print(f"{video_id}: change status to {item_status}") - PendingInteract(video_id=video_id, status=item_status).update_status() - RedisQueue(queue_name="dl_queue").clear_item(video_id) + if item_status == "priority": + download_single.delay(pending_video) + else: + PendingInteract(video_id, item_status).update_status() + RedisQueue(queue_name="dl_queue").clear_item(video_id) return Response(request.data) diff --git a/tubearchivist/home/src/download/queue.py b/tubearchivist/home/src/download/queue.py index a3338c9f..5233ffd2 100644 --- a/tubearchivist/home/src/download/queue.py +++ b/tubearchivist/home/src/download/queue.py @@ -117,6 +117,12 @@ class PendingInteract: path = f"ta_download/_update/{self.video_id}" _, _ = ElasticWrap(path).post(data=data) + def get_item(self): + """return pending item dict""" + path = f"ta_download/_doc/{self.video_id}" + response, status_code = ElasticWrap(path).get() + return response["_source"], status_code + class PendingList(PendingIndex): """manage the pending videos list""" diff --git a/tubearchivist/home/src/frontend/api_calls.py b/tubearchivist/home/src/frontend/api_calls.py index 96957921..524f55d6 100644 --- a/tubearchivist/home/src/frontend/api_calls.py +++ b/tubearchivist/home/src/frontend/api_calls.py @@ -13,7 +13,6 @@ from home.src.ta.helper import UrlListParser from home.src.ta.ta_redis import RedisArchivist, RedisQueue from home.tasks import ( download_pending, - download_single, index_channel_playlists, kill_dl, re_sync_thumbs, @@ -56,7 +55,6 @@ class PostData: "sort_order": self._sort_order, "hide_watched": self._hide_watched, "show_subed_only": self._show_subed_only, - "dlnow": self._dlnow, "show_ignored_only": self._show_ignored_only, "manual-import": self._manual_import, "re-embed": self._re_embed, @@ -178,16 +176,6 @@ class PostData: RedisArchivist().set_message(key, message) return {"success": True} - def _dlnow(self): - """start downloading single vid now""" - youtube_id = self.exec_val - print(f"{youtube_id}: downloading now") - running = download_single.delay(youtube_id=youtube_id) - task_id = running.id - print("set task id: " + task_id) - RedisArchivist().set_message("dl_queue_id", task_id) - return {"success": True} - def _show_ignored_only(self): """switch view on /downloads/ to show ignored only""" show_value = self.exec_val diff --git a/tubearchivist/home/tasks.py b/tubearchivist/home/tasks.py index 034f805a..ec08e399 100644 --- a/tubearchivist/home/tasks.py +++ b/tubearchivist/home/tasks.py @@ -6,6 +6,7 @@ Functionality: because tasks are initiated at application start """ +import json import os from celery import Celery, shared_task @@ -107,11 +108,15 @@ def download_pending(): @shared_task -def download_single(youtube_id): +def download_single(pending_video): """start download single video now""" queue = RedisQueue(queue_name="dl_queue") - queue.add_priority(youtube_id) - print("Added to queue with priority: " + youtube_id) + to_add = { + "youtube_id": pending_video["youtube_id"], + "vid_type": pending_video["vid_type"], + } + queue.add_priority(json.dumps(to_add)) + print(f"Added to queue with priority: {to_add}") # start queue if needed have_lock = False my_lock = RedisArchivist().get_lock("downloading") diff --git a/tubearchivist/static/script.js b/tubearchivist/static/script.js index 56174c4e..1028f84a 100644 --- a/tubearchivist/static/script.js +++ b/tubearchivist/static/script.js @@ -193,8 +193,8 @@ function toIgnore(button) { function downloadNow(button) { let youtube_id = button.getAttribute('data-id'); - let payload = JSON.stringify({ dlnow: youtube_id }); - sendPost(payload); + let apiEndpoint = '/api/download/' + youtube_id + '/'; + apiRequest(apiEndpoint, 'POST', { status: 'priority' }); document.getElementById(youtube_id).remove(); setTimeout(function () { checkMessages();