2 # Someone's Mattermost scripts.
3 # Copyright (c) 2016-2020 by Someone <someone@somenet.org> (aka. Jan Vales <jan@jvales.net>)
4 # published under MIT-License
13 from html.parser import HTMLParser
16 feedparser.PREFERRED_XML_PARSERS.remove("drv_libxml2")
23 class MLStripper(HTMLParser):
27 self.convert_charrefs = True
29 def handle_data(self, d):
32 return "".join(self.fed)
40 def processFeed(mm, feed):
43 with open("data/rss."+feed+".json") as data_file:
44 rssdata = json.load(data_file)
49 for id in list(rssdata):
52 f = feedparser.parse(config.mm_rss_config[feed][0])
53 for entry in f.entries:
54 id = entry.link+entry.title
60 split = urllib.parse.urlsplit(entry.link)
63 qry = split.query+"&mattermost"
64 url = urllib.parse.urlunsplit((split.scheme, split.netloc, split.path, qry, split.fragment))
66 date = "("+entry["published"]+") "
70 msg = "### "+config.mm_rss_config[feed][2]+" ``"+entry.title+"``\n"+date+url+"\n\n----\n\n"+entry["summary"]
71 if config.mm_rss_config[feed][3]:
72 msg = "### "+config.mm_rss_config[feed][2]+" ``"+entry.title+"``\n"+date+url+"\n\n----\n\n"+strip_tags(entry["summary"])
73 for chanid in config.mm_rss_config[feed][1]:
74 print("Posting to channel:" +str(mm.create_post(chanid, msg)))
76 for id in list(rssdata):
77 if rssdata[id] == 0 and int(random.random() * 20000) == 0:
80 with open("data/rss."+feed+".json", "w") as f:
81 f.write(json.dumps(rssdata))
86 for feed in config.mm_rss_config:
94 if __name__ == "__main__":
95 mm = mattermost.MMApi(config.mm_api_url)
96 mm.login(config.mm_user, config.mm_user_pw)