#!/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()
