add subscribe to playlist backend functionality

This commit is contained in:
simon 2021-11-18 16:00:55 +07:00
parent 8b69be645c
commit 1af0196208
No known key found for this signature in database
GPG Key ID: 2C15AA5E89985DD4
2 changed files with 59 additions and 1 deletions

View File

@ -378,6 +378,31 @@ class PlaylistSubscription:
return all_playlists 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: class VideoDownloader:
""" """

View File

@ -8,7 +8,12 @@ import os
from celery import Celery, shared_task from celery import Celery, shared_task
from home.src.config import AppConfig 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.helper import RedisArchivist, RedisQueue, UrlListParser
from home.src.index import YoutubeChannel, YoutubePlaylist from home.src.index import YoutubeChannel, YoutubePlaylist
from home.src.index_management import backup_all_indexes, restore_from_backup from home.src.index_management import backup_all_indexes, restore_from_backup
@ -224,3 +229,31 @@ def index_channel_playlists(channel_id):
handler = ThumbManager() handler = ThumbManager()
missing_playlists = handler.get_missing_playlists() missing_playlists = handler.get_missing_playlists()
handler.download_playlist(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
)