[API] validate sub type, add sub/unsub channel and playlist

pull/524/head
Simon 1 year ago
parent a2eb42ebb9
commit b005b7bcfe
No known key found for this signature in database
GPG Key ID: 2C15AA5E89985DD4

@ -2,6 +2,10 @@
from api.src.search_processor import SearchProcess from api.src.search_processor import SearchProcess
from home.src.download.queue import PendingInteract from home.src.download.queue import PendingInteract
from home.src.download.subscriptions import (
ChannelSubscription,
PlaylistSubscription,
)
from home.src.download.yt_dlp_base import CookieHandler from home.src.download.yt_dlp_base import CookieHandler
from home.src.es.connect import ElasticWrap from home.src.es.connect import ElasticWrap
from home.src.es.snapshot import ElasticSnapshot from home.src.es.snapshot import ElasticSnapshot
@ -318,9 +322,8 @@ class ChannelApiListView(ApiBaseView):
return Response(self.response) return Response(self.response)
@staticmethod def post(self, request):
def post(request): """subscribe/unsubscribe to list of channels"""
"""subscribe to list of channels"""
data = request.data data = request.data
try: try:
to_add = data["data"] to_add = data["data"]
@ -329,12 +332,28 @@ class ChannelApiListView(ApiBaseView):
print(message) print(message)
return Response({"message": message}, status=400) return Response({"message": message}, status=400)
pending = [i["channel_id"] for i in to_add if i["channel_subscribed"]] pending = []
url_str = " ".join(pending) for channel_item in to_add:
subscribe_to.delay(url_str, expected_type="channel") channel_id = channel_item["channel_id"]
if channel_item["channel_subscribed"]:
pending.append(channel_id)
else:
self._unsubscribe(channel_id)
if pending:
url_str = " ".join(pending)
subscribe_to.delay(url_str, expected_type="channel")
return Response(data) return Response(data)
@staticmethod
def _unsubscribe(channel_id: str):
"""unsubscribe"""
print(f"[{channel_id}] unsubscribe from channel")
ChannelSubscription().change_subscribe(
channel_id, channel_subscribed=False
)
class ChannelApiVideoView(ApiBaseView): class ChannelApiVideoView(ApiBaseView):
"""resolves to /api/channel/<channel-id>/video """resolves to /api/channel/<channel-id>/video
@ -373,6 +392,38 @@ class PlaylistApiListView(ApiBaseView):
self.get_document_list(request) self.get_document_list(request)
return Response(self.response) return Response(self.response)
def post(self, request):
"""subscribe/unsubscribe to list of playlists"""
data = request.data
try:
to_add = data["data"]
except KeyError:
message = "missing expected data key"
print(message)
return Response({"message": message}, status=400)
pending = []
for playlist_item in to_add:
playlist_id = playlist_item["playlist_id"]
if playlist_item["playlist_subscribed"]:
pending.append(playlist_id)
else:
self._unsubscribe(playlist_id)
if pending:
url_str = " ".join(pending)
subscribe_to.delay(url_str, expected_type="playlist")
return Response(data)
@staticmethod
def _unsubscribe(playlist_id: str):
"""unsubscribe"""
print(f"[{playlist_id}] unsubscribe from playlist")
PlaylistSubscription().change_subscribe(
playlist_id, subscribe_status=False
)
class PlaylistApiView(ApiBaseView): class PlaylistApiView(ApiBaseView):
"""resolves to /api/playlist/<playlist_id>/ """resolves to /api/playlist/<playlist_id>/

Loading…
Cancel
Save