Last active
December 20, 2022 23:28
-
-
Save vgel/d7d954cc83c7b411ed935a6ee7b8a8f3 to your computer and use it in GitHub Desktop.
Simple RSS proxy to add title elements to a Mastodon feed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# put in rss_proxy/ as app.py and run with `python -m flask run` | |
# requirements: flask beautifulsoup4 requests lxml | |
# usage: `curl localhost:5000?feed=https://instance.example.com/users/username.rss` | |
from bs4 import BeautifulSoup | |
from flask import Flask, request | |
import requests | |
from lxml.builder import E | |
import lxml.etree | |
app = Flask(__name__) | |
@app.route("/", methods=["GET"]) | |
def index(): | |
feed = requests.get(request.args["feed"]) | |
feed.raise_for_status() | |
feed_xml = lxml.etree.fromstring(feed.text.encode("utf-8"), parser=None) | |
for tag in feed_xml.find("channel").findall("item"): | |
desc = BeautifulSoup(tag.find("description").text).text # strip html tags | |
truncated = desc[:100] # or desired length | |
tag.append(E.title(truncated)) | |
return lxml.etree.tostring(feed_xml) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment