From 599dd26b53a7cca270de6bf1d21a644420660997 Mon Sep 17 00:00:00 2001 From: simon Date: Fri, 23 Dec 2022 22:34:25 +0700 Subject: [PATCH] refactor comment interface into reusable CommentList class --- .../home/src/download/yt_dlp_handler.py | 22 +---------- tubearchivist/home/src/index/comments.py | 39 ++++++++++++++++++- tubearchivist/home/src/index/filesystem.py | 7 +++- 3 files changed, 45 insertions(+), 23 deletions(-) diff --git a/tubearchivist/home/src/download/yt_dlp_handler.py b/tubearchivist/home/src/download/yt_dlp_handler.py index c5c452d1..a0f01eb9 100644 --- a/tubearchivist/home/src/download/yt_dlp_handler.py +++ b/tubearchivist/home/src/download/yt_dlp_handler.py @@ -15,7 +15,7 @@ from home.src.download.subscriptions import PlaylistSubscription from home.src.download.yt_dlp_base import CookieHandler, YtWrap from home.src.es.connect import ElasticWrap, IndexPaginate from home.src.index.channel import YoutubeChannel -from home.src.index.comments import Comments +from home.src.index.comments import CommentList from home.src.index.playlist import YoutubePlaylist from home.src.index.video import YoutubeVideo, index_new_video from home.src.ta.config import AppConfig @@ -143,25 +143,7 @@ class DownloadPostProcess: def get_comments(self): """get comments from youtube""" - if not self.download.config["downloads"]["comment_max"]: - return - - total_videos = len(self.download.videos) - for idx, video_id in enumerate(self.download.videos): - comment = Comments(video_id, config=self.download.config) - comment.build_json(notify=(idx, total_videos)) - if comment.json_data: - comment.upload_comments() - - key = "message:download" - message = { - "status": key, - "level": "info", - "title": "Download and index comments finished", - "message": f"added comments for {total_videos} videos", - } - - RedisArchivist().set_message(key, message, expire=4) + CommentList(self.download.videos).index(send_notifications=True) class VideoDownloader: diff --git a/tubearchivist/home/src/index/comments.py b/tubearchivist/home/src/index/comments.py index c512802a..414d8753 100644 --- a/tubearchivist/home/src/index/comments.py +++ b/tubearchivist/home/src/index/comments.py @@ -14,7 +14,7 @@ from home.src.ta.ta_redis import RedisArchivist class Comments: - """hold all comments functionality""" + """interact with comments per video""" def __init__(self, youtube_id, config=False): self.youtube_id = youtube_id @@ -187,3 +187,40 @@ class Comments: self.delete_comments() self.upload_comments() + + +class CommentList: + """interact with comments in group""" + + def __init__(self, video_ids): + self.video_ids = video_ids + self.config = AppConfig().config + + def index(self, notify=False): + """index group of videos""" + if not self.config["downloads"].get("comment_max"): + return + + total_videos = len(self.video_ids) + for idx, video_id in enumerate(self.video_ids): + comment = Comments(video_id, config=self.config) + if notify: + notify = (idx, total_videos) + comment.build_json(notify=notify) + if comment.json_data: + comment.upload_comments() + + if notify: + self.notify_final(total_videos) + + @staticmethod + def notify_final(total_videos): + """send final notification""" + key = "message:download" + message = { + "status": key, + "level": "info", + "title": "Download and index comments finished", + "message": f"added comments for {total_videos} videos", + } + RedisArchivist().set_message(key, message, expire=4) diff --git a/tubearchivist/home/src/index/filesystem.py b/tubearchivist/home/src/index/filesystem.py index 38ca3e98..5f496ae0 100644 --- a/tubearchivist/home/src/index/filesystem.py +++ b/tubearchivist/home/src/index/filesystem.py @@ -14,6 +14,7 @@ import subprocess from home.src.download.queue import PendingList from home.src.download.thumbnails import ThumbManager from home.src.es.connect import ElasticWrap +from home.src.index.comments import CommentList from home.src.index.video import YoutubeVideo, index_new_video from home.src.ta.config import AppConfig from home.src.ta.helper import clean_string, ignore_filelist @@ -601,6 +602,8 @@ def scan_filesystem(): filesystem_handler.delete_from_index() if filesystem_handler.to_index: print("index new videos") - for missing_vid in filesystem_handler.to_index: - youtube_id = missing_vid[2] + video_ids = [i[2] for i in filesystem_handler.to_index] + for youtube_id in video_ids: index_new_video(youtube_id) + + CommentList(video_ids).index()