From 1691bdadf5a2ccc709308966293c1adf741c58d6 Mon Sep 17 00:00:00 2001 From: simon Date: Fri, 3 Dec 2021 19:00:26 +0700 Subject: [PATCH] transform progress message to list, change subscribe messages --- tubearchivist/home/src/download.py | 11 ++++++++-- tubearchivist/home/src/helper.py | 32 +++++++++++++++++++----------- tubearchivist/home/tasks.py | 14 +++++++++---- tubearchivist/home/urls.py | 2 +- tubearchivist/home/views.py | 28 +++++++++++++++++++------- 5 files changed, 61 insertions(+), 26 deletions(-) diff --git a/tubearchivist/home/src/download.py b/tubearchivist/home/src/download.py index 72b92b5..fee1cd0 100644 --- a/tubearchivist/home/src/download.py +++ b/tubearchivist/home/src/download.py @@ -402,7 +402,7 @@ class PlaylistSubscription: all_youtube_ids = [i["youtube_id"] for i in all_indexed] new_thumbs = [] - + counter = 1 for playlist in new_playlists: url_type = playlist["type"] playlist_id = playlist["url"] @@ -425,9 +425,16 @@ class PlaylistSubscription: self.change_subscribe(playlist_id, subscribe_status=True) # notify + message = { + "status": "message:subplaylist", + "level": "info", + "title": "Subscribing to Playlists", + "message": f"Processing {counter} of {len(new_playlists)}", + } RedisArchivist().set_message( - "progress:subscribe", {"status": "subscribing"} + "message:subplaylist", message=message ) + counter = counter + 1 return new_thumbs diff --git a/tubearchivist/home/src/helper.py b/tubearchivist/home/src/helper.py index faa5495..aa6fe8a 100644 --- a/tubearchivist/home/src/helper.py +++ b/tubearchivist/home/src/helper.py @@ -155,6 +155,14 @@ class RedisArchivist: REDIS_HOST = os.environ.get("REDIS_HOST") REDIS_PORT = os.environ.get("REDIS_PORT") or 6379 NAME_SPACE = "ta:" + CHANNELS = [ + "download", + "add", + "rescan", + "subchannel", + "subplaylist", + "playlistscan", + ] def __init__(self): self.redis_connection = redis.Redis( @@ -200,19 +208,19 @@ class RedisArchivist: redis_lock = self.redis_connection.lock(self.NAME_SPACE + lock_key) return redis_lock - def get_dl_message(self, cache_dir): - """get latest download progress message if available""" - reply = self.redis_connection.execute_command( - "JSON.GET", self.NAME_SPACE + "progress:download" - ) - if reply: - json_str = json.loads(reply) - elif json_str := self.monitor_cache_dir(cache_dir): - json_str = self.monitor_cache_dir(cache_dir) - else: - json_str = {"status": False} + def get_progress(self): + """get a list of all progress messages""" + all_messages = [] + for channel in self.CHANNELS: + key = "message:" + channel + reply = self.redis_connection.execute_command( + "JSON.GET", self.NAME_SPACE + key + ) + if reply: + json_str = json.loads(reply) + all_messages.append(json_str) - return json_str + return all_messages @staticmethod def monitor_cache_dir(cache_dir): diff --git a/tubearchivist/home/tasks.py b/tubearchivist/home/tasks.py index 560a483..f80f165 100644 --- a/tubearchivist/home/tasks.py +++ b/tubearchivist/home/tasks.py @@ -223,6 +223,7 @@ def re_sync_thumbs(): def subscribe_to(url_str): """take a list of urls to subscribe to""" to_subscribe_list = UrlListParser(url_str).process_list() + counter = 1 for item in to_subscribe_list: to_sub_id = item["url"] if item["type"] == "playlist": @@ -242,10 +243,15 @@ def subscribe_to(url_str): ChannelSubscription().change_subscribe( channel_id_sub, channel_subscribed=True ) - # notify - RedisArchivist().set_message( - "progress:subscribe", {"status": "subscribing"} - ) + # notify for channels + message = { + "status": "message:subchannel", + "level": "info", + "title": "Subscribing to Channels", + "message": f"Processing {counter} of {len(to_subscribe_list)}", + } + RedisArchivist().set_message("message:subchannel", message=message) + counter = counter + 1 @shared_task diff --git a/tubearchivist/home/urls.py b/tubearchivist/home/urls.py index 07e55c2..72d2bc3 100644 --- a/tubearchivist/home/urls.py +++ b/tubearchivist/home/urls.py @@ -34,7 +34,7 @@ urlpatterns = [ ), path("settings/", login_required(SettingsView.as_view()), name="settings"), path("process/", login_required(process), name="process"), - path("downloads/progress/", login_required(progress), name="progress"), + path("progress/", login_required(progress), name="progress"), path("channel/", login_required(ChannelView.as_view()), name="channel"), path( "channel//", diff --git a/tubearchivist/home/views.py b/tubearchivist/home/views.py index 6face69..48ed282 100644 --- a/tubearchivist/home/views.py +++ b/tubearchivist/home/views.py @@ -521,9 +521,13 @@ 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"} - ) + message = { + "status": "message:subchannel", + "level": "info", + "title": "Subscribing to Channels", + "message": "Parsing form data", + } + RedisArchivist().set_message("message:subchannel", message=message) url_str = request.POST.get("subscribe") print(url_str) subscribe_to.delay(url_str) @@ -800,6 +804,15 @@ class PlaylistView(View): if subscribe_form.is_valid(): url_str = request.POST.get("subscribe") print(url_str) + message = { + "status": "message:subplaylist", + "level": "info", + "title": "Subscribing to Playlists", + "message": "Parsing form data", + } + RedisArchivist().set_message( + "message:subplaylist", message=message + ) subscribe_to.delay(url_str) sleep(1) @@ -929,10 +942,11 @@ class SettingsView(View): def progress(request): # pylint: disable=unused-argument - """endpoint for download progress ajax calls""" - config = AppConfig().config - cache_dir = config["application"]["cache_dir"] - json_data = RedisArchivist().get_dl_message(cache_dir) + """resolves to /progress/ + return list of messages for frontend + """ + all_messages = RedisArchivist().get_progress() + json_data = {"messages": all_messages} return JsonResponse(json_data)