better subscribe_to task with notification

This commit is contained in:
simon 2021-10-31 17:48:56 +07:00
parent 283a72308e
commit 4d9cda3a24
3 changed files with 28 additions and 14 deletions

View File

@ -9,7 +9,7 @@ import os
from celery import Celery, shared_task from celery import Celery, shared_task
from home.src.config import AppConfig from home.src.config import AppConfig
from home.src.download import ChannelSubscription, PendingList, VideoDownloader from home.src.download import ChannelSubscription, PendingList, VideoDownloader
from home.src.helper import RedisArchivist, RedisQueue from home.src.helper import RedisArchivist, RedisQueue, UrlListParser
from home.src.index_management import backup_all_indexes, restore_from_backup from home.src.index_management import backup_all_indexes, restore_from_backup
from home.src.reindex import ( from home.src.reindex import (
ManualImport, ManualImport,
@ -169,8 +169,9 @@ def rescan_filesystem():
@shared_task @shared_task
def subscribe_to(youtube_ids): def subscribe_to(url_str):
"""take a list of urls to subscribe to""" """take a list of urls to subscribe to"""
youtube_ids = UrlListParser(url_str).process_list()
for youtube_id in youtube_ids: for youtube_id in youtube_ids:
if youtube_id["type"] == "video": if youtube_id["type"] == "video":
to_sub = youtube_id["url"] to_sub = youtube_id["url"]
@ -185,3 +186,7 @@ def subscribe_to(youtube_ids):
channel_id_sub, channel_subscribed=True channel_id_sub, channel_subscribed=True
) )
print("subscribed to: " + channel_id_sub) print("subscribed to: " + channel_id_sub)
# notify
RedisArchivist().set_message(
"progress:subscribe", {"status": "subscribing"}
)

View File

@ -7,15 +7,19 @@
</div> </div>
<div class="info-box info-box-2"> <div class="info-box info-box-2">
<div class="icon-text"> <div class="icon-text">
<img id="add-icon" onclick="showForm()" src="{% static 'img/icon-add.svg' %}" alt="add-icon"> {% if running == "subscribing" %}
<p>Subscribe to Channels</p> <p>Subscribing in progress, refresh.</p>
<div class="show-form"> {% else %}
<form id="hidden-form" action="/channel/" method="post"> <img id="add-icon" onclick="showForm()" src="{% static 'img/icon-add.svg' %}" alt="add-icon">
{% csrf_token %} <p>Subscribe to Channels</p>
{{ subscribe_form }} <div class="show-form">
<button type="submit">Subscribe</button> <form id="hidden-form" action="/channel/" method="post">
</form> {% csrf_token %}
</div> {{ subscribe_form }}
<button type="submit">Subscribe</button>
</form>
</div>
{% endif %}
</div> </div>
<div class="search-form icon-text"> <div class="search-form icon-text">
<div class="search-icon"> <div class="search-icon">

View File

@ -497,6 +497,7 @@ class ChannelView(View):
"title": "Channels", "title": "Channels",
"colors": view_config["colors"], "colors": view_config["colors"],
"view_style": view_config["view_style"], "view_style": view_config["view_style"],
"running": view_config["running"],
} }
return render(request, "home/channel.html", context) return render(request, "home/channel.html", context)
@ -506,6 +507,7 @@ class ChannelView(View):
config_handler = AppConfig(user_id) config_handler = AppConfig(user_id)
view_key = f"{user_id}:view:channel" view_key = f"{user_id}:view:channel"
view_style = RedisArchivist().get_message(view_key)["status"] view_style = RedisArchivist().get_message(view_key)["status"]
running = RedisArchivist().get_message("progress:subscribe")["status"]
if not view_style: if not view_style:
view_style = config_handler.config["default_view"]["channel"] view_style = config_handler.config["default_view"]["channel"]
@ -517,6 +519,7 @@ class ChannelView(View):
"view_style": view_style, "view_style": view_style,
"show_subed_only": show_subed_only, "show_subed_only": show_subed_only,
"colors": config_handler.colors, "colors": config_handler.colors,
"running": running,
} }
return view_config return view_config
@ -526,10 +529,12 @@ class ChannelView(View):
"""handle http post requests""" """handle http post requests"""
subscribe_form = SubscribeToChannelForm(data=request.POST) subscribe_form = SubscribeToChannelForm(data=request.POST)
if subscribe_form.is_valid(): if subscribe_form.is_valid():
RedisArchivist().set_message(
"progress:subscribe", {"status": "subscribing"}
)
url_str = request.POST.get("subscribe") url_str = request.POST.get("subscribe")
youtube_ids = UrlListParser(url_str).process_list() print(url_str)
print(youtube_ids) subscribe_to.delay(url_str)
subscribe_to.delay(youtube_ids)
sleep(1) sleep(1)
return redirect("channel", permanent=True) return redirect("channel", permanent=True)