From 4d9cda3a244991bdd452f7fb102966cc1d880218 Mon Sep 17 00:00:00 2001 From: simon Date: Sun, 31 Oct 2021 17:48:56 +0700 Subject: [PATCH] better subscribe_to task with notification --- tubearchivist/home/tasks.py | 9 ++++++-- .../home/templates/home/channel.html | 22 +++++++++++-------- tubearchivist/home/views.py | 11 +++++++--- 3 files changed, 28 insertions(+), 14 deletions(-) diff --git a/tubearchivist/home/tasks.py b/tubearchivist/home/tasks.py index 20b50b0..5b220a1 100644 --- a/tubearchivist/home/tasks.py +++ b/tubearchivist/home/tasks.py @@ -9,7 +9,7 @@ import os from celery import Celery, shared_task from home.src.config import AppConfig from home.src.download import ChannelSubscription, PendingList, VideoDownloader -from home.src.helper import RedisArchivist, RedisQueue +from home.src.helper import RedisArchivist, RedisQueue, UrlListParser from home.src.index_management import backup_all_indexes, restore_from_backup from home.src.reindex import ( ManualImport, @@ -169,8 +169,9 @@ def rescan_filesystem(): @shared_task -def subscribe_to(youtube_ids): +def subscribe_to(url_str): """take a list of urls to subscribe to""" + youtube_ids = UrlListParser(url_str).process_list() for youtube_id in youtube_ids: if youtube_id["type"] == "video": to_sub = youtube_id["url"] @@ -185,3 +186,7 @@ def subscribe_to(youtube_ids): channel_id_sub, channel_subscribed=True ) print("subscribed to: " + channel_id_sub) + # notify + RedisArchivist().set_message( + "progress:subscribe", {"status": "subscribing"} + ) diff --git a/tubearchivist/home/templates/home/channel.html b/tubearchivist/home/templates/home/channel.html index 9ad90c3..f83c5ea 100644 --- a/tubearchivist/home/templates/home/channel.html +++ b/tubearchivist/home/templates/home/channel.html @@ -7,15 +7,19 @@
- add-icon -

Subscribe to Channels

-
-
- {% csrf_token %} - {{ subscribe_form }} - -
-
+ {% if running == "subscribing" %} +

Subscribing in progress, refresh.

+ {% else %} + add-icon +

Subscribe to Channels

+
+
+ {% csrf_token %} + {{ subscribe_form }} + +
+
+ {% endif %}
diff --git a/tubearchivist/home/views.py b/tubearchivist/home/views.py index 3943b91..a2e6130 100644 --- a/tubearchivist/home/views.py +++ b/tubearchivist/home/views.py @@ -497,6 +497,7 @@ class ChannelView(View): "title": "Channels", "colors": view_config["colors"], "view_style": view_config["view_style"], + "running": view_config["running"], } return render(request, "home/channel.html", context) @@ -506,6 +507,7 @@ class ChannelView(View): config_handler = AppConfig(user_id) view_key = f"{user_id}:view:channel" view_style = RedisArchivist().get_message(view_key)["status"] + running = RedisArchivist().get_message("progress:subscribe")["status"] if not view_style: view_style = config_handler.config["default_view"]["channel"] @@ -517,6 +519,7 @@ class ChannelView(View): "view_style": view_style, "show_subed_only": show_subed_only, "colors": config_handler.colors, + "running": running, } return view_config @@ -526,10 +529,12 @@ class ChannelView(View): """handle http post requests""" subscribe_form = SubscribeToChannelForm(data=request.POST) if subscribe_form.is_valid(): + RedisArchivist().set_message( + "progress:subscribe", {"status": "subscribing"} + ) url_str = request.POST.get("subscribe") - youtube_ids = UrlListParser(url_str).process_list() - print(youtube_ids) - subscribe_to.delay(youtube_ids) + print(url_str) + subscribe_to.delay(url_str) sleep(1) return redirect("channel", permanent=True)