validate subscribe form and handover to scheduler, #75

This commit is contained in:
simon 2021-10-30 15:00:10 +07:00
parent aebf44ee7e
commit 45c2215dc2
4 changed files with 50 additions and 38 deletions

View File

@ -98,3 +98,17 @@ class AddToQueueForm(forms.Form):
} }
), ),
) )
class SubscribeToChannelForm(forms.Form):
"""text area form to subscribe to multiple channels"""
subscribe = forms.CharField(
label=False,
widget=forms.Textarea(
attrs={
"rows": 3,
"placeholder": "Input channel ID, URL or Video of a channel",
}
),
)

View File

@ -166,3 +166,22 @@ def kill_dl(task_id):
def rescan_filesystem(): def rescan_filesystem():
"""check the media folder for mismatches""" """check the media folder for mismatches"""
scan_filesystem() scan_filesystem()
@shared_task
def subscribe_to(youtube_ids):
"""take a list of urls to subscribe to"""
for youtube_id in youtube_ids:
if youtube_id["type"] == "video":
to_sub = youtube_id["url"]
vid_details = PendingList().get_youtube_details(to_sub)
channel_id_sub = vid_details["channel_id"]
elif youtube_id["type"] == "channel":
channel_id_sub = youtube_id["url"]
else:
raise ValueError("failed to subscribe to: " + youtube_id)
ChannelSubscription().change_subscribe(
channel_id_sub, channel_subscribed=True
)
print("subscribed to: " + channel_id_sub)

View File

@ -12,7 +12,7 @@
<div class="show-form"> <div class="show-form">
<form id="hidden-form" action="/channel/" method="post"> <form id="hidden-form" action="/channel/" method="post">
{% csrf_token %} {% csrf_token %}
<textarea rows="3" placeholder="Input channel ID, channel URL or Video of a channel" id="subscribe" name="subscribe"></textarea> {{ subscribe_form }}
<button type="submit">Subscribe</button> <button type="submit">Subscribe</button>
</form> </form>
</div> </div>
@ -24,7 +24,6 @@
</div> </div>
<form onSubmit="return channelRedirect();" id="search-box"> <form onSubmit="return channelRedirect();" id="search-box">
{% csrf_token %} {% csrf_token %}
<!-- <input name="videoSearch" list="resultBox" type="text" id="searchInput" autocomplete="off" oninput="searchChannels(this.value)"> -->
{{ search_form }} {{ search_form }}
<datalist id="resultBox"> <datalist id="resultBox">
</datalist> </datalist>

View File

@ -20,6 +20,7 @@ from home.forms import (
ApplicationSettingsForm, ApplicationSettingsForm,
ChannelSearchForm, ChannelSearchForm,
CustomAuthForm, CustomAuthForm,
SubscribeToChannelForm,
UserSettingsForm, UserSettingsForm,
VideoSearchForm, VideoSearchForm,
) )
@ -37,6 +38,7 @@ from home.tasks import (
run_backup, run_backup,
run_manual_import, run_manual_import,
run_restore_backup, run_restore_backup,
subscribe_to,
update_subscribed, update_subscribed,
) )
@ -466,13 +468,12 @@ class ChannelView(View):
view_config = self.read_config(user_id=user_id) view_config = self.read_config(user_id=user_id)
page_get = int(request.GET.get("page", 0)) page_get = int(request.GET.get("page", 0))
pagination_handler = Pagination(page_get, user_id) pagination_handler = Pagination(page_get, user_id)
page_size = pagination_handler.pagination["page_size"]
page_from = pagination_handler.pagination["page_from"]
# get # get
url = view_config["es_url"] + "/ta_channel/_search" url = view_config["es_url"] + "/ta_channel/_search"
data = { data = {
"size": page_size, "size": pagination_handler.pagination["page_size"],
"from": page_from, "from": pagination_handler.pagination["page_from"],
"query": {"match_all": {}}, "query": {"match_all": {}},
"sort": [{"channel_name.keyword": {"order": "asc"}}], "sort": [{"channel_name.keyword": {"order": "asc"}}],
} }
@ -480,13 +481,14 @@ class ChannelView(View):
data["query"] = {"term": {"channel_subscribed": {"value": True}}} data["query"] = {"term": {"channel_subscribed": {"value": True}}}
search = SearchHandler(url, data) search = SearchHandler(url, data)
channel_hits = search.get_data() channel_hits = search.get_data()
max_hits = search.max_hits
pagination_handler.validate(search.max_hits) pagination_handler.validate(search.max_hits)
search_form = ChannelSearchForm() search_form = ChannelSearchForm()
subscribe_form = SubscribeToChannelForm()
context = { context = {
"search_form": search_form, "search_form": search_form,
"subscribe_form": subscribe_form,
"channels": channel_hits, "channels": channel_hits,
"max_hits": max_hits, "max_hits": search.max_hits,
"pagination": pagination_handler.pagination, "pagination": pagination_handler.pagination,
"show_subed_only": view_config["show_subed_only"], "show_subed_only": view_config["show_subed_only"],
"title": "Channels", "title": "Channels",
@ -516,41 +518,19 @@ class ChannelView(View):
return view_config return view_config
def post(self, request): @staticmethod
def post(request):
"""handle http post requests""" """handle http post requests"""
subscriptions_post = dict(request.POST) subscribe_form = SubscribeToChannelForm(data=request.POST)
print(subscriptions_post) if subscribe_form.is_valid():
subscriptions_post = dict(request.POST) vid_url_list = [request.POST.get("subscribe")]
if "subscribe" in subscriptions_post.keys(): youtube_ids = process_url_list(vid_url_list)
sub_str = subscriptions_post["subscribe"] print(youtube_ids)
try: subscribe_to.delay(youtube_ids)
youtube_ids = process_url_list(sub_str)
self.subscribe_to(youtube_ids)
except ValueError:
print("parsing subscribe ids failed!")
print(sub_str)
sleep(1) sleep(1)
return redirect("channel", permanent=True) return redirect("channel", permanent=True)
@staticmethod
def subscribe_to(youtube_ids):
"""process the subscribe ids"""
for youtube_id in youtube_ids:
if youtube_id["type"] == "video":
to_sub = youtube_id["url"]
vid_details = PendingList().get_youtube_details(to_sub)
channel_id_sub = vid_details["channel_id"]
elif youtube_id["type"] == "channel":
channel_id_sub = youtube_id["url"]
else:
raise ValueError("failed to subscribe to: " + youtube_id)
ChannelSubscription().change_subscribe(
channel_id_sub, channel_subscribed=True
)
print("subscribed to: " + channel_id_sub)
class VideoView(View): class VideoView(View):
"""resolves to /video/<video-id>/ """resolves to /video/<video-id>/