diff --git a/cleantoots/commands/clean.py b/cleantoots/commands/clean.py index 87dd5fb..f4dabba 100644 --- a/cleantoots/commands/clean.py +++ b/cleantoots/commands/clean.py @@ -1,9 +1,12 @@ import click +import html2text import pendulum from mastodon import Mastodon from cleantoots.utils import _config_has_sections +CONTENT_PREVIEW = 78 + @click.command() @click.option( @@ -21,6 +24,12 @@ def clean(config, delete): """ if not _config_has_sections(config): return + h = html2text.HTML2Text() + h.ignore_links = True + h.ignore_emphasis = True + h.ignore_images = True + h.ignore_tables = True + for section in config.sections(): section = config[section] user_secret_file = config.file(section.get("user_secret_file")) @@ -30,15 +39,23 @@ def clean(config, delete): would_delete = [] while page: for toot in page: + 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()) + time_limit = pendulum.now(tz=section.get("timezone")).subtract( + days=section.getint("days_count") + ) if ( - toot["reblogs_count"] >= section.getint("boost_limit") - or toot["favourites_count"] >= section.getint("favorite_limit") - or toot["id"] - in map(int, section.get("protected_toots", "").split()) - or toot["created_at"] - >= pendulum.now(tz=section.get("timezone")).subtract( - days=section.getint("days_count") - ) + boost_count >= section.getint("boost_limit") + or favorite_count >= section.getint("favorite_limit") + or id_ in protected_toots + or original_id in protected_toots + or created_at >= time_limit ): continue would_delete.append(toot) @@ -50,15 +67,30 @@ def clean(config, delete): click.secho("No toot would be deleted given the rules.", fg="blue") return click.secho( - "Would delete {count} toots:".format(count=len(would_delete)), fg="blue" + "Would delete {count} toots/boost:".format(count=len(would_delete)), + fg="blue", ) for toot in would_delete: - click.echo(toot["id"]) - click.echo(toot["content"]) + message = format_toot(toot, prefix="=== ", separator=" ", suffix=" ===") + click.echo(message) + content = h.handle(toot["content"]).replace("\n", " ").strip() + if len(content) > CONTENT_PREVIEW: + content = content[: CONTENT_PREVIEW - 3] + "..." + else: + content = content[:CONTENT_PREVIEW] + click.echo(content) click.echo() else: click.echo("Deleting toots...") with click.progressbar(would_delete) as bar: for toot in bar: mastodon.status_delete(toot) - click.secho("Deleted toot {}".format(toot["id"]), fg="green") + click.secho("Deleted {}".format(format_toot(toot)), fg="green") + + +def format_toot(toot, prefix="", separator="\t", suffix=""): + if toot.get("reblog"): + message = f"{prefix}boost of{separator}{toot['reblog']['url']}{suffix}" + else: + message = f"{prefix}original toot{separator}{toot['url']}{suffix}" + return message diff --git a/poetry.lock b/poetry.lock index 6c193aa..c17276b 100644 --- a/poetry.lock +++ b/poetry.lock @@ -101,6 +101,14 @@ optional = false python-versions = ">=2.6, !=3.0.*, !=3.1.*" version = "4.4.1" +[[package]] +category = "main" +description = "Turn HTML into equivalent Markdown-structured text." +name = "html2text" +optional = false +python-versions = ">=3.5" +version = "2019.9.26" + [[package]] category = "main" description = "Internationalized Domain Names in Applications (IDNA)" @@ -366,7 +374,7 @@ docs = ["sphinx", "jaraco.packaging (>=3.2)", "rst.linker (>=1.9)"] testing = ["pathlib2", "contextlib2", "unittest2"] [metadata] -content-hash = "ae20f80e556f01fcec81f2f9cf6a15749ff812430878444b2deace101f18b691" +content-hash = "96d2794b9ab59ac4b591ce10e00298bc515e1f67bdae75a212814eef7f34f533" python-versions = ">=3.6" [metadata.files] @@ -410,6 +418,10 @@ decorator = [ {file = "decorator-4.4.1-py2.py3-none-any.whl", hash = "sha256:5d19b92a3c8f7f101c8dd86afd86b0f061a8ce4540ab8cd401fa2542756bce6d"}, {file = "decorator-4.4.1.tar.gz", hash = "sha256:54c38050039232e1db4ad7375cfce6748d7b41c29e95a081c8a6d2c30364a2ce"}, ] +html2text = [ + {file = "html2text-2019.9.26-py3-none-any.whl", hash = "sha256:55ce85704f244fc18890c5ded89fa22ff7333e41e9f3cad04d51f48d62ad8834"}, + {file = "html2text-2019.9.26.tar.gz", hash = "sha256:6f56057c5c2993b5cc5b347cb099bdf6d095828fef1b53ef4e2a2bf2a1be9b4f"}, +] idna = [ {file = "idna-2.8-py2.py3-none-any.whl", hash = "sha256:ea8b7f6188e6fa117537c3df7da9fc686d485087abf6ac197f9c46432f7e4a3c"}, {file = "idna-2.8.tar.gz", hash = "sha256:c357b3f628cf53ae2c4c05627ecc484553142ca23264e593d327bcde5e9c3407"}, diff --git a/pyproject.toml b/pyproject.toml index 23c370e..828196f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -20,6 +20,7 @@ python = ">=3.6" "Mastodon.py" = "^1.5.0" click = "^7.0" pendulum = "^2.0.5" +html2text = "^2019.9.26" [tool.poetry.dev-dependencies] pytest = "^5.2"