[cron] RSS-Feed(s) to Mattermost channel(s).
authorSomeone <someone@somenet.org>
Thu, 18 Jun 2020 23:32:13 +0000 (01:32 +0200)
committerSomeone <someone@somenet.org>
Thu, 18 Jun 2020 23:32:13 +0000 (01:32 +0200)
rss_to_channel/.gitignore [new file with mode: 0644]
rss_to_channel/config.py.example [new file with mode: 0644]
rss_to_channel/data/.gitkeep [new file with mode: 0644]
rss_to_channel/main.py [new file with mode: 0755]

diff --git a/rss_to_channel/.gitignore b/rss_to_channel/.gitignore
new file mode 100644 (file)
index 0000000..bbc97f1
--- /dev/null
@@ -0,0 +1,2 @@
+config.py
+data/**
diff --git a/rss_to_channel/config.py.example b/rss_to_channel/config.py.example
new file mode 100644 (file)
index 0000000..98c9196
--- /dev/null
@@ -0,0 +1,16 @@
+#
+# Someone's Mattermost scripts.
+#  Copyright (c) 2016-2020 by Someone <someone@somenet.org> (aka. Jan Vales <jan@jvales.net>)
+#  published under MIT-License
+#
+# Example config file.
+#  mv to config.py.examle config.py + edit.
+#
+
+mm_api_url = "..."
+mm_user = "..."
+mm_user_pw = "..."
+
+mm_rss_config = {
+    "feed-name": ["feed-url",["channel1-id", "channel2-id", ], "title-prefix", True],
+    }
diff --git a/rss_to_channel/data/.gitkeep b/rss_to_channel/data/.gitkeep
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/rss_to_channel/main.py b/rss_to_channel/main.py
new file mode 100755 (executable)
index 0000000..b4f646f
--- /dev/null
@@ -0,0 +1,100 @@
+#!/usr/bin/env python3
+# Someone's Mattermost scripts.
+#  Copyright (c) 2016-2020 by Someone <someone@somenet.org> (aka. Jan Vales <jan@jvales.net>)
+#  published under MIT-License
+#
+# spam channel
+#
+
+import traceback
+import urllib.parse
+import json
+import random
+from html.parser import HTMLParser
+
+import feedparser
+feedparser.PREFERRED_XML_PARSERS.remove("drv_libxml2")
+
+import mattermost
+
+import config
+
+
+class MLStripper(HTMLParser):
+    def __init__(self):
+        self.reset()
+        self.strict = False
+        self.convert_charrefs = True
+        self.fed = []
+    def handle_data(self, d):
+        self.fed.append(d)
+    def get_data(self):
+        return "".join(self.fed)
+
+def strip_tags(html):
+    s = MLStripper()
+    s.feed(html)
+    return s.get_data()
+
+
+def processFeed(mm, feed):
+    rssdata = dict()
+    try:
+        with open("data/rss."+feed+".json") as data_file:
+            rssdata = json.load(data_file)
+    except:
+        pass
+
+    # Is still present?
+    for id in list(rssdata):
+        rssdata[id] = 0
+
+    f = feedparser.parse(config.mm_rss_config[feed][0])
+    for entry in f.entries:
+        id = entry.link+entry.title
+        if id in rssdata:
+            rssdata[id] = 1
+        else:
+            rssdata[id] = 2
+
+            split = urllib.parse.urlsplit(entry.link)
+            qry = "mattermost"
+            if split.query != "":
+                qry = split.query+"&mattermost"
+            url = urllib.parse.urlunsplit((split.scheme, split.netloc, split.path, qry, split.fragment))
+            try:
+                date = "("+entry["published"]+") "
+            except:
+                date = ""
+
+            msg = "### "+config.mm_rss_config[feed][2]+" ``"+entry.title+"``\n"+date+url+"\n\n----\n\n"+entry["summary"]
+            if config.mm_rss_config[feed][3]:
+                msg = "### "+config.mm_rss_config[feed][2]+" ``"+entry.title+"``\n"+date+url+"\n\n----\n\n"+strip_tags(entry["summary"])
+            for chanid in config.mm_rss_config[feed][1]:
+                print("Posting to channel:" +str(mm.create_post(chanid, msg)))
+
+    for id in list(rssdata):
+        if rssdata[id] == 0 and int(random.random() * 20000) == 0:
+            del rssdata[id]
+
+    with open("data/rss."+feed+".json", "w") as f:
+        f.write(json.dumps(rssdata))
+
+
+
+def main(mm):
+    for feed in config.mm_rss_config:
+        try:
+            processFeed(mm, feed)
+        except:
+            traceback.print_exc()
+
+
+
+if __name__ == "__main__":
+    mm = mattermost.MMApi(config.mm_api_url)
+    mm.login(config.mm_user, config.mm_user_pw)
+
+    main(mm)
+
+    mm.logout()