From 094d179915af1449083ed110bb885f104ae5ed66 Mon Sep 17 00:00:00 2001 From: Michael Walker Date: Wed, 4 Sep 2024 16:46:34 +0100 Subject: [PATCH 1/2] [nyarlathotep] Add dry-run mode to rss-to-mastodon Handy for debugging! --- hosts/nyarlathotep/jobs/rss-to-mastodon.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/hosts/nyarlathotep/jobs/rss-to-mastodon.py b/hosts/nyarlathotep/jobs/rss-to-mastodon.py index bc86d57a..deaf54d5 100644 --- a/hosts/nyarlathotep/jobs/rss-to-mastodon.py +++ b/hosts/nyarlathotep/jobs/rss-to-mastodon.py @@ -5,9 +5,10 @@ Requires the API_KEY environment variable to be set. Usage: - rss-to-mastodon -d -f -l [-e ] [-v ] + rss-to-mastodon [--dry-run] -d -f -l [-e ] [-v ] Options: + --dry-run just print what would be published -d api domain -f rss feed URL -l file to log feed item IDs to (to prevent double-posting) @@ -24,17 +25,20 @@ import sys import time -api_token = os.getenv("API_KEY") -if api_token is None: - raise Exception("missing API key") - args = docopt.docopt(__doc__) +dry_run = args["--dry-run"] api_domain = args["-d"] feed_url = args["-f"] history_file = pathlib.Path(args["-l"]) entries = int(args["-e"]) visibility = args["-v"] +if not dry_run: + api_token = os.getenv("API_KEY") + if api_token is None: + print("missing API key", file=sys.stderr) + sys.exit(1) + attempts = 0 feed = None while attempts < 5: @@ -63,6 +67,9 @@ print(item["title"]) print() + if dry_run: + continue + requests.post( f"{api_domain}/api/v1/statuses", headers={ From e2b73c47677844bc0b6e9ad2d33f0d3b59190c1d Mon Sep 17 00:00:00 2001 From: Michael Walker Date: Wed, 4 Sep 2024 16:54:53 +0100 Subject: [PATCH 2/2] [nyarlathotep] Unescape htmlentities in rss-to-mastodon https://hacksrus.xyz/notice/Alev7xG7RAsSRdEaie --- hosts/nyarlathotep/jobs/rss-to-mastodon.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/hosts/nyarlathotep/jobs/rss-to-mastodon.py b/hosts/nyarlathotep/jobs/rss-to-mastodon.py index deaf54d5..1b851714 100644 --- a/hosts/nyarlathotep/jobs/rss-to-mastodon.py +++ b/hosts/nyarlathotep/jobs/rss-to-mastodon.py @@ -18,6 +18,7 @@ import docopt import feedparser +import html.parser import http.client import os import pathlib @@ -63,8 +64,11 @@ # if there are multiple items, post the older ones first for item in reversed(items): + # handle entities + title = html.parser.unescape(item["title"]) + print(item["id"]) - print(item["title"]) + print(title) print() if dry_run: @@ -77,7 +81,7 @@ "Idempotency-Key": item["id"], }, json={ - "status": item["title"], + "status": title, "visibility": visibility, }, ).raise_for_status()