Skip to content

Commit 2ab857a

Browse files
committed
scripts: improve cleanup script
1 parent aadefbc commit 2ab857a

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

boards.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ boards:
1919
- name: TJ
2020
url: https://tjournal.ru
2121
rss: https://tjournal.ru/rss/all
22+
icon: https://i.vas3k.ru/bba63f7248fb952e4157c70b83bfd769d0328ed14055addf7756d1ad191bad05.jpg
2223
is_parsable: false
2324
- name: Коммерсантъ
2425
url: https://www.kommersant.ru/

infomate/settings.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,9 @@
119119

120120
BLEACH_STRIP_TAGS = True
121121

122+
OLD_ARTICLES_CLEANUP_AFTER_DAYS = 300
123+
OLD_ARTICLES_CLEANUP_AFTER_AMOUNT = 150
124+
122125
if SENTRY_DSN and not DEBUG:
123126
sentry_sdk.init(
124127
dsn=SENTRY_DSN,

scripts/cleanup.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,34 @@
99
import click
1010
from datetime import datetime, timedelta
1111

12-
from boards.models import Article
12+
from django.conf import settings
1313

14-
15-
DEFAULT_CLEANUP_DAYS = 300
14+
from boards.models import Article, BoardFeed
1615

1716

1817
@click.command()
19-
@click.option('--older-than-days', default=DEFAULT_CLEANUP_DAYS, help="Num days to cleanup older articles")
20-
def cleanup(older_than_days):
18+
@click.option(
19+
'--older-than-days',
20+
default=settings.OLD_ARTICLES_CLEANUP_AFTER_DAYS,
21+
help="Num days to cleanup older articles"
22+
)
23+
@click.option(
24+
'--more-than-amount',
25+
default=settings.OLD_ARTICLES_CLEANUP_AFTER_AMOUNT,
26+
help="Max amount of articles allowed in feed"
27+
)
28+
def cleanup(older_than_days, more_than_amount):
29+
click.echo(f"Cleaning up articles older than {older_than_days} days...")
2130
Article.objects.filter(created_at__lte=datetime.utcnow() - timedelta(days=older_than_days)).delete()
31+
for feed in BoardFeed.objects.all():
32+
click.echo(f"Cleaning up feed {feed.name}, leaving {more_than_amount} last articles...")
33+
last_article_to_leave = Article.objects\
34+
.filter(feed=feed)\
35+
.order_by("created_at")[more_than_amount:more_than_amount + 1].first()
36+
if last_article_to_leave:
37+
num_deleted, _ = Article.objects.filter(feed=feed, created_at__gt=last_article_to_leave.created_at).delete()
38+
click.echo(f"Deleted {num_deleted} old articles!")
39+
click.echo("Done")
2240

2341

2442
if __name__ == '__main__':

0 commit comments

Comments
 (0)