add subscribe to playlist form and better task management

This commit is contained in:
simon 2021-11-19 11:52:27 +07:00
parent fcddf85245
commit 3881d305f9
No known key found for this signature in database
GPG Key ID: 2C15AA5E89985DD4
4 changed files with 68 additions and 26 deletions

View File

@ -121,3 +121,17 @@ class SubscribeToChannelForm(forms.Form):
}
),
)
class SubscribeToPlaylistForm(forms.Form):
"""text area form to subscribe to multiple playlists"""
subscribe = forms.CharField(
label=False,
widget=forms.Textarea(
attrs={
"rows": 3,
"placeholder": "Input playlist IDs",
}
),
)

View File

@ -375,6 +375,40 @@ class PlaylistSubscription:
return all_playlists
def process_url_str(self, new_playlists):
"""process playlist subscribe form url_str"""
all_indexed = PendingList().get_all_indexed()
all_youtube_ids = [i["youtube_id"] for i in all_indexed]
new_thumbs = []
for playlist in new_playlists:
url_type = playlist["type"]
playlist_id = playlist["url"]
if not url_type == "playlist":
print(f"{playlist_id} not a playlist, skipping...")
continue
playlist_handler = YoutubePlaylist(
playlist_id, all_youtube_ids=all_youtube_ids
)
if not playlist_handler.get_es_playlist():
playlist_handler.get_playlist_dict()
playlist_handler.playlist_dict["playlist_subscribed"] = True
playlist_handler.upload_to_es()
playlist_handler.add_vids_to_playlist()
thumb = playlist_handler.playlist_dict["playlist_thumbnail"]
new_thumbs.append((playlist_id, thumb))
else:
self.change_subscribe(playlist_id, subscribe_status=True)
# notify
RedisArchivist().set_message(
"progress:subscribe", {"status": "subscribing"}
)
return new_thumbs
def change_subscribe(self, playlist_id, subscribe_status):
"""change the subscribe status of a playlist"""
es_url = self.config["application"]["es_url"]

View File

@ -244,31 +244,11 @@ def index_channel_playlists(channel_id):
@shared_task
def subscribe_to_playlist(url_str):
"""process url string to subscribe to playlists"""
RedisArchivist().set_message(
"progress:subscribe", {"status": "subscribing"}
)
new_playlists = UrlListParser(url_str).process_list()
all_indexed = PendingList().get_all_indexed()
all_youtube_ids = [i["youtube_id"] for i in all_indexed]
for playlist in new_playlists:
url_type = playlist["type"]
playlist_id = playlist["url"]
if not url_type == "playlist":
print(f"{playlist_id} not a playlist, skipping...")
continue
playlist_handler = YoutubePlaylist(
playlist_id, all_youtube_ids=all_youtube_ids
)
if not playlist_handler.get_es_playlist():
playlist_handler.get_playlist_dict()
playlist_handler.playlist_dict["playlist_subscribed"] = True
playlist_handler.upload_to_es()
playlist_handler.add_vids_to_playlist()
else:
PlaylistSubscription().change_subscribe(
playlist_id, subscribe_status=True
)
if new_playlists:
handler = ThumbManager()
missing_playlists = handler.get_missing_playlists()
handler.download_playlist(missing_playlists)
new_thumbs = PlaylistSubscription().process_url_str(new_playlists)
if new_thumbs:
ThumbManager().download_playlist(new_thumbs)

View File

@ -22,6 +22,7 @@ from home.forms import (
CustomAuthForm,
PlaylistSearchForm,
SubscribeToChannelForm,
SubscribeToPlaylistForm,
UserSettingsForm,
VideoSearchForm,
)
@ -47,6 +48,7 @@ from home.tasks import (
run_manual_import,
run_restore_backup,
subscribe_to,
subscribe_to_playlist,
update_subscribed,
)
@ -737,8 +739,10 @@ class PlaylistView(View):
playlist_hits = search.get_data()
pagination_handler.validate(search.max_hits)
search_form = PlaylistSearchForm()
subscribe_form = SubscribeToChannelForm()
context = {
"subscribe_form": subscribe_form,
"search_form": search_form,
"title": "Playlists",
"colors": view_config["colors"],
@ -746,6 +750,7 @@ class PlaylistView(View):
"pagination": pagination_handler.pagination,
"playlists": playlist_hits,
"view_style": view_config["view_style"],
"running": view_config["running"],
}
return render(request, "home/playlist.html", context)
@ -796,12 +801,14 @@ class PlaylistView(View):
sub_only_key = f"{user_id}:show_subed_only"
show_subed_only = RedisArchivist().get_message(sub_only_key)["status"]
running = RedisArchivist().get_message("progress:subscribe")["status"]
view_config = {
"es_url": config_handler.config["application"]["es_url"],
"colors": config_handler.colors,
"view_style": view_style,
"show_subed_only": show_subed_only,
"running": running,
}
return view_config
@ -815,6 +822,13 @@ class PlaylistView(View):
search_url = "/playlist/?" + urlencode({"search": search_query})
return redirect(search_url, permanent=True)
subscribe_form = SubscribeToPlaylistForm(data=request.POST)
if subscribe_form.is_valid():
url_str = request.POST.get("subscribe")
print(url_str)
subscribe_to_playlist.delay(url_str)
sleep(1)
return redirect("playlist")