standard api key processor for list and single views

This commit is contained in:
simon 2022-04-16 00:37:56 +07:00
parent 51ceffd58f
commit ef803a157a
No known key found for this signature in database
GPG Key ID: 2C15AA5E89985DD4
2 changed files with 9 additions and 24 deletions

View File

@ -64,6 +64,8 @@ class SearchProcess:
def _process_video(self, video_dict): def _process_video(self, video_dict):
"""run on single video dict""" """run on single video dict"""
# fix cache_dir build dynamic
cache_dir = "/cache"
video_id = video_dict["youtube_id"] video_id = video_dict["youtube_id"]
media_url = urllib.parse.quote(video_dict["media_url"]) media_url = urllib.parse.quote(video_dict["media_url"])
vid_last_refresh = date_praser(video_dict["vid_last_refresh"]) vid_last_refresh = date_praser(video_dict["vid_last_refresh"])
@ -71,13 +73,18 @@ class SearchProcess:
vid_thumb_url = ThumbManager().vid_thumb_path(video_id) vid_thumb_url = ThumbManager().vid_thumb_path(video_id)
channel = self._process_channel(video_dict["channel"]) channel = self._process_channel(video_dict["channel"])
if "subtitles" in video_dict:
for idx, _ in enumerate(video_dict["subtitles"]):
url = video_dict["subtitles"][idx]["media_url"]
video_dict["subtitles"][idx]["media_url"] = f"/media/{url}"
video_dict.update( video_dict.update(
{ {
"channel": channel, "channel": channel,
"media_url": media_url, "media_url": f"/media/{media_url}",
"vid_last_refresh": vid_last_refresh, "vid_last_refresh": vid_last_refresh,
"published": published, "published": published,
"vid_thumb_url": vid_thumb_url, "vid_thumb_url": f"{cache_dir}/{vid_thumb_url}",
} }
) )

View File

@ -1,7 +1,6 @@
"""all API views""" """all API views"""
from api.src.search_processor import SearchProcess from api.src.search_processor import SearchProcess
from home.src.download.thumbnails import ThumbManager
from home.src.es.connect import ElasticWrap from home.src.es.connect import ElasticWrap
from home.src.index.video import SponsorBlock from home.src.index.video import SponsorBlock
from home.src.ta.config import AppConfig from home.src.ta.config import AppConfig
@ -44,25 +43,6 @@ class ApiBaseView(APIView):
self.response["data"] = False self.response["data"] = False
self.status_code = status_code self.status_code = status_code
def process_keys(self):
"""process keys for frontend"""
all_keys = self.response["data"].keys()
if "media_url" in all_keys:
media_url = self.response["data"]["media_url"]
self.response["data"]["media_url"] = f"/media/{media_url}"
if "vid_thumb_url" in all_keys:
youtube_id = self.response["data"]["youtube_id"]
vid_thumb_url = ThumbManager().vid_thumb_path(youtube_id)
cache_dir = self.response["config"]["application"]["cache_dir"]
new_thumb = f"{cache_dir}/{vid_thumb_url}"
self.response["data"]["vid_thumb_url"] = new_thumb
if "subtitles" in all_keys:
all_subtitles = self.response["data"]["subtitles"]
for idx, _ in enumerate(all_subtitles):
url = self.response["data"]["subtitles"][idx]["media_url"]
new_url = f"/media/{url}"
self.response["data"]["subtitles"][idx]["media_url"] = new_url
def get_paginate(self): def get_paginate(self):
"""add pagination detail to response""" """add pagination detail to response"""
self.response["paginate"] = False self.response["paginate"] = False
@ -86,8 +66,6 @@ class VideoApiView(ApiBaseView):
# pylint: disable=unused-argument # pylint: disable=unused-argument
"""get request""" """get request"""
self.get_document(video_id) self.get_document(video_id)
if self.response.get("data"):
self.process_keys()
return Response(self.response, status=self.status_code) return Response(self.response, status=self.status_code)