[API] add and use DELETE channel endpoint

This commit is contained in:
simon 2022-12-22 19:24:48 +07:00
parent bad4b48573
commit 7a9f5e5685
No known key found for this signature in database
GPG Key ID: 2C15AA5E89985DD4
5 changed files with 23 additions and 11 deletions

View File

@ -165,7 +165,9 @@ POST /api/channel/
```
## Channel Item View
/api/channel/\<channel_id>/
GET: /api/channel/\<channel_id>/
DELETE: /api/channel/\<channel_id>/
- Will delete channel with all it's videos
## Channel Videos View
/api/channel/\<channel_id>/video/

View File

@ -7,6 +7,7 @@ from home.src.download.yt_dlp_base import CookieHandler
from home.src.es.connect import ElasticWrap
from home.src.es.snapshot import ElasticSnapshot
from home.src.frontend.searching import SearchForm
from home.src.index.channel import YoutubeChannel
from home.src.index.generic import Pagination
from home.src.index.reindex import ReindexProgress
from home.src.index.video import SponsorBlock, YoutubeVideo
@ -265,6 +266,20 @@ class ChannelApiView(ApiBaseView):
self.get_document(channel_id)
return Response(self.response, status=self.status_code)
def delete(self, request, channel_id):
# pylint: disable=unused-argument
"""delete channel"""
message = {"channel": channel_id}
try:
YoutubeChannel(channel_id).delete_channel()
status_code = 200
message.update({"state": "delete"})
except FileNotFoundError:
status_code = 404
message.update({"state": "not found"})
return Response(message, status=status_code)
class ChannelApiListView(ApiBaseView):
"""resolves to /api/channel/

View File

@ -10,7 +10,6 @@ from home.src.download.subscriptions import (
PlaylistSubscription,
)
from home.src.frontend.watched import WatchState
from home.src.index.channel import YoutubeChannel
from home.src.index.playlist import YoutubePlaylist
from home.src.ta.helper import UrlListParser
from home.src.ta.ta_redis import RedisArchivist, RedisQueue
@ -71,7 +70,6 @@ class PostData:
"db-backup": self._db_backup,
"db-restore": self._db_restore,
"fs-rescan": self._fs_rescan,
"delete-channel": self._delete_channel,
"delete-playlist": self._delete_playlist,
"find-playlists": self._find_playlists,
}
@ -274,12 +272,6 @@ class PostData:
rescan_filesystem.delay()
return {"success": True}
def _delete_channel(self):
"""delete channel and all matching videos"""
channel_id = self.exec_val
YoutubeChannel(channel_id).delete_channel()
return {"success": True}
def _delete_playlist(self):
"""delete playlist, only metadata or incl all videos"""
playlist_dict = self.exec_val

View File

@ -298,6 +298,9 @@ class YoutubeChannel(YouTubeItem):
"""delete channel and all videos"""
print(f"{self.youtube_id}: delete channel")
self.get_from_es()
if not self.json_data:
raise FileNotFoundError
folder_path = self.get_folder_path()
print(f"{self.youtube_id}: delete all media files")
try:

View File

@ -344,8 +344,8 @@ function deleteVideo(button) {
function deleteChannel(button) {
let to_delete = button.getAttribute('data-id');
let payload = JSON.stringify({ 'delete-channel': to_delete });
sendPost(payload);
let apiEndpoint = '/api/channel/' + to_delete + '/';
apiRequest(apiEndpoint, 'DELETE');
setTimeout(function () {
window.location.replace('/channel/');
}, 1000);