diff --git a/tubearchivist/api/README.md b/tubearchivist/api/README.md index 3b69e763..ad56ba37 100644 --- a/tubearchivist/api/README.md +++ b/tubearchivist/api/README.md @@ -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/\/ +GET: /api/video/\/ +DELETE: /api/video/\/ ## Video Comment View /api/video/\/comment/ diff --git a/tubearchivist/api/views.py b/tubearchivist/api/views.py index df8554cc..f9bb4bc3 100644 --- a/tubearchivist/api/views.py +++ b/tubearchivist/api/views.py @@ -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/ diff --git a/tubearchivist/home/src/frontend/api_calls.py b/tubearchivist/home/src/frontend/api_calls.py index 6e0bb78d..2175b115 100644 --- a/tubearchivist/home/src/frontend/api_calls.py +++ b/tubearchivist/home/src/frontend/api_calls.py @@ -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 diff --git a/tubearchivist/home/src/index/video.py b/tubearchivist/home/src/index/video.py index 6b8cb93e..45ddeb3c 100644 --- a/tubearchivist/home/src/index/video.py +++ b/tubearchivist/home/src/index/video.py @@ -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) diff --git a/tubearchivist/static/script.js b/tubearchivist/static/script.js index 09a8580c..188c6602 100644 --- a/tubearchivist/static/script.js +++ b/tubearchivist/static/script.js @@ -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);