40 lines
965 B
Python
40 lines
965 B
Python
|
from aiocache import cached
|
||
|
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"
|
||
|
|
||
|
|
||
|
@cached(ttl=3600)
|
||
|
async def get_feed() -> str:
|
||
|
return (await client.get(FEED_URL)).content
|
||
|
|
||
|
|
||
|
async def index(request: Request) -> Response:
|
||
|
raw_feed = await get_feed()
|
||
|
|
||
|
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)]
|
||
|
)
|