from bs4 import BeautifulSoup from httpx import AsyncClient from starlette.applications import Starlette from starlette.requests import Request from starlette.responses import Response from starlette.routing import Route client = AsyncClient() FEED_URL = "https://lwn.net/headlines/rss" async def index(request: Request) -> Response: feed_response = await client.get(FEED_URL) raw_feed = await feed_response.aread() feed = BeautifulSoup(raw_feed, "xml") for item in feed.find_all("item"): title = item.find("title").text # Hide entries behind a paywall if title.startswith("[$]"): item.extract() return Response(content=feed.encode(), media_type="application/xml") async def health(request: Request) -> Response: return Response() app = Starlette( routes=[Route("/", endpoint=index), Route("/.health/", endpoint=health)] )