[API] add and use DELETE video endpoint

This commit is contained in:
simon 2022-12-22 19:01:13 +07:00
parent 63f35b19fe
commit db21ee8bcb
No known key found for this signature in database
GPG Key ID: 2C15AA5E89985DD4
5 changed files with 22 additions and 12 deletions

View File

@ -79,7 +79,8 @@ Pass page number as a query parameter: `page=2`. Defaults to *0*, `page=1` is re
/api/video/
## Video Item View
/api/video/\<video_id>/
GET: /api/video/\<video_id>/
DELETE: /api/video/\<video_id>/
## Video Comment View
/api/video/\<video_id>/comment/

View File

@ -9,7 +9,7 @@ from home.src.es.snapshot import ElasticSnapshot
from home.src.frontend.searching import SearchForm
from home.src.index.generic import Pagination
from home.src.index.reindex import ReindexProgress
from home.src.index.video import SponsorBlock
from home.src.index.video import SponsorBlock, YoutubeVideo
from home.src.ta.config import AppConfig
from home.src.ta.helper import UrlListParser
from home.src.ta.ta_redis import RedisArchivist, RedisQueue
@ -95,6 +95,20 @@ class VideoApiView(ApiBaseView):
self.get_document(video_id)
return Response(self.response, status=self.status_code)
def delete(self, request, video_id):
# pylint: disable=unused-argument
"""delete single video"""
message = {"video": video_id}
try:
YoutubeVideo(video_id).delete_media_file()
status_code = 200
message.update({"state": "delete"})
except FileNotFoundError:
status_code = 404
message.update({"state": "not found"})
return Response(message, status=status_code)
class VideoApiListView(ApiBaseView):
"""resolves to /api/video/

View File

@ -12,7 +12,6 @@ from home.src.download.subscriptions import (
from home.src.frontend.watched import WatchState
from home.src.index.channel import YoutubeChannel
from home.src.index.playlist import YoutubePlaylist
from home.src.index.video import YoutubeVideo
from home.src.ta.helper import UrlListParser
from home.src.ta.ta_redis import RedisArchivist, RedisQueue
from home.tasks import (
@ -73,7 +72,6 @@ class PostData:
"db-backup": self._db_backup,
"db-restore": self._db_restore,
"fs-rescan": self._fs_rescan,
"delete-video": self._delete_video,
"delete-channel": self._delete_channel,
"delete-playlist": self._delete_playlist,
"find-playlists": self._find_playlists,
@ -284,12 +282,6 @@ class PostData:
rescan_filesystem.delay()
return {"success": True}
def _delete_video(self):
"""delete media file, metadata and thumb"""
youtube_id = self.exec_val
YoutubeVideo(youtube_id).delete_media_file()
return {"success": True}
def _delete_channel(self):
"""delete channel and all matching videos"""
channel_id = self.exec_val

View File

@ -292,6 +292,9 @@ class YoutubeVideo(YouTubeItem, YoutubeSubtitle):
"""delete video file, meta data"""
print(f"{self.youtube_id}: delete video")
self.get_from_es()
if not self.json_data:
raise FileNotFoundError
video_base = self.app_conf["videos"]
media_url = self.json_data.get("media_url")
file_path = os.path.join(video_base, media_url)

View File

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