diff --git a/hosts/nyarlathotep/jobs/rss-to-mastodon.py b/hosts/nyarlathotep/jobs/rss-to-mastodon.py index bc86d57a..1b851714 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) @@ -17,6 +18,7 @@ import docopt import feedparser +import html.parser import http.client import os import pathlib @@ -24,17 +26,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: @@ -59,10 +64,16 @@ # 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: + continue + requests.post( f"{api_domain}/api/v1/statuses", headers={ @@ -70,7 +81,7 @@ "Idempotency-Key": item["id"], }, json={ - "status": item["title"], + "status": title, "visibility": visibility, }, ).raise_for_status()