|
|
|
@ -6,103 +6,9 @@ Functionality:
|
|
|
|
|
- calculate pagination values
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
import urllib.parse
|
|
|
|
|
from datetime import datetime
|
|
|
|
|
|
|
|
|
|
from api.src.search_processor import SearchProcess
|
|
|
|
|
from home.src.download.thumbnails import ThumbManager
|
|
|
|
|
from home.src.es.connect import ElasticWrap
|
|
|
|
|
from home.src.ta.helper import get_duration_str
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class SearchHandler:
|
|
|
|
|
"""search elastic search"""
|
|
|
|
|
|
|
|
|
|
def __init__(self, path, data=False):
|
|
|
|
|
self.max_hits = None
|
|
|
|
|
self.aggs = None
|
|
|
|
|
self.path = path
|
|
|
|
|
self.data = data
|
|
|
|
|
|
|
|
|
|
def get_data(self):
|
|
|
|
|
"""get the data"""
|
|
|
|
|
response, _ = ElasticWrap(self.path).get(self.data)
|
|
|
|
|
|
|
|
|
|
if "hits" in response.keys():
|
|
|
|
|
self.max_hits = response["hits"]["total"]["value"]
|
|
|
|
|
return_value = response["hits"]["hits"]
|
|
|
|
|
else:
|
|
|
|
|
# simulate list for single result to reuse rest of class
|
|
|
|
|
return_value = [response]
|
|
|
|
|
|
|
|
|
|
if not return_value:
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
for idx, hit in enumerate(return_value):
|
|
|
|
|
return_value[idx] = self.hit_cleanup(hit)
|
|
|
|
|
|
|
|
|
|
if response.get("aggregations"):
|
|
|
|
|
self.aggs = response["aggregations"]
|
|
|
|
|
if "total_duration" in self.aggs:
|
|
|
|
|
duration_sec = int(self.aggs["total_duration"]["value"])
|
|
|
|
|
self.aggs["total_duration"].update(
|
|
|
|
|
{"value_str": get_duration_str(duration_sec)}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
return return_value
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def hit_cleanup(hit):
|
|
|
|
|
"""clean up and parse data from a single hit"""
|
|
|
|
|
hit["source"] = hit.pop("_source")
|
|
|
|
|
hit_keys = hit["source"].keys()
|
|
|
|
|
if "media_url" in hit_keys:
|
|
|
|
|
parsed_url = urllib.parse.quote(hit["source"]["media_url"])
|
|
|
|
|
hit["source"]["media_url"] = parsed_url
|
|
|
|
|
|
|
|
|
|
if "published" in hit_keys:
|
|
|
|
|
published = hit["source"]["published"]
|
|
|
|
|
date_pub = datetime.strptime(published, "%Y-%m-%d")
|
|
|
|
|
date_str = datetime.strftime(date_pub, "%d %b, %Y")
|
|
|
|
|
hit["source"]["published"] = date_str
|
|
|
|
|
|
|
|
|
|
if "vid_last_refresh" in hit_keys:
|
|
|
|
|
vid_last_refresh = hit["source"]["vid_last_refresh"]
|
|
|
|
|
date_refresh = datetime.fromtimestamp(vid_last_refresh)
|
|
|
|
|
date_str = datetime.strftime(date_refresh, "%d %b, %Y")
|
|
|
|
|
hit["source"]["vid_last_refresh"] = date_str
|
|
|
|
|
|
|
|
|
|
if "playlist_last_refresh" in hit_keys:
|
|
|
|
|
playlist_last_refresh = hit["source"]["playlist_last_refresh"]
|
|
|
|
|
date_refresh = datetime.fromtimestamp(playlist_last_refresh)
|
|
|
|
|
date_str = datetime.strftime(date_refresh, "%d %b, %Y")
|
|
|
|
|
hit["source"]["playlist_last_refresh"] = date_str
|
|
|
|
|
|
|
|
|
|
if "vid_thumb_url" in hit_keys:
|
|
|
|
|
youtube_id = hit["source"]["youtube_id"]
|
|
|
|
|
thumb_path = ThumbManager(youtube_id).vid_thumb_path()
|
|
|
|
|
hit["source"]["vid_thumb_url"] = f"/cache/{thumb_path}"
|
|
|
|
|
|
|
|
|
|
if "channel_last_refresh" in hit_keys:
|
|
|
|
|
refreshed = hit["source"]["channel_last_refresh"]
|
|
|
|
|
date_refresh = datetime.fromtimestamp(refreshed)
|
|
|
|
|
date_str = datetime.strftime(date_refresh, "%d %b, %Y")
|
|
|
|
|
hit["source"]["channel_last_refresh"] = date_str
|
|
|
|
|
|
|
|
|
|
if "channel" in hit_keys:
|
|
|
|
|
channel_keys = hit["source"]["channel"].keys()
|
|
|
|
|
if "channel_last_refresh" in channel_keys:
|
|
|
|
|
refreshed = hit["source"]["channel"]["channel_last_refresh"]
|
|
|
|
|
date_refresh = datetime.fromtimestamp(refreshed)
|
|
|
|
|
date_str = datetime.strftime(date_refresh, "%d %b, %Y")
|
|
|
|
|
hit["source"]["channel"]["channel_last_refresh"] = date_str
|
|
|
|
|
|
|
|
|
|
if "subtitle_fragment_id" in hit_keys:
|
|
|
|
|
youtube_id = hit["source"]["youtube_id"]
|
|
|
|
|
thumb_path = ThumbManager(youtube_id).vid_thumb_path()
|
|
|
|
|
hit["source"]["vid_thumb_url"] = f"/cache/{thumb_path}"
|
|
|
|
|
|
|
|
|
|
return hit
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class SearchForm:
|
|
|
|
|