From 9068e4569df97452715c9722c839937d07ff8d2c Mon Sep 17 00:00:00 2001 From: Simon Date: Wed, 22 May 2024 18:34:18 +0200 Subject: [PATCH] add manual fix management commands --- .../management/commands/ta_fix_channels.py | 44 +++++++++++ .../commands/ta_fix_comment_link.py | 76 +++++++++++++++++++ 2 files changed, 120 insertions(+) create mode 100644 tubearchivist/config/management/commands/ta_fix_channels.py create mode 100644 tubearchivist/config/management/commands/ta_fix_comment_link.py diff --git a/tubearchivist/config/management/commands/ta_fix_channels.py b/tubearchivist/config/management/commands/ta_fix_channels.py new file mode 100644 index 00000000..d602e668 --- /dev/null +++ b/tubearchivist/config/management/commands/ta_fix_channels.py @@ -0,0 +1,44 @@ +""" +channel fix for update from v0.4.7 to v0.4.8 +reindex channels with 0 subscriber count +python manage.py ta_fix_channels +""" + +from django.core.management.base import BaseCommand +from home.src.es.connect import IndexPaginate +from home.tasks import check_reindex + + +class Command(BaseCommand): + """fix comment link""" + + def handle(self, *args, **options): + """run command""" + self.stdout.write("reindex failed channels") + channels = self._get_channels() + if not channels: + self.stdout.write("did not find any failed channels") + return + + self.stdout.write(f"add {len(channels)} channels(s) to queue") + to_reindex = {"channel": [i["channel_id"] for i in channels]} + check_reindex.delay(data=to_reindex) + self.stdout.write(self.style.SUCCESS(" ✓ task queued\n")) + + def _get_channels(self): + """get failed channels""" + self.stdout.write("search for failed channels") + es_query = { + "query": { + "bool": { + "must": [ + {"term": {"channel_subs": {"value": 0}}}, + {"term": {"channel_active": {"value": True}}}, + ] + }, + }, + "_source": ["channel_id"], + } + channels = IndexPaginate("ta_channel", es_query).get_results() + + return channels diff --git a/tubearchivist/config/management/commands/ta_fix_comment_link.py b/tubearchivist/config/management/commands/ta_fix_comment_link.py new file mode 100644 index 00000000..55ce69d3 --- /dev/null +++ b/tubearchivist/config/management/commands/ta_fix_comment_link.py @@ -0,0 +1,76 @@ +""" +comment link fix for update from v0.4.7 to v0.4.8 +scan your videos and comments to fix comment_count field +python manage.py ta_fix_comment_link +""" + +from django.core.management.base import BaseCommand, CommandError +from home.src.es.connect import ElasticWrap, IndexPaginate + + +class Command(BaseCommand): + """fix comment link""" + + def handle(self, *args, **options): + """run command""" + self.stdout.write("run comment link fix") + expected_count = self._get_comment_indexed() + all_videos = self._get_videos() + + self.stdout.write(f"checking {len(all_videos)} video(s)") + videos_updated = [] + for video in all_videos: + video_id = video["youtube_id"] + comment_count = expected_count.get(video_id) + if not comment_count: + continue + + data = {"doc": {"comment_count": comment_count}} + path = f"ta_video/_update/{video_id}" + response, status_code = ElasticWrap(path).post(data=data) + + if status_code != 200: + message = ( + "failed to add comment count to video" + + f"response code: {status_code}" + + response + ) + raise CommandError(message) + + videos_updated.append(video_id) + + self.stdout.write(f"fixed {len(videos_updated)} video(s)") + self.stdout.write(self.style.SUCCESS(" ✓ task completed\n")) + + def _get_comment_indexed(self): + """get comment count by index""" + self.stdout.write("get comments") + src = "params['_source']['comment_comments'].length" + data = { + "script_fields": { + "comments_length": { + "script": {"source": src, "lang": "painless"} + } + } + } + all_comments = IndexPaginate( + "ta_comment", data=data, keep_source=True + ).get_results() + + expected_count = { + i["_id"]: i["fields"]["comments_length"][0] for i in all_comments + } + + return expected_count + + def _get_videos(self): + """get videos without comment_count""" + self.stdout.write("get videos") + data = { + "query": { + "bool": {"must_not": [{"exists": {"field": "comment_count"}}]} + } + } + all_videos = IndexPaginate("ta_video", data).get_results() + + return all_videos