From ef803a157a11a84ce72dd98278f49f480e018a96 Mon Sep 17 00:00:00 2001 From: simon Date: Sat, 16 Apr 2022 00:37:56 +0700 Subject: [PATCH 1/3] 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) From 50f4e0e5b70fb45d69cd0637ac28eca463a84132 Mon Sep 17 00:00:00 2001 From: simon Date: Sat, 16 Apr 2022 13:08:32 +0700 Subject: [PATCH 2/3] skip base64 generator, hotfix --- tubearchivist/home/src/index/video.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tubearchivist/home/src/index/video.py b/tubearchivist/home/src/index/video.py index b8adb9a..969d716 100644 --- a/tubearchivist/home/src/index/video.py +++ b/tubearchivist/home/src/index/video.py @@ -10,7 +10,6 @@ from datetime import datetime import requests from django.conf import settings -from home.src.download.thumbnails import ThumbManager from home.src.es.connect import ElasticWrap from home.src.index import channel as ta_channel from home.src.index.generic import YouTubeItem @@ -437,7 +436,8 @@ class YoutubeVideo(YouTubeItem, YoutubeSubtitle): upload_date_time = datetime.strptime(upload_date, "%Y%m%d") published = upload_date_time.strftime("%Y-%m-%d") last_refresh = int(datetime.now().strftime("%s")) - base64_blur = ThumbManager().get_base64_blur(self.youtube_id) + # base64_blur = ThumbManager().get_base64_blur(self.youtube_id) + base64_blur = False # build json_data basics self.json_data = { "title": self.youtube_meta["title"], From 7d45d23767c9401ca20fe28435f82b5c2f8efc70 Mon Sep 17 00:00:00 2001 From: simon Date: Sun, 17 Apr 2022 05:21:35 +0700 Subject: [PATCH 3/3] process ta_download search results --- tubearchivist/api/src/search_processor.py | 24 ++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/tubearchivist/api/src/search_processor.py b/tubearchivist/api/src/search_processor.py index 695f82d..6a1e2dd 100644 --- a/tubearchivist/api/src/search_processor.py +++ b/tubearchivist/api/src/search_processor.py @@ -7,12 +7,16 @@ Functionality: import urllib.parse from home.src.download.thumbnails import ThumbManager +from home.src.ta.config import AppConfig from home.src.ta.helper import date_praser class SearchProcess: """process search results""" + CONFIG = AppConfig().config + CACHE_DIR = CONFIG["application"]["cache_dir"] + def __init__(self, response): self.response = response self.processed = False @@ -42,6 +46,8 @@ class SearchProcess: processed = self._process_channel(result["_source"]) if index == "ta_playlist": processed = self._process_playlist(result["_source"]) + if index == "ta_download": + processed = self._process_download(result["_source"]) return processed @@ -64,8 +70,6 @@ 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"]) @@ -84,7 +88,7 @@ class SearchProcess: "media_url": f"/media/{media_url}", "vid_last_refresh": vid_last_refresh, "published": published, - "vid_thumb_url": f"{cache_dir}/{vid_thumb_url}", + "vid_thumb_url": f"{self.CACHE_DIR}/{vid_thumb_url}", } ) @@ -105,3 +109,17 @@ class SearchProcess: ) return dict(sorted(playlist_dict.items())) + + def _process_download(self, download_dict): + """run on single download item""" + video_id = download_dict["youtube_id"] + vid_thumb_url = ThumbManager().vid_thumb_path(video_id) + published = date_praser(download_dict["published"]) + + download_dict.update( + { + "vid_thumb_url": f"{self.CACHE_DIR}/{vid_thumb_url}", + "published": published, + } + ) + return dict(sorted(download_dict.items()))