From 6ea44f6ed38c1f3407790081c3a44b495dbc4a52 Mon Sep 17 00:00:00 2001 From: Gabriel Augendre Date: Sat, 23 May 2020 09:42:13 +0200 Subject: [PATCH] Implement protecting toots by tag --- README.md | 8 ++++++++ cleantoots/commands/clean.py | 23 ++++++++++++++++------- cleantoots/utils.py | 1 + pyproject.toml | 2 +- 4 files changed, 26 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 83606ea..32ffdeb 100644 --- a/README.md +++ b/README.md @@ -60,6 +60,14 @@ protected_toots = 103362008817616000 103361883565013391 103363106195441418 +# Tags you want to protect (never delete). +# Tags are matched case insensitively and are only matched for original toots (not for boosts): +# if you boost a toot containing #ScreenshotSunday it won't be protected by this rule. +# You MUST omit the `#` part of the tag. +protected_tags = 100DaysToOffload + screenshotsunday + + # Another account [Mastodon.social] api_base_url = https://mastodon.social diff --git a/cleantoots/commands/clean.py b/cleantoots/commands/clean.py index 1691fca..2055f32 100644 --- a/cleantoots/commands/clean.py +++ b/cleantoots/commands/clean.py @@ -124,7 +124,7 @@ def _format_toot(toot: dict, protection_reason: str = ""): else: message = f"original toot {toot['url']}" if protection_reason: - message = f"{message} protected because of {protection_reason}" + message = f"{message} protected because {protection_reason}" return message @@ -152,16 +152,25 @@ def _toot_protection_reason(toot: dict, section) -> Optional[str]: original_id = toot["reblog"].get("id") created_at = toot["created_at"] protected_toots = map(int, section.get("protected_toots", "").split()) + protected_tags = section.get("protected_tags", "").lower().split() time_limit = pendulum.now(tz=section.get("timezone")).subtract( days=section.getint("days_count") ) - if boost_count >= section.getint("boost_limit"): - return "boost count" - if favorite_count >= section.getint("favorite_limit"): - return "favorite count" + boost_limit = section.getint("boost_limit") + if boost_count >= boost_limit: + return "boost count is over limit {} >= {}".format(boost_count, boost_limit) + favorite_limit = section.getint("favorite_limit") + if favorite_count >= favorite_limit: + return "favorite count is over limit {} >= {}".format( + favorite_count, favorite_limit + ) if id_ in protected_toots or original_id in protected_toots: - return "protected id" + return "{} or {} is a protected id".format(id_, original_id) if created_at >= time_limit: - return "creation time" + return "creation time {} is later than limit {}".format(created_at, time_limit) + for tag in toot.get("tags", []): + tag_name = tag.get("name").lower() + if tag_name and tag_name in protected_tags: + return "{} is a protected tag".format(tag_name) return None diff --git a/cleantoots/utils.py b/cleantoots/utils.py index 33db5ec..382db06 100644 --- a/cleantoots/utils.py +++ b/cleantoots/utils.py @@ -47,6 +47,7 @@ def _get_default_config(): "app_secret_file": "mastodon_social_app.secret", "user_secret_file": "mastodon_social_user.secret", "protected_toots": "1234\n5678", + "protected_tags": "TagToProtect", } return default_config diff --git a/pyproject.toml b/pyproject.toml index 3fd9035..addb46c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "cleantoots" -version = "0.3.6" +version = "0.4.0" description = "Cleanup your toot history." license = "GPL-3.0-or-later" authors = ["Gabriel Augendre "]