Display links of toots and only one line of content. Close #5
This commit is contained in:
parent
ce5c68957a
commit
64dc52ca7f
3 changed files with 58 additions and 13 deletions
|
@ -1,9 +1,12 @@
|
||||||
import click
|
import click
|
||||||
|
import html2text
|
||||||
import pendulum
|
import pendulum
|
||||||
from mastodon import Mastodon
|
from mastodon import Mastodon
|
||||||
|
|
||||||
from cleantoots.utils import _config_has_sections
|
from cleantoots.utils import _config_has_sections
|
||||||
|
|
||||||
|
CONTENT_PREVIEW = 78
|
||||||
|
|
||||||
|
|
||||||
@click.command()
|
@click.command()
|
||||||
@click.option(
|
@click.option(
|
||||||
|
@ -21,6 +24,12 @@ def clean(config, delete):
|
||||||
"""
|
"""
|
||||||
if not _config_has_sections(config):
|
if not _config_has_sections(config):
|
||||||
return
|
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():
|
for section in config.sections():
|
||||||
section = config[section]
|
section = config[section]
|
||||||
user_secret_file = config.file(section.get("user_secret_file"))
|
user_secret_file = config.file(section.get("user_secret_file"))
|
||||||
|
@ -30,15 +39,23 @@ def clean(config, delete):
|
||||||
would_delete = []
|
would_delete = []
|
||||||
while page:
|
while page:
|
||||||
for toot in page:
|
for toot in page:
|
||||||
if (
|
boost_count = toot["reblogs_count"]
|
||||||
toot["reblogs_count"] >= section.getint("boost_limit")
|
favorite_count = toot["favourites_count"]
|
||||||
or toot["favourites_count"] >= section.getint("favorite_limit")
|
id_ = toot["id"]
|
||||||
or toot["id"]
|
original_id = None
|
||||||
in map(int, section.get("protected_toots", "").split())
|
if toot.get("reblog"):
|
||||||
or toot["created_at"]
|
original_id = toot["reblog"].get("id")
|
||||||
>= pendulum.now(tz=section.get("timezone")).subtract(
|
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")
|
days=section.getint("days_count")
|
||||||
)
|
)
|
||||||
|
if (
|
||||||
|
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
|
continue
|
||||||
would_delete.append(toot)
|
would_delete.append(toot)
|
||||||
|
@ -50,15 +67,30 @@ def clean(config, delete):
|
||||||
click.secho("No toot would be deleted given the rules.", fg="blue")
|
click.secho("No toot would be deleted given the rules.", fg="blue")
|
||||||
return
|
return
|
||||||
click.secho(
|
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:
|
for toot in would_delete:
|
||||||
click.echo(toot["id"])
|
message = format_toot(toot, prefix="=== ", separator=" ", suffix=" ===")
|
||||||
click.echo(toot["content"])
|
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()
|
click.echo()
|
||||||
else:
|
else:
|
||||||
click.echo("Deleting toots...")
|
click.echo("Deleting toots...")
|
||||||
with click.progressbar(would_delete) as bar:
|
with click.progressbar(would_delete) as bar:
|
||||||
for toot in bar:
|
for toot in bar:
|
||||||
mastodon.status_delete(toot)
|
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
|
||||||
|
|
14
poetry.lock
generated
14
poetry.lock
generated
|
@ -101,6 +101,14 @@ optional = false
|
||||||
python-versions = ">=2.6, !=3.0.*, !=3.1.*"
|
python-versions = ">=2.6, !=3.0.*, !=3.1.*"
|
||||||
version = "4.4.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]]
|
[[package]]
|
||||||
category = "main"
|
category = "main"
|
||||||
description = "Internationalized Domain Names in Applications (IDNA)"
|
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"]
|
testing = ["pathlib2", "contextlib2", "unittest2"]
|
||||||
|
|
||||||
[metadata]
|
[metadata]
|
||||||
content-hash = "ae20f80e556f01fcec81f2f9cf6a15749ff812430878444b2deace101f18b691"
|
content-hash = "96d2794b9ab59ac4b591ce10e00298bc515e1f67bdae75a212814eef7f34f533"
|
||||||
python-versions = ">=3.6"
|
python-versions = ">=3.6"
|
||||||
|
|
||||||
[metadata.files]
|
[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-py2.py3-none-any.whl", hash = "sha256:5d19b92a3c8f7f101c8dd86afd86b0f061a8ce4540ab8cd401fa2542756bce6d"},
|
||||||
{file = "decorator-4.4.1.tar.gz", hash = "sha256:54c38050039232e1db4ad7375cfce6748d7b41c29e95a081c8a6d2c30364a2ce"},
|
{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 = [
|
idna = [
|
||||||
{file = "idna-2.8-py2.py3-none-any.whl", hash = "sha256:ea8b7f6188e6fa117537c3df7da9fc686d485087abf6ac197f9c46432f7e4a3c"},
|
{file = "idna-2.8-py2.py3-none-any.whl", hash = "sha256:ea8b7f6188e6fa117537c3df7da9fc686d485087abf6ac197f9c46432f7e4a3c"},
|
||||||
{file = "idna-2.8.tar.gz", hash = "sha256:c357b3f628cf53ae2c4c05627ecc484553142ca23264e593d327bcde5e9c3407"},
|
{file = "idna-2.8.tar.gz", hash = "sha256:c357b3f628cf53ae2c4c05627ecc484553142ca23264e593d327bcde5e9c3407"},
|
||||||
|
|
|
@ -20,6 +20,7 @@ python = ">=3.6"
|
||||||
"Mastodon.py" = "^1.5.0"
|
"Mastodon.py" = "^1.5.0"
|
||||||
click = "^7.0"
|
click = "^7.0"
|
||||||
pendulum = "^2.0.5"
|
pendulum = "^2.0.5"
|
||||||
|
html2text = "^2019.9.26"
|
||||||
|
|
||||||
[tool.poetry.dev-dependencies]
|
[tool.poetry.dev-dependencies]
|
||||||
pytest = "^5.2"
|
pytest = "^5.2"
|
||||||
|
|
Loading…
Reference in a new issue