make SearchHandler use new ElasticWrap class

This commit is contained in:
simon 2022-01-18 13:37:22 +07:00
parent 3eb0353fa9
commit 68f19b1719
No known key found for this signature in database
GPG Key ID: 2C15AA5E89985DD4
2 changed files with 21 additions and 31 deletions

View File

@ -10,8 +10,8 @@ import math
import urllib.parse import urllib.parse
from datetime import datetime from datetime import datetime
import requests
from home.src.config import AppConfig from home.src.config import AppConfig
from home.src.es import ElasticWrap
from home.src.helper import RedisArchivist from home.src.helper import RedisArchivist
from home.src.thumbnails import ThumbManager from home.src.thumbnails import ThumbManager
@ -19,23 +19,15 @@ from home.src.thumbnails import ThumbManager
class SearchHandler: class SearchHandler:
"""search elastic search""" """search elastic search"""
CONFIG = AppConfig().config def __init__(self, path, config, data=False):
CACHE_DIR = CONFIG["application"]["cache_dir"]
ES_AUTH = CONFIG["application"]["es_auth"]
def __init__(self, url, data):
self.max_hits = None self.max_hits = None
self.url = url self.path = path
self.config = config
self.data = data self.data = data
def get_data(self): def get_data(self):
"""get the data""" """get the data"""
if self.data: response, _ = ElasticWrap(self.path, config=self.config).get(self.data)
response = requests.get(
self.url, json=self.data, auth=self.ES_AUTH
).json()
else:
response = requests.get(self.url, auth=self.ES_AUTH).json()
if "hits" in response.keys(): if "hits" in response.keys():
self.max_hits = response["hits"]["total"]["value"] self.max_hits = response["hits"]["total"]["value"]
@ -153,11 +145,10 @@ class SearchForm:
"""build query from search form data""" """build query from search form data"""
CONFIG = AppConfig().config CONFIG = AppConfig().config
ES_URL = CONFIG["application"]["es_url"]
def multi_search(self, search_query): def multi_search(self, search_query):
"""searching through index""" """searching through index"""
url = self.ES_URL + "/ta_video,ta_channel,ta_playlist/_search" path = "ta_video,ta_channel,ta_playlist/_search"
data = { data = {
"size": 30, "size": 30,
"query": { "query": {
@ -184,7 +175,7 @@ class SearchForm:
} }
}, },
} }
look_up = SearchHandler(url, data) look_up = SearchHandler(path, config=self.CONFIG, data=data)
search_results = look_up.get_data() search_results = look_up.get_data()
all_results = self.build_results(search_results) all_results = self.build_results(search_results)

View File

@ -169,8 +169,7 @@ class ArchivistResultsView(ArchivistViewConfig):
def single_lookup(self, es_path): def single_lookup(self, es_path):
"""retrieve a single item from url""" """retrieve a single item from url"""
es_url = self.default_conf["application"]["es_url"] search = SearchHandler(es_path, config=self.default_conf)
search = SearchHandler(f"{es_url}/{es_path}", data=False)
result = search.get_data()[0]["source"] result = search.get_data()[0]["source"]
return result return result
@ -189,8 +188,9 @@ class ArchivistResultsView(ArchivistViewConfig):
def find_results(self): def find_results(self):
"""add results and pagination to context""" """add results and pagination to context"""
url = self.default_conf["application"]["es_url"] + self.es_search search = SearchHandler(
search = SearchHandler(url, self.data) self.es_search, config=self.default_conf, data=self.data
)
self.context["results"] = search.get_data() self.context["results"] = search.get_data()
self.pagination_handler.validate(search.max_hits) self.pagination_handler.validate(search.max_hits)
self.context["max_hits"] = search.max_hits self.context["max_hits"] = search.max_hits
@ -203,7 +203,7 @@ class HomeView(ArchivistResultsView):
""" """
view_origin = "home" view_origin = "home"
es_search = "/ta_video/_search" es_search = "ta_video/_search"
def get(self, request): def get(self, request):
"""handle get requests""" """handle get requests"""
@ -284,7 +284,7 @@ class DownloadView(ArchivistResultsView):
""" """
view_origin = "downloads" view_origin = "downloads"
es_search = "/ta_download/_search" es_search = "ta_download/_search"
def get(self, request): def get(self, request):
"""handle get request""" """handle get request"""
@ -346,7 +346,7 @@ class ChannelIdView(ArchivistResultsView):
""" """
view_origin = "home" view_origin = "home"
es_search = "/ta_video/_search" es_search = "ta_video/_search"
def get(self, request, channel_id): def get(self, request, channel_id):
"""get request""" """get request"""
@ -395,7 +395,7 @@ class ChannelView(ArchivistResultsView):
""" """
view_origin = "channel" view_origin = "channel"
es_search = "/ta_channel/_search" es_search = "ta_channel/_search"
def get(self, request): def get(self, request):
"""handle get request""" """handle get request"""
@ -445,7 +445,7 @@ class PlaylistIdView(ArchivistResultsView):
""" """
view_origin = "home" view_origin = "home"
es_search = "/ta_video/_search" es_search = "ta_video/_search"
def get(self, request, playlist_id): def get(self, request, playlist_id):
"""handle get request""" """handle get request"""
@ -521,7 +521,7 @@ class PlaylistView(ArchivistResultsView):
""" """
view_origin = "playlist" view_origin = "playlist"
es_search = "/ta_playlist/_search" es_search = "ta_playlist/_search"
def get(self, request): def get(self, request):
"""handle get request""" """handle get request"""
@ -592,9 +592,9 @@ class VideoView(View):
def get(self, request, video_id): def get(self, request, video_id):
"""get single video""" """get single video"""
es_url, colors, cast = self.read_config(user_id=request.user.id) colors, cast = self.read_config(user_id=request.user.id)
url = f"{es_url}/ta_video/_doc/{video_id}" path = f"ta_video/_doc/{video_id}"
look_up = SearchHandler(url, None) look_up = SearchHandler(path, config=False)
video_hit = look_up.get_data() video_hit = look_up.get_data()
video_data = video_hit[0]["source"] video_data = video_hit[0]["source"]
try: try:
@ -636,10 +636,9 @@ class VideoView(View):
def read_config(user_id): def read_config(user_id):
"""read config file""" """read config file"""
config_handler = AppConfig(user_id) config_handler = AppConfig(user_id)
es_url = config_handler.config["application"]["es_url"]
cast = config_handler.config["application"]["enable_cast"] cast = config_handler.config["application"]["enable_cast"]
colors = config_handler.colors colors = config_handler.colors
return es_url, colors, cast return colors, cast
@staticmethod @staticmethod
def star_creator(rating): def star_creator(rating):