aboutsummaryrefslogtreecommitdiffstats
path: root/app/FeedSinkPlugin.py
blob: c0fc4c95730952d1a346b5e9a593ea9c2917950e (plain)
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
79
80
81
82
83
84
85
86
import time
from typing import Any
import xml.etree.ElementTree as ET

from tinydb import Query

from app.Item import Item
from app.PluginInterface import PluginInterface
from app.utils import get_param
from app.DatabaseManager import database_manager


class Plugin(PluginInterface):
    def __init__(self, id: str, params: dict[str, Any]) -> None:
        super().__init__(id, 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 = 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 = self.feed_description

        for item in items:
            item_elem = ET.SubElement(channel, "item")
            if item.title is not None:
                item_title = ET.SubElement(item_elem, "title")
                item_title.text = item.title
            if item.link is not None:
                item_link = ET.SubElement(item_elem, "link")
                item_link.text = item.link
            if item.description is not None:
                item_description = ET.SubElement(item_elem, "description")
                item_description.text = item.description
            if item.category is not None:
                item_category = ET.SubElement(item_elem, "category")
                item_category.text = item.category
            if item.comments is not None:
                item_comments = ET.SubElement(item_elem, "comments")
                item_comments.text = item.comments
            if item.pub_date is not None:
                item_pub_date = ET.SubElement(item_elem, "pubDate")
                item_pub_date.text = time.strftime(
                    "%a, %d %b %Y %H:%M:%S +0000", item.pub_date
                )
            if item.author is not None:
                item_author = ET.SubElement(item_elem, "author")
                item_author.text = item.author

            for enc in item.enclosures:
                ET.SubElement(
                    item_elem,
                    "enclosure",
                    {
                        "length": enc.length,
                        "type": enc.type,
                        "url": enc.url,
                    },
                )

        return ET.tostring(rss, "unicode")

    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)

        Q = Query()
        database_manager.feeds.upsert({"feed_id": self.feed_id, "feed_xml": ret}, Q.feed_id == self.feed_id)  # type: ignore
        print(f"[FeedSinkPlugin#{self.id}] processed")
        return []