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 home.src.config import AppConfig
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.reindex import (
ManualImport,
@ -169,8 +169,9 @@ def rescan_filesystem():
@shared_task
def subscribe_to(youtube_ids):
def subscribe_to(url_str):
"""take a list of urls to subscribe to"""
youtube_ids = UrlListParser(url_str).process_list()
for youtube_id in youtube_ids:
if youtube_id["type"] == "video":
to_sub = youtube_id["url"]
@ -185,3 +186,7 @@ def subscribe_to(youtube_ids):
channel_id_sub, channel_subscribed=True
)
print("subscribed to: " + channel_id_sub)
# notify
RedisArchivist().set_message(
"progress:subscribe", {"status": "subscribing"}
)

View File

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

View File

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