transform progress message to list, change subscribe messages

This commit is contained in:
simon 2021-12-03 19:00:26 +07:00
parent 5a2d7c07bf
commit 1691bdadf5
No known key found for this signature in database
GPG Key ID: 2C15AA5E89985DD4
5 changed files with 61 additions and 26 deletions

View File

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

View File

@ -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"""
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 + "progress:download"
"JSON.GET", self.NAME_SPACE + key
)
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}
all_messages.append(json_str)
return json_str
return all_messages
@staticmethod
def monitor_cache_dir(cache_dir):

View File

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

View File

@ -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/<slug:channel_id_detail>/",

View File

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