Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[nyarlathotep] Unescape htmlentities in rss-to-mastodon #298

Merged
merged 2 commits into from
Sep 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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