Skip to content

Commit

Permalink
Merge pull request #298 from barrucadu/nya/rss-to-mastodon-entities
Browse files Browse the repository at this point in the history
[nyarlathotep] Unescape htmlentities in rss-to-mastodon
  • Loading branch information
barrucadu authored Sep 4, 2024
2 parents 7dbd926 + e2b73c4 commit 76fcf7e
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions hosts/nyarlathotep/jobs/rss-to-mastodon.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
Requires the API_KEY environment variable to be set.
Usage:
rss-to-mastodon -d <domain> -f <feed-url> -l <history-file> [-e <entries>] [-v <visibility>]
rss-to-mastodon [--dry-run] -d <domain> -f <feed-url> -l <history-file> [-e <entries>] [-v <visibility>]
Options:
--dry-run just print what would be published
-d <domain> api domain
-f <feed-url> rss feed URL
-l <history-file> file to log feed item IDs to (to prevent double-posting)
Expand All @@ -17,24 +18,28 @@

import docopt
import feedparser
import html.parser
import http.client
import os
import pathlib
import requests
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:
Expand All @@ -59,18 +64,24 @@

# 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={
"Authorization": f"Bearer {api_token}",
"Idempotency-Key": item["id"],
},
json={
"status": item["title"],
"status": title,
"visibility": visibility,
},
).raise_for_status()
Expand Down

0 comments on commit 76fcf7e

Please sign in to comment.