From ef803a157a11a84ce72dd98278f49f480e018a96 Mon Sep 17 00:00:00 2001 From: simon Date: Sat, 16 Apr 2022 00:37:56 +0700 Subject: [PATCH] standard api key processor for list and single views --- tubearchivist/api/src/search_processor.py | 11 +++++++++-- tubearchivist/api/views.py | 22 ---------------------- 2 files changed, 9 insertions(+), 24 deletions(-) diff --git a/tubearchivist/api/src/search_processor.py b/tubearchivist/api/src/search_processor.py index 0fc672b..695f82d 100644 --- a/tubearchivist/api/src/search_processor.py +++ b/tubearchivist/api/src/search_processor.py @@ -64,6 +64,8 @@ class SearchProcess: def _process_video(self, video_dict): """run on single video dict""" + # fix cache_dir build dynamic + cache_dir = "/cache" video_id = video_dict["youtube_id"] media_url = urllib.parse.quote(video_dict["media_url"]) 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) 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( { "channel": channel, - "media_url": media_url, + "media_url": f"/media/{media_url}", "vid_last_refresh": vid_last_refresh, "published": published, - "vid_thumb_url": vid_thumb_url, + "vid_thumb_url": f"{cache_dir}/{vid_thumb_url}", } ) diff --git a/tubearchivist/api/views.py b/tubearchivist/api/views.py index afe1c0b..60f2ba3 100644 --- a/tubearchivist/api/views.py +++ b/tubearchivist/api/views.py @@ -1,7 +1,6 @@ """all API views""" from api.src.search_processor import SearchProcess -from home.src.download.thumbnails import ThumbManager from home.src.es.connect import ElasticWrap from home.src.index.video import SponsorBlock from home.src.ta.config import AppConfig @@ -44,25 +43,6 @@ class ApiBaseView(APIView): self.response["data"] = False 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): """add pagination detail to response""" self.response["paginate"] = False @@ -86,8 +66,6 @@ class VideoApiView(ApiBaseView): # pylint: disable=unused-argument """get request""" self.get_document(video_id) - if self.response.get("data"): - self.process_keys() return Response(self.response, status=self.status_code)