aboutsummaryrefslogtreecommitdiffstats
path: root/app
diff options
context:
space:
mode:
authorJan Tuomi <jans.tuomi@gmail.com>2023-09-03 00:12:19 +0300
committerJan Tuomi <jans.tuomi@gmail.com>2023-09-10 19:01:00 +0300
commit3a4cd4935dafe6d445daac3b7d6ced409f99aa23 (patch)
tree751e4ca259f7f35556fdfabc78dfacdedbad9ac5 /app
parentc13734f56e9b01a61cfb66019a71d46c3dfe6f86 (diff)
Add web server
Diffstat (limited to 'app')
-rw-r--r--app/AggroConfig.py2
-rw-r--r--app/FeedSinkPlugin.py52
-rw-r--r--app/database.py12
-rw-r--r--app/server.py31
4 files changed, 60 insertions, 37 deletions
diff --git a/app/AggroConfig.py b/app/AggroConfig.py
index 6ff6801..7e6a5f3 100644
--- a/app/AggroConfig.py
+++ b/app/AggroConfig.py
@@ -3,6 +3,8 @@ from dataclasses import dataclass
@dataclass
class AggroConfig:
+ server_host: str
+ server_port: int
db_path: str
plugins: dict[str, dict[str, str]]
graph: dict[str, list[str]]
diff --git a/app/FeedSinkPlugin.py b/app/FeedSinkPlugin.py
index 6b0125d..2c85167 100644
--- a/app/FeedSinkPlugin.py
+++ b/app/FeedSinkPlugin.py
@@ -1,47 +1,34 @@
from typing import Any
-from app.Item import Item
-from app.PluginInterface import PluginInterface
-from app.utils import get_param
import xml.etree.ElementTree as ET
+from tinydb import Query
-# <?xml version="1.0" encoding="UTF-8" ?>
-# <rss version="2.0">
-
-# <channel>
-# <title>W3Schools Home Page</title>
-# <link>https://www.w3schools.com</link>
-# <description>Free web building tutorials</description>
-# <item>
-# <title>RSS Tutorial</title>
-# <link>https://www.w3schools.com/xml/xml_rss.asp</link>
-# <description>New RSS tutorial on W3Schools</description>
-# </item>
-# <item>
-# <title>XML Tutorial</title>
-# <link>https://www.w3schools.com/xml</link>
-# <description>New XML tutorial on W3Schools</description>
-# </item>
-# </channel>
-
-# </rss>
+from app.Item import Item
+from app.PluginInterface import PluginInterface
+from app.utils import get_param
+from app.database import database_manager
class Plugin(PluginInterface):
def __init__(self, id: str, params: dict[str, Any]) -> None:
super().__init__(id, params)
- self.feed_name: str = get_param("feed_name", params)
+ self.feed_id: str = get_param("feed_id", params)
+ self.feed_title: str = get_param("feed_title", params)
+ self.feed_link: str | None = params.get("feed_link", None)
+ self.feed_description: str = get_param("feed_description", params)
print(f"[FeedSinkPlugin#{self.id}] initialized")
+ print(f"[FeedSinkPlugin#{self.id}] feed will be served at path /{self.feed_id}")
def build_xml(self, items: list[Item]):
rss = ET.Element("rss", {"version": "2.0"})
channel = ET.SubElement(rss, "channel")
title = ET.SubElement(channel, "title")
- title.text = "channel title"
- link = ET.SubElement(channel, "link")
- link.text = "channel link"
+ title.text = self.feed_title
+ if self.feed_link is not None:
+ link = ET.SubElement(channel, "link")
+ link.text = self.feed_link
description = ET.SubElement(channel, "description")
- description.text = "channel description"
+ description.text = self.feed_description
for item in items:
item_elem = ET.SubElement(channel, "item")
@@ -56,10 +43,15 @@ class Plugin(PluginInterface):
def process(self, source_id: str | None, items: list[Item]) -> list[Item]:
print(f"[FeedSinkPlugin#{self.id}] process called, n={len(items)}")
+
if source_id is None:
raise Exception(f"FeedSinkPlugin#{self.id} can not be scheduled")
+ if database_manager.db is None:
+ raise Exception("Database is not initialized")
ret = self.build_xml(items)
- print(f"[FeedSinkPlugin#{self.id}] process returning XML:")
- print(ret)
+
+ Q = Query()
+ database_manager.db.upsert({"feed_id": self.feed_id, "feed_xml": ret}, Q.feed_id == self.feed_id) # type: ignore
+ print(f"[FeedSinkPlugin#{self.id}] processed")
return []
diff --git a/app/database.py b/app/database.py
index fdeac1a..b796dc8 100644
--- a/app/database.py
+++ b/app/database.py
@@ -3,14 +3,12 @@ from tinydb import TinyDB
from app.AggroConfig import AggroConfig
-def setup_db(config: AggroConfig):
- global database_manager
- database_manager = DatabaseManager(config)
-
-
class DatabaseManager:
- def __init__(self, config: AggroConfig):
+ def __init__(self):
+ self.db: TinyDB | None = None
+
+ def setup(self, config: AggroConfig):
self.db = TinyDB(config.db_path)
-database_manager: DatabaseManager
+database_manager: DatabaseManager = DatabaseManager()
diff --git a/app/server.py b/app/server.py
new file mode 100644
index 0000000..785e30f
--- /dev/null
+++ b/app/server.py
@@ -0,0 +1,31 @@
+from typing import Any
+import bottle as _bottle # type: ignore
+from tinydb import Query
+
+from app.database import database_manager
+
+bottle: Any = _bottle
+
+
+@bottle.route("/<feed_id>")
+def index(feed_id: str):
+ if database_manager.db is None:
+ raise Exception("Database is not initialized")
+
+ Q = Query()
+ res = database_manager.db.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)