add delete channel backend

This commit is contained in:
simon 2021-10-09 19:54:36 +07:00
parent 8d3e2f14fc
commit 11780a0ff0
2 changed files with 61 additions and 10 deletions

View File

@ -24,6 +24,7 @@ class YoutubeChannel:
CONFIG = AppConfig().config CONFIG = AppConfig().config
ES_URL = CONFIG["application"]["es_url"] ES_URL = CONFIG["application"]["es_url"]
CACHE_DIR = CONFIG["application"]["cache_dir"] CACHE_DIR = CONFIG["application"]["cache_dir"]
VIDEOS = CONFIG["application"]["videos"]
def __init__(self, channel_id): def __init__(self, channel_id):
self.channel_id = channel_id self.channel_id = channel_id
@ -193,17 +194,60 @@ class YoutubeChannel:
if not request.ok: if not request.ok:
print(request.text) print(request.text)
def get_total_hits(self): def get_folder_path(self):
"""get total channels indexed""" """get folder where media files get stored"""
headers = {"Content-type": "application/json"} headers = {"Content-type": "application/json"}
data = {"query": {"match_all": {}}} # get media url
data = {
"size": 1,
"query": {
"term": {"channel.channel_id": {"value": self.channel_id}}
},
"sort": [{"published": {"order": "desc"}}],
}
payload = json.dumps(data) payload = json.dumps(data)
url = f"{self.ES_URL}/ta_channel/_search?filter_path=hits.total" url = self.ES_URL + "/ta_video/_search"
request = requests.post(url, data=payload, headers=headers) response = requests.post(url, data=payload, headers=headers)
if not request.ok: if not response.ok:
print(request.text) print(response.text)
total_hits = json.loads(request.text)["hits"]["total"]["value"] json_data = json.loads(response.text)
return total_hits # get folder
media_url = json_data["hits"]["hits"][0]["_source"]["media_url"]
folder_name = os.path.split(media_url)[0]
folder_path = os.path.join(self.VIDEOS, folder_name)
return folder_path
def delete_es_videos(self):
"""delete all channel documents from elasticsearch"""
headers = {"Content-type": "application/json"}
data = {
"query": {
"term": {"channel.channel_id": {"value": self.channel_id}}
}
}
payload = json.dumps(data)
url = self.ES_URL + "/ta_video/_delete_by_query"
response = requests.post(url, data=payload, headers=headers)
if not response.ok:
print(response.text)
def delete_channel(self):
"""delete channel and all videos"""
print(f"deleting {self.channel_id} and all matching media files")
folder_path = self.get_folder_path()
print("delete all media files")
all_videos = os.listdir(folder_path)
for video in all_videos:
video_path = os.path.join(folder_path, video)
os.remove(video_path)
os.rmdir(folder_path)
print("delete indexed videos")
self.delete_es_videos()
url = self.ES_URL + "/ta_channel/_doc/" + self.channel_id
response = requests.delete(url)
if not response.ok:
print(response.text)
class YoutubeVideo: class YoutubeVideo:

View File

@ -15,7 +15,7 @@ from django.views import View
from home.src.config import AppConfig from home.src.config import AppConfig
from home.src.download import ChannelSubscription, PendingList from home.src.download import ChannelSubscription, PendingList
from home.src.helper import RedisArchivist, RedisQueue, process_url_list from home.src.helper import RedisArchivist, RedisQueue, process_url_list
from home.src.index import WatchState, YoutubeVideo from home.src.index import WatchState, YoutubeChannel, YoutubeVideo
from home.src.searching import Pagination, SearchForm, SearchHandler from home.src.searching import Pagination, SearchForm, SearchHandler
from home.tasks import ( from home.tasks import (
download_pending, download_pending,
@ -510,6 +510,7 @@ class PostData:
"fs-rescan": self.fs_rescan, "fs-rescan": self.fs_rescan,
"channel-search": self.channel_search, "channel-search": self.channel_search,
"delete-video": self.delete_video, "delete-video": self.delete_video,
"delete-channel": self.delete_channel,
} }
return exec_map[self.to_exec] return exec_map[self.to_exec]
@ -680,3 +681,9 @@ class PostData:
youtube_id = self.exec_val youtube_id = self.exec_val
YoutubeVideo(youtube_id).delete_media_file() YoutubeVideo(youtube_id).delete_media_file()
return {"success": True} 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}