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
ES_URL = CONFIG["application"]["es_url"]
CACHE_DIR = CONFIG["application"]["cache_dir"]
VIDEOS = CONFIG["application"]["videos"]
def __init__(self, channel_id):
self.channel_id = channel_id
@ -193,17 +194,60 @@ class YoutubeChannel:
if not request.ok:
print(request.text)
def get_total_hits(self):
"""get total channels indexed"""
def get_folder_path(self):
"""get folder where media files get stored"""
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)
url = f"{self.ES_URL}/ta_channel/_search?filter_path=hits.total"
request = requests.post(url, data=payload, headers=headers)
if not request.ok:
print(request.text)
total_hits = json.loads(request.text)["hits"]["total"]["value"]
return total_hits
url = self.ES_URL + "/ta_video/_search"
response = requests.post(url, data=payload, headers=headers)
if not response.ok:
print(response.text)
json_data = json.loads(response.text)
# 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:

View File

@ -15,7 +15,7 @@ from django.views import View
from home.src.config import AppConfig
from home.src.download import ChannelSubscription, PendingList
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.tasks import (
download_pending,
@ -510,6 +510,7 @@ class PostData:
"fs-rescan": self.fs_rescan,
"channel-search": self.channel_search,
"delete-video": self.delete_video,
"delete-channel": self.delete_channel,
}
return exec_map[self.to_exec]
@ -680,3 +681,9 @@ class PostData:
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
YoutubeChannel(channel_id).delete_channel()
return {"success": True}