From 1af0196208900e98f2a1ce10a52925c42f94d333 Mon Sep 17 00:00:00 2001 From: simon Date: Thu, 18 Nov 2021 16:00:55 +0700 Subject: [PATCH] add subscribe to playlist backend functionality --- tubearchivist/home/src/download.py | 25 +++++++++++++++++++++ tubearchivist/home/tasks.py | 35 +++++++++++++++++++++++++++++- 2 files changed, 59 insertions(+), 1 deletion(-) diff --git a/tubearchivist/home/src/download.py b/tubearchivist/home/src/download.py index fd06cf3..4e0d119 100644 --- a/tubearchivist/home/src/download.py +++ b/tubearchivist/home/src/download.py @@ -378,6 +378,31 @@ class PlaylistSubscription: return all_playlists + def change_subscribe(self, playlist_id, subscribe_status): + """change the subscribe status of a playlist""" + playlist_handler = YoutubePlaylist(playlist_id) + playlist_handler.get_playlist_dict() + subed_now = playlist_handler.playlist_dict["playlist_subscribed"] + + if subed_now == subscribe_status: + # status already as expected, do nothing + return False + + # update subscribed status + print(f"changing status of {playlist_id} to {subscribe_status}") + headers = {"Content-type": "application/json"} + url = f"{self.es_url}/ta_playlist/_update/{playlist_id}" + payload = json.dumps( + {"doc": {"playlist_subscribed": subscribe_status}} + ) + response = requests.post( + url, data=payload, headers=headers, auth=self.es_auth + ) + if not response.ok: + print(response.text) + + return True + class VideoDownloader: """ diff --git a/tubearchivist/home/tasks.py b/tubearchivist/home/tasks.py index 92608fb..2f5bd72 100644 --- a/tubearchivist/home/tasks.py +++ b/tubearchivist/home/tasks.py @@ -8,7 +8,12 @@ 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.download import ( + ChannelSubscription, + PendingList, + PlaylistSubscription, + VideoDownloader, +) from home.src.helper import RedisArchivist, RedisQueue, UrlListParser from home.src.index import YoutubeChannel, YoutubePlaylist from home.src.index_management import backup_all_indexes, restore_from_backup @@ -224,3 +229,31 @@ def index_channel_playlists(channel_id): handler = ThumbManager() missing_playlists = handler.get_missing_playlists() handler.download_playlist(missing_playlists) + + +@shared_task +def subscribe_to_playlist(url_str): + """process url string to subscribe to playlists""" + 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 + )