2019-12-30 20:23:42 +01:00
|
|
|
import logging.handlers
|
2019-12-30 21:02:20 +01:00
|
|
|
from typing import Optional, List
|
2019-12-30 20:23:42 +01:00
|
|
|
|
2019-12-28 13:31:34 +01:00
|
|
|
import click
|
2019-12-30 19:48:24 +01:00
|
|
|
import html2text
|
2019-12-28 13:31:34 +01:00
|
|
|
import pendulum
|
|
|
|
from mastodon import Mastodon
|
|
|
|
|
2019-12-30 20:29:27 +01:00
|
|
|
from cleantoots.utils import _config_has_sections, CleanTootsConfig
|
2019-12-28 13:31:34 +01:00
|
|
|
|
2019-12-30 20:23:42 +01:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2019-12-30 19:48:24 +01:00
|
|
|
CONTENT_PREVIEW = 78
|
|
|
|
|
2019-12-28 13:31:34 +01:00
|
|
|
|
|
|
|
@click.command()
|
|
|
|
@click.option(
|
|
|
|
"--delete",
|
|
|
|
help="Delete toots that match the rules without confirmation. This is a destructive operation. "
|
|
|
|
"Without this flags, toots will only be listed.",
|
|
|
|
is_flag=True,
|
|
|
|
)
|
2019-12-30 20:23:42 +01:00
|
|
|
@click.option(
|
|
|
|
"--headless", help="Use to make output more logging friendly.", is_flag=True
|
|
|
|
)
|
2019-12-28 13:31:34 +01:00
|
|
|
@click.pass_obj
|
2019-12-30 20:29:27 +01:00
|
|
|
def clean(config: CleanTootsConfig, delete: bool, headless: bool):
|
2019-12-28 13:31:34 +01:00
|
|
|
"""
|
|
|
|
Delete Toots based on rules in config file.
|
|
|
|
|
|
|
|
Without the `--delete` flag, toots will only be displayed.
|
|
|
|
"""
|
|
|
|
if not _config_has_sections(config):
|
|
|
|
return
|
2019-12-30 19:48:24 +01:00
|
|
|
h = html2text.HTML2Text()
|
|
|
|
h.ignore_links = True
|
|
|
|
h.ignore_emphasis = True
|
|
|
|
h.ignore_images = True
|
|
|
|
h.ignore_tables = True
|
|
|
|
|
2019-12-28 13:31:34 +01:00
|
|
|
for section in config.sections():
|
|
|
|
section = config[section]
|
|
|
|
user_secret_file = config.file(section.get("user_secret_file"))
|
|
|
|
mastodon = Mastodon(access_token=user_secret_file)
|
|
|
|
user = mastodon.me()
|
|
|
|
page = mastodon.account_statuses(user["id"])
|
|
|
|
would_delete = []
|
2019-12-30 21:02:20 +01:00
|
|
|
protected = []
|
2019-12-28 13:31:34 +01:00
|
|
|
while page:
|
|
|
|
for toot in page:
|
2019-12-30 21:02:20 +01:00
|
|
|
protection_reason = _toot_protection_reason(toot, section)
|
|
|
|
if protection_reason:
|
|
|
|
protected.append({"toot": toot, "reason": protection_reason})
|
|
|
|
else:
|
|
|
|
would_delete.append(toot)
|
2019-12-28 13:31:34 +01:00
|
|
|
|
|
|
|
page = mastodon.fetch_next(page)
|
|
|
|
|
2019-12-30 21:02:20 +01:00
|
|
|
_delete_or_log(delete, h, headless, mastodon, protected, would_delete)
|
|
|
|
|
|
|
|
|
|
|
|
def _delete_or_log(delete, html_handler, headless, mastodon, protected, would_delete):
|
|
|
|
if not delete:
|
|
|
|
_log_item_list(
|
|
|
|
would_delete,
|
|
|
|
headless,
|
|
|
|
html_handler,
|
|
|
|
no_item_message="No toot would be deleted given the rules.",
|
|
|
|
count_message_format="Would delete {count} toots/boost:",
|
|
|
|
)
|
|
|
|
_log_item_list(
|
|
|
|
protected,
|
|
|
|
headless,
|
|
|
|
html_handler,
|
|
|
|
no_item_message="No toot would be protected given the rules.",
|
|
|
|
count_message_format="Would protect {count} toots/boost:",
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
log("Deleting toots...", headless)
|
|
|
|
with click.progressbar(would_delete) as bar:
|
|
|
|
for toot in bar:
|
|
|
|
mastodon.status_delete(toot)
|
|
|
|
log("Deleted {}".format(_format_toot(toot)), headless, fg="green")
|
|
|
|
|
|
|
|
|
|
|
|
def _log_item_list(
|
|
|
|
items: List[dict],
|
|
|
|
headless: bool,
|
|
|
|
html_handler: html2text.HTML2Text,
|
|
|
|
no_item_message: str,
|
|
|
|
count_message_format: str,
|
|
|
|
):
|
|
|
|
if not items:
|
|
|
|
log(no_item_message, headless, fg="blue")
|
|
|
|
else:
|
|
|
|
log(
|
|
|
|
count_message_format.format(count=len(items)), headless, fg="blue",
|
|
|
|
)
|
|
|
|
for toot in items:
|
|
|
|
_log_item(toot, headless, html_handler)
|
|
|
|
|
|
|
|
|
|
|
|
def _log_item(item, headless, html_handler):
|
|
|
|
if "reason" in item and "toot" in item:
|
|
|
|
toot = item["toot"]
|
|
|
|
reason = item["reason"]
|
|
|
|
else:
|
|
|
|
toot = item
|
|
|
|
reason = ""
|
|
|
|
message = _format_toot(toot, reason)
|
|
|
|
log(message, headless, bold=True)
|
|
|
|
content = html_handler.handle(toot["content"]).replace("\n", " ").strip()
|
|
|
|
if len(content) > CONTENT_PREVIEW:
|
|
|
|
content = content[: CONTENT_PREVIEW - 3] + "..."
|
|
|
|
else:
|
|
|
|
content = content[:CONTENT_PREVIEW]
|
|
|
|
log(content, headless)
|
|
|
|
log("", headless)
|
|
|
|
|
|
|
|
|
|
|
|
def _format_toot(toot: dict, protection_reason: str = ""):
|
2019-12-30 19:48:24 +01:00
|
|
|
if toot.get("reblog"):
|
2019-12-30 20:23:42 +01:00
|
|
|
message = f"boost of toot {toot['reblog']['url']}"
|
2019-12-30 19:48:24 +01:00
|
|
|
else:
|
2019-12-30 20:23:42 +01:00
|
|
|
message = f"original toot {toot['url']}"
|
2019-12-30 21:02:20 +01:00
|
|
|
if protection_reason:
|
2020-05-23 09:42:13 +02:00
|
|
|
message = f"{message} protected because {protection_reason}"
|
2019-12-30 19:48:24 +01:00
|
|
|
return message
|
2019-12-30 20:23:42 +01:00
|
|
|
|
|
|
|
|
2019-12-30 21:02:20 +01:00
|
|
|
def log(message: str, headless: bool, level: int = logging.INFO, *args, **kwargs):
|
2019-12-30 20:23:42 +01:00
|
|
|
if headless:
|
|
|
|
if message and message.strip():
|
|
|
|
logger.log(level, message)
|
|
|
|
else:
|
|
|
|
click.secho(message, *args, **kwargs)
|
2019-12-30 21:02:20 +01:00
|
|
|
|
|
|
|
|
|
|
|
def _toot_protection_reason(toot: dict, section) -> Optional[str]:
|
|
|
|
"""
|
|
|
|
Return a protection reason or None if the toot should not be protected.
|
|
|
|
|
|
|
|
:param toot: The toot to check.
|
|
|
|
:param section: The section of the config file to check against.
|
|
|
|
:return: The protection reason or None.
|
|
|
|
"""
|
|
|
|
boost_count = toot["reblogs_count"]
|
|
|
|
favorite_count = toot["favourites_count"]
|
|
|
|
id_ = toot["id"]
|
|
|
|
original_id = None
|
|
|
|
if toot.get("reblog"):
|
|
|
|
original_id = toot["reblog"].get("id")
|
|
|
|
created_at = toot["created_at"]
|
|
|
|
protected_toots = map(int, section.get("protected_toots", "").split())
|
2020-05-23 09:42:13 +02:00
|
|
|
protected_tags = section.get("protected_tags", "").lower().split()
|
2019-12-30 21:02:20 +01:00
|
|
|
time_limit = pendulum.now(tz=section.get("timezone")).subtract(
|
|
|
|
days=section.getint("days_count")
|
|
|
|
)
|
2020-05-23 09:42:13 +02:00
|
|
|
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
|
|
|
|
)
|
2019-12-30 21:02:20 +01:00
|
|
|
if id_ in protected_toots or original_id in protected_toots:
|
2020-05-23 09:42:13 +02:00
|
|
|
return "{} or {} is a protected id".format(id_, original_id)
|
|
|
|
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)
|
2020-05-23 10:26:28 +02:00
|
|
|
if created_at >= time_limit:
|
|
|
|
return "creation time {} is later than limit {}".format(created_at, time_limit)
|
2019-12-30 21:02:20 +01:00
|
|
|
|
|
|
|
return None
|