From f194259ab3fe57ea2ccb00b2542fc8e7407679fb Mon Sep 17 00:00:00 2001 From: simon Date: Sat, 18 Mar 2023 17:46:51 +0700 Subject: [PATCH] refactor subscribe task backend --- .../home/src/download/subscriptions.py | 26 +++++++++++-------- tubearchivist/home/tasks.py | 6 ++--- tubearchivist/home/views.py | 16 ------------ 3 files changed, 18 insertions(+), 30 deletions(-) diff --git a/tubearchivist/home/src/download/subscriptions.py b/tubearchivist/home/src/download/subscriptions.py index 41cf1821..4bb6f689 100644 --- a/tubearchivist/home/src/download/subscriptions.py +++ b/tubearchivist/home/src/download/subscriptions.py @@ -319,17 +319,23 @@ class SubscriptionScanner: class SubscriptionHandler: """subscribe to channels and playlists from url_str""" - def __init__(self, url_str): + def __init__(self, url_str, task=False): self.url_str = url_str + self.task = task self.to_subscribe = False def subscribe(self): """subscribe to url_str items""" + if self.task: + self.task.send_progress(["Processing form content."]) self.to_subscribe = Parser(self.url_str).parse() + total = len(self.to_subscribe) for idx, item in enumerate(self.to_subscribe): + if self.task: + self._notify(idx, item, total) + self.subscribe_type(item) - self._notify(idx) def subscribe_type(self, item): """process single item""" @@ -354,13 +360,11 @@ class SubscriptionHandler: channel_id, channel_subscribed=True ) - def _notify(self, idx): + def _notify(self, idx, item, total): """send notification message to redis""" - key = "message:subchannel" - message = { - "status": key, - "level": "info", - "title": "Subscribing", - "message": f"Processing {idx + 1} of {len(self.to_subscribe)}", - } - RedisArchivist().set_message(key, message=message, expire=True) + subscribe_type = item["type"].title() + message_lines = [ + f"Subscribe to {subscribe_type}", + f"Progress: {idx + 1}/{total}", + ] + self.task.send_progress(message_lines, progress=(idx + 1) / total) diff --git a/tubearchivist/home/tasks.py b/tubearchivist/home/tasks.py index 66f9465c..6eebf1df 100644 --- a/tubearchivist/home/tasks.py +++ b/tubearchivist/home/tasks.py @@ -309,10 +309,10 @@ def re_sync_thumbs(self): ThumbFilesystem(task=self).embed() -@shared_task(name="subscribe_to") -def subscribe_to(url_str): +@shared_task(bind=True, name="subscribe_to", base=BaseTask) +def subscribe_to(self, url_str): """take a list of urls to subscribe to""" - SubscriptionHandler(url_str).subscribe() + SubscriptionHandler(url_str, task=self).subscribe() @shared_task(bind=True, name="index_playlists", base=BaseTask) diff --git a/tubearchivist/home/views.py b/tubearchivist/home/views.py index e2ccc6ab..9f9886a2 100644 --- a/tubearchivist/home/views.py +++ b/tubearchivist/home/views.py @@ -754,14 +754,6 @@ class ChannelView(ArchivistResultsView): """handle http post requests""" subscribe_form = SubscribeToChannelForm(data=request.POST) if subscribe_form.is_valid(): - key = "message:subchannel" - message = { - "status": key, - "level": "info", - "title": "Subscribing to Channels", - "message": "Parsing form data", - } - RedisArchivist().set_message(key, message=message, expire=True) url_str = request.POST.get("subscribe") print(url_str) subscribe_to.delay(url_str) @@ -907,14 +899,6 @@ class PlaylistView(ArchivistResultsView): if subscribe_form.is_valid(): url_str = request.POST.get("subscribe") print(url_str) - key = "message:subplaylist" - message = { - "status": key, - "level": "info", - "title": "Subscribing to Playlists", - "message": "Parsing form data", - } - RedisArchivist().set_message(key, message=message, expire=True) subscribe_to.delay(url_str) sleep(1)