From 975a9fb434d45f1440d50ce0c04f9c9635922dfb Mon Sep 17 00:00:00 2001 From: Someone Date: Fri, 19 Jun 2020 01:32:13 +0200 Subject: [PATCH] [cron] RSS-Feed(s) to Mattermost channel(s). --- rss_to_channel/.gitignore | 2 + rss_to_channel/config.py.example | 16 +++++ rss_to_channel/data/.gitkeep | 0 rss_to_channel/main.py | 100 +++++++++++++++++++++++++++++++ 4 files changed, 118 insertions(+) create mode 100644 rss_to_channel/.gitignore create mode 100644 rss_to_channel/config.py.example create mode 100644 rss_to_channel/data/.gitkeep create mode 100755 rss_to_channel/main.py diff --git a/rss_to_channel/.gitignore b/rss_to_channel/.gitignore new file mode 100644 index 0000000..bbc97f1 --- /dev/null +++ b/rss_to_channel/.gitignore @@ -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 index 0000000..98c9196 --- /dev/null +++ b/rss_to_channel/config.py.example @@ -0,0 +1,16 @@ +# +# Someone's Mattermost scripts. +# Copyright (c) 2016-2020 by Someone (aka. Jan Vales ) +# 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 index 0000000..e69de29 diff --git a/rss_to_channel/main.py b/rss_to_channel/main.py new file mode 100755 index 0000000..b4f646f --- /dev/null +++ b/rss_to_channel/main.py @@ -0,0 +1,100 @@ +#!/usr/bin/env python3 +# Someone's Mattermost scripts. +# Copyright (c) 2016-2020 by Someone (aka. Jan Vales ) +# 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() -- 2.43.0