from typing import Any
import bottle # type: ignore
from tinydb import Query
from app.DatabaseManager import database_manager
def feed_id_to_td(feed_id: str) -> str:
return f'
{feed_id} | '
def feed_last_build_date_to_td(last_build_date: str) -> str:
return f"{last_build_date} | "
def feed_to_tr(feed: dict[str, str]) -> str:
return f'{feed_id_to_td(feed["feed_id"])}{feed_last_build_date_to_td(feed.get("feed_last_build_date", ""))}
'
def feeds_to_table(feeds: list[dict[str, str]]) -> str:
if len(feeds) == 0:
return "No feeds (yet). Add feeds by defining them in your Aggrofile. If you did that already, you might have to wait a bit for the data to propagate."
trs: list[str] = [feed_to_tr(feed) for feed in feeds]
thead = f"| Feed | Last build date |
"
tbody = f'{"".join(trs)}'
return ""
@bottle.route("/")
def index():
if database_manager.db is None:
raise Exception("Database is not initialized")
Q = Query()
feeds = database_manager.feeds.all()
bottle.response.set_header("content-type", "text/html")
page = f"""
Aggro – Feed manipulator
Aggro
Feed manipulator
Feeds served at this address
{feeds_to_table(feeds)}
"""
return page
@bottle.route("/")
def feed(feed_id: str):
if database_manager.db is None:
raise Exception("Database is not initialized")
Q = Query()
res = database_manager.feeds.search(Q.feed_id == feed_id)
if len(res) == 0:
bottle.abort(400, f"No feed found with id {feed_id}")
if len(res) > 1:
bottle.abort(400, f"Weird number of feeds found with id {feed_id}: {len(res)}")
bottle.response.set_header("content-type", "application/xml")
feed: Any = res[0] # type: ignore
feed_xml: str = feed["feed_xml"]
return feed_xml
def run_web_server(host: str, port: int):
bottle.run(host=host, port=port)