From 3881d305f9f6817c20d146192d7112fab76e362c Mon Sep 17 00:00:00 2001 From: simon Date: Fri, 19 Nov 2021 11:52:27 +0700 Subject: [PATCH] add subscribe to playlist form and better task management --- tubearchivist/home/forms.py | 14 ++++++++++++ tubearchivist/home/src/download.py | 34 ++++++++++++++++++++++++++++++ tubearchivist/home/tasks.py | 32 ++++++---------------------- tubearchivist/home/views.py | 14 ++++++++++++ 4 files changed, 68 insertions(+), 26 deletions(-) diff --git a/tubearchivist/home/forms.py b/tubearchivist/home/forms.py index c6b4d0a..5661025 100644 --- a/tubearchivist/home/forms.py +++ b/tubearchivist/home/forms.py @@ -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", + } + ), + ) diff --git a/tubearchivist/home/src/download.py b/tubearchivist/home/src/download.py index eec24c6..ff09be0 100644 --- a/tubearchivist/home/src/download.py +++ b/tubearchivist/home/src/download.py @@ -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"] diff --git a/tubearchivist/home/tasks.py b/tubearchivist/home/tasks.py index d081d84..4103655 100644 --- a/tubearchivist/home/tasks.py +++ b/tubearchivist/home/tasks.py @@ -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) diff --git a/tubearchivist/home/views.py b/tubearchivist/home/views.py index 2631b7e..d9348bb 100644 --- a/tubearchivist/home/views.py +++ b/tubearchivist/home/views.py @@ -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")