move add to queue to api call

This commit is contained in:
simon 2023-04-22 11:57:09 +07:00
parent 790ba3d20e
commit 76535c6304
No known key found for this signature in database
GPG Key ID: 2C15AA5E89985DD4
4 changed files with 25 additions and 34 deletions

View File

@ -25,6 +25,7 @@ from home.src.index.reindex import Reindex, ReindexManual, ReindexPopulate
from home.src.ta.config import AppConfig, ReleaseVersion, ScheduleBuilder
from home.src.ta.ta_redis import RedisArchivist
from home.src.ta.task_manager import TaskManager
from home.src.ta.urlparser import Parser
CONFIG = AppConfig().config
REDIS_HOST = os.environ.get("REDIS_HOST")
@ -196,7 +197,12 @@ def download_pending(self, auto_only=False):
def extrac_dl(self, youtube_ids, auto_start=False):
"""parse list passed and add to pending"""
TaskManager().init(self)
pending_handler = PendingList(youtube_ids=youtube_ids, task=self)
if isinstance(youtube_ids, str):
to_add = Parser(youtube_ids).parse()
else:
to_add = youtube_ids
pending_handler = PendingList(youtube_ids=to_add, task=self)
pending_handler.parse_url_list()
pending_handler.add_to_pending(auto_start=auto_start)

View File

@ -20,10 +20,10 @@
<img id="animate-icon" onclick="showForm()" src="{% static 'img/icon-add.svg' %}" alt="add-icon">
<p>Add to download queue</p>
<div class="show-form">
<form id='hidden-form' action="/downloads/" method="post">
<form id='hidden-form'>
{% csrf_token %}
{{ add_form }}
<button type="submit">Add to download queue</button>
<button onclick="addToQueue()">Add to download queue</button>
</form>
</div>
</div>

View File

@ -41,8 +41,7 @@ from home.src.index.video_constants import VideoTypeEnum
from home.src.ta.config import AppConfig, ReleaseVersion, ScheduleBuilder
from home.src.ta.helper import time_parser
from home.src.ta.ta_redis import RedisArchivist
from home.src.ta.urlparser import Parser
from home.tasks import extrac_dl, index_channel_playlists, subscribe_to
from home.tasks import index_channel_playlists, subscribe_to
from rest_framework.authtoken.models import Token
@ -368,7 +367,7 @@ class AboutView(MinView):
class DownloadView(ArchivistResultsView):
"""resolves to /download/
takes POST for downloading youtube links
handle the download queue
"""
view_origin = "downloads"
@ -452,34 +451,6 @@ class DownloadView(ArchivistResultsView):
return buckets_sorted
@staticmethod
def post(request):
"""handle post requests"""
to_queue = AddToQueueForm(data=request.POST)
if to_queue.is_valid():
url_str = request.POST.get("vid_url")
print(url_str)
try:
youtube_ids = Parser(url_str).parse()
except ValueError:
# failed to process
key = "message:add"
print(f"failed to parse: {url_str}")
mess_dict = {
"status": key,
"level": "error",
"title": "Failed to extract links.",
"message": "Not a video, channel or playlist ID or URL",
}
RedisArchivist().set_message(key, mess_dict, expire=True)
return redirect("downloads")
print(youtube_ids)
extrac_dl.delay(youtube_ids)
sleep(2)
return redirect("downloads", permanent=True)
class ChannelIdBaseView(ArchivistResultsView):
"""base class for all channel-id views"""

View File

@ -160,6 +160,20 @@ function dlPending() {
}, 500);
}
function addToQueue() {
let textArea = document.getElementById('id_vid_url');
if (textArea.value === '') {
return
}
let toPost = {data: [{youtube_id: textArea.value, status: 'pending'}]};
let apiEndpoint = '/api/download/';
apiRequest(apiEndpoint, 'POST', toPost);
textArea.value = "";
setTimeout(function () {
checkMessages();
}, 500);
}
function toIgnore(button) {
let youtube_id = button.getAttribute('data-id');
let apiEndpoint = '/api/download/' + youtube_id + '/';