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

View File

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