Display protected toots and reason. Close #4

This commit is contained in:
Gabriel Augendre 2019-12-30 21:02:20 +01:00
parent 2b6698ee98
commit 5ffa45c3c5
No known key found for this signature in database
GPG key ID: 1E693F4CE4AEE7B4

View file

@ -1,4 +1,5 @@
import logging.handlers import logging.handlers
from typing import Optional, List
import click import click
import html2text import html2text
@ -44,8 +45,105 @@ def clean(config: CleanTootsConfig, delete: bool, headless: bool):
user = mastodon.me() user = mastodon.me()
page = mastodon.account_statuses(user["id"]) page = mastodon.account_statuses(user["id"])
would_delete = [] would_delete = []
protected = []
while page: while page:
for toot in page: for toot in page:
protection_reason = _toot_protection_reason(toot, section)
if protection_reason:
protected.append({"toot": toot, "reason": protection_reason})
else:
would_delete.append(toot)
page = mastodon.fetch_next(page)
_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 = ""):
if toot.get("reblog"):
message = f"boost of toot {toot['reblog']['url']}"
else:
message = f"original toot {toot['url']}"
if protection_reason:
message = f"{message} protected because of {protection_reason}"
return message
def log(message: str, headless: bool, level: int = logging.INFO, *args, **kwargs):
if headless:
if message and message.strip():
logger.log(level, message)
else:
click.secho(message, *args, **kwargs)
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"] boost_count = toot["reblogs_count"]
favorite_count = toot["favourites_count"] favorite_count = toot["favourites_count"]
id_ = toot["id"] id_ = toot["id"]
@ -57,56 +155,13 @@ def clean(config: CleanTootsConfig, delete: bool, headless: bool):
time_limit = pendulum.now(tz=section.get("timezone")).subtract( time_limit = pendulum.now(tz=section.get("timezone")).subtract(
days=section.getint("days_count") days=section.getint("days_count")
) )
if ( if boost_count >= section.getint("boost_limit"):
boost_count >= section.getint("boost_limit") return "boost count"
or favorite_count >= section.getint("favorite_limit") if favorite_count >= section.getint("favorite_limit"):
or id_ in protected_toots return "favorite count"
or original_id in protected_toots if id_ in protected_toots or original_id in protected_toots:
or created_at >= time_limit return "protected id"
): if created_at >= time_limit:
continue return "creation time"
would_delete.append(toot)
page = mastodon.fetch_next(page) return None
if not delete:
if not would_delete:
log("No toot would be deleted given the rules.", headless, fg="blue")
return
log(
"Would delete {count} toots/boost:".format(count=len(would_delete)),
headless,
fg="blue",
)
for toot in would_delete:
message = format_toot(toot)
log(message, headless, bold=True)
content = h.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)
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 format_toot(toot):
if toot.get("reblog"):
message = f"boost of toot {toot['reblog']['url']}"
else:
message = f"original toot {toot['url']}"
return message
def log(message, headless, level=logging.INFO, *args, **kwargs):
if headless:
if message and message.strip():
logger.log(level, message)
else:
click.secho(message, *args, **kwargs)