This repository has been archived by the owner on Dec 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
78 lines (55 loc) · 2.14 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
from datetime import timedelta
from cachetools import cached, TTLCache
from fastapi import FastAPI, Request
from fastapi.responses import FileResponse, HTMLResponse
from fastapi.templating import Jinja2Templates
from feedparser import parse
app = FastAPI()
templates = Jinja2Templates(directory="templates")
AWS_BLOG_CONTAINERS_URL = "https://aws.amazon.com/blogs/containers/feed/"
@cached(cache=TTLCache(maxsize=1, ttl=timedelta(minutes=5).total_seconds()))
def get_feed(url):
feed = parse(url)
return feed
def filter_feed(entries, filter_term: str) -> set:
# Iterate through entries and find ones that contains the `filter_term`.
# Return a set of unique entries.
relevant_entries = {
entry
for entry in entries
for tag in entry.tags
if filter_term.lower() in tag.get("term", "").lower()
}
return relevant_entries
def get_blog_entries(filter_term: str) -> set:
feed = get_feed(AWS_BLOG_CONTAINERS_URL)
relevant_entries = filter_feed(feed.entries, filter_term)
return relevant_entries
@app.get("/", response_class=HTMLResponse)
def read_root(request: Request):
relevant_blog_entries = get_blog_entries(filter_term="ecs")
return templates.TemplateResponse(
"index.html.jinja",
{"request": request, "blog_entries": relevant_blog_entries},
)
@app.get("/entries", response_class=HTMLResponse)
def read_entries(request: Request, term: str = "ecs"):
relevant_blog_entries = get_blog_entries(filter_term=term)
return templates.TemplateResponse(
"entries.html.jinja",
{"request": request, "term": term, "blog_entries": relevant_blog_entries},
)
@app.get("/json")
def read_json(term: str = "ecs"):
relevant_blog_entries = get_blog_entries(filter_term=term)
return {"blog_entries": relevant_blog_entries}
@app.get("/favicon.ico")
def favicon():
return FileResponse("favicon.ico")
if __name__ == "__main__":
"""
Useful for local development. Debug with VS Code or PyCharm.
https://fastapi.tiangolo.com/tutorial/debugging/#run-your-code-with-your-debugger
"""
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)