diff options
| author | Jan Tuomi <jans.tuomi@gmail.com> | 2023-09-02 00:38:23 +0300 |
|---|---|---|
| committer | Jan Tuomi <jans.tuomi@gmail.com> | 2023-09-10 19:01:00 +0300 |
| commit | 52418884284b5bf0919c48cf4b02c3bd23dc9e97 (patch) | |
| tree | 932b30bce805c5602c7cf49c1a8ffef05d8f6a18 /app | |
| parent | 398799de0c1826c3544eb6eec681fa48096932a0 (diff) | |
Do stuff
Diffstat (limited to 'app')
| -rw-r--r-- | app/FeedSinkPlugin.py | 62 | ||||
| -rw-r--r-- | app/FeedSourcePlugin.py | 16 | ||||
| -rw-r--r-- | app/FilterPlugin.py | 10 | ||||
| -rw-r--r-- | app/PluginInterface.py | 6 | ||||
| -rw-r--r-- | app/PluginManager.py | 2 |
5 files changed, 76 insertions, 20 deletions
diff --git a/app/FeedSinkPlugin.py b/app/FeedSinkPlugin.py new file mode 100644 index 0000000..c35a811 --- /dev/null +++ b/app/FeedSinkPlugin.py @@ -0,0 +1,62 @@ +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 + + +# <?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> + + +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) + print(f"[FeedSinkPlugin#{self.id}] initialized") + + 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" + description = ET.SubElement(channel, "description") + description.text = "channel description" + + for item in items: + item_elem = ET.SubElement(channel, "item") + item_title = ET.SubElement(item_elem, "title") + item_title.text = item.title + item_link = ET.SubElement(item_elem, "link") + item_link.text = item.link + item_description = ET.SubElement(item_elem, "description") + item_description.text = item.description + + return ET.tostring(rss, "unicode") + + def process(self, items: list[Item]) -> list[Item]: + print(f"[FeedSinkPlugin#{self.id}] process called, n={len(items)}") + ret = self.build_xml(items) + print(f"[FeedSinkPlugin#{self.id}] process returning XML:") + print(ret) + return [] diff --git a/app/FeedSourcePlugin.py b/app/FeedSourcePlugin.py index af686b9..5453f80 100644 --- a/app/FeedSourcePlugin.py +++ b/app/FeedSourcePlugin.py @@ -12,17 +12,19 @@ class Plugin(PluginInterface): print(f"[FeedSourcePlugin#{self.id}] initialized") - def validate_input_n(self, n: int) -> bool: - return n == 0 - - def process(self, inputs: list[list[Item]]) -> list[Item]: + def process(self, items: list[Item]) -> list[Item]: print(f"[FeedSourcePlugin#{self.id}] process called") feed: Any = feedparser.parse(self.feed_url) # type: ignore - items: list[Item] = [] + result_items: list[Item] = [] + if "bozo" in feed and feed["bozo"] == 1: + raise Exception( + f"[FeedSourcePlugin#{self.id}] malformed XML in feed {self.feed_url}" + ) + for d in feed.entries: - items.append( + result_items.append( Item(title=d["title"], link=d["link"], description=d["description"]) ) print(f"[FeedSourcePlugin#{self.id}] process returns items, n={len(items)}") - return items + return result_items diff --git a/app/FilterPlugin.py b/app/FilterPlugin.py index fd3984a..735540c 100644 --- a/app/FilterPlugin.py +++ b/app/FilterPlugin.py @@ -10,13 +10,9 @@ class Plugin(PluginInterface): self.filter_expr: str = get_param("filter_expr", params) print(f"[FilterPlugin#{self.id}] initialized") - def validate_input_n(self, n: int) -> bool: - return n == 1 - - def process(self, inputs: list[list[Item]]) -> list[Item]: - input_feed: list[Item] = inputs[0] - print(f"[FilterPlugin#{self.id}] process called, n={len(input_feed)}") + def process(self, items: list[Item]) -> list[Item]: + print(f"[FilterPlugin#{self.id}] process called, n={len(items)}") expr = f"filter(lambda item: {self.filter_expr}, input_feed)" - ret = list(eval(expr, {"input_feed": input_feed})) + ret = list(eval(expr, {"input_feed": items})) print(f"[FilterPlugin#{self.id}] process returning items, n={len(ret)}") return ret diff --git a/app/PluginInterface.py b/app/PluginInterface.py index 6194436..f334034 100644 --- a/app/PluginInterface.py +++ b/app/PluginInterface.py @@ -10,9 +10,5 @@ class PluginInterface(ABC): self.params = params @abstractmethod - def validate_input_n(self, n: int) -> bool: - raise NotImplemented("abstract method not implemented") - - @abstractmethod - def process(self, inputs: list[list[Item]]) -> list[Item]: + def process(self, items: list[Item]) -> list[Item]: pass diff --git a/app/PluginManager.py b/app/PluginManager.py index 8d60bc1..2b616ca 100644 --- a/app/PluginManager.py +++ b/app/PluginManager.py @@ -36,7 +36,7 @@ class PluginManager: return plugin: PluginInterface = self.plugin_instances[id] - ret_items: list[Item] = plugin.process([items]) + ret_items: list[Item] = plugin.process(items) self.propagate(id, ret_items) def build_plugin_instances(self): |
