scripts: improve cleanup script

This commit is contained in:
vas3k 2022-08-15 12:11:50 +02:00
parent aadefbc286
commit 2ab857aae0
3 changed files with 27 additions and 5 deletions

View File

@ -19,6 +19,7 @@ boards:
- name: TJ
url: https://tjournal.ru
rss: https://tjournal.ru/rss/all
icon: https://i.vas3k.ru/bba63f7248fb952e4157c70b83bfd769d0328ed14055addf7756d1ad191bad05.jpg
is_parsable: false
- name: Коммерсантъ
url: https://www.kommersant.ru/

View File

@ -119,6 +119,9 @@ TELEGRAM_CACHE_SECONDS = 10 * 60 # 10 min
BLEACH_STRIP_TAGS = True
OLD_ARTICLES_CLEANUP_AFTER_DAYS = 300
OLD_ARTICLES_CLEANUP_AFTER_AMOUNT = 150
if SENTRY_DSN and not DEBUG:
sentry_sdk.init(
dsn=SENTRY_DSN,

View File

@ -9,16 +9,34 @@ django.setup()
import click
from datetime import datetime, timedelta
from boards.models import Article
from django.conf import settings
DEFAULT_CLEANUP_DAYS = 300
from boards.models import Article, BoardFeed
@click.command()
@click.option('--older-than-days', default=DEFAULT_CLEANUP_DAYS, help="Num days to cleanup older articles")
def cleanup(older_than_days):
@click.option(
'--older-than-days',
default=settings.OLD_ARTICLES_CLEANUP_AFTER_DAYS,
help="Num days to cleanup older articles"
)
@click.option(
'--more-than-amount',
default=settings.OLD_ARTICLES_CLEANUP_AFTER_AMOUNT,
help="Max amount of articles allowed in feed"
)
def cleanup(older_than_days, more_than_amount):
click.echo(f"Cleaning up articles older than {older_than_days} days...")
Article.objects.filter(created_at__lte=datetime.utcnow() - timedelta(days=older_than_days)).delete()
for feed in BoardFeed.objects.all():
click.echo(f"Cleaning up feed {feed.name}, leaving {more_than_amount} last articles...")
last_article_to_leave = Article.objects\
.filter(feed=feed)\
.order_by("created_at")[more_than_amount:more_than_amount + 1].first()
if last_article_to_leave:
num_deleted, _ = Article.objects.filter(feed=feed, created_at__gt=last_article_to_leave.created_at).delete()
click.echo(f"Deleted {num_deleted} old articles!")
click.echo("Done")
if __name__ == '__main__':