2024-10-22 21:49:40 +01:00
|
|
|
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
|
|
|
|
|
2024-10-22 21:56:26 +01:00
|
|
|
client = AsyncClient(follow_redirects=True)
|
2024-10-22 21:49:40 +01:00
|
|
|
|
|
|
|
FEED_URL = "https://lwn.net/headlines/rss"
|
|
|
|
|
|
|
|
|
|
|
|
async def index(request: Request) -> Response:
|
2024-10-22 21:54:29 +01:00
|
|
|
feed_response = await client.get(FEED_URL)
|
|
|
|
|
|
|
|
raw_feed = await feed_response.aread()
|
2024-10-22 21:49:40 +01:00
|
|
|
|
|
|
|
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)]
|
|
|
|
)
|