refactor subscribe task backend

This commit is contained in:
simon 2023-03-18 17:46:51 +07:00
parent 3b9d083f5e
commit f194259ab3
No known key found for this signature in database
GPG Key ID: 2C15AA5E89985DD4
3 changed files with 18 additions and 30 deletions

View File

@ -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)

View File

@ -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)

View File

@ -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)