mirror of
https://github.com/tubearchivist/tubearchivist
synced 2024-11-02 09:41:07 +00:00
refactor subscribe task backend
This commit is contained in:
parent
3b9d083f5e
commit
f194259ab3
@ -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)
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user