Esempio n. 1
0
    def notify(config, result, result_k, coherence, extension):
        now = datetime.datetime.now()
        # data format: YYYYMMDD
        now_month = now.month
        if int(now_month) < 10:
            now_month = "0" + str(now.month)
        today = str(now.year) + str(now_month) + str(now.day)

        logging.info("- EmailNotifier is sending...")
        smtp_server = config["SERVER"]
        smtp_port = config["PORT"]
        smtp_username = config["USERNAME"]
        smtp_password = config["PASSWORD"]
        smtp_from = config["FROM"]
        smtp_to = config["TO"]
        smtp_cc = config["CC"]
        smtp_bcc = config["BCC"]
        smtp_subject = config["SUBJECT"]
        smtp_body = config["BODY"]

        # Attachments
        smtp_new = config["NEW"] + "_" + today
        smtp_mod = config["MOD"] + "_" + today
        smtp_del = config["DEL"] + "_" + today
        smtp_new_k = config["NEW_K"] + "_" + today
        smtp_mod_k = config["MOD_K"] + "_" + today

        # Attachment format: <dir><file_name>_<date>
        smtp_file_new = config["PATH_TEMP"] + smtp_new
        smtp_file_mod = config["PATH_TEMP"] + smtp_mod
        smtp_file_del = config["PATH_TEMP"] + smtp_del
        smtp_file_new_k = config["PATH_TEMP"] + smtp_new_k
        smtp_file_mod_k = config["PATH_TEMP"] + smtp_mod_k

        try:
            logging.info("- EmailNotifier is sending...")
            server = SMTP(smtp_server, smtp_port)
            server.set_debuglevel(True)
            msg = MIMEMultipart()
            msg["From"] = smtp_from
            msg["To"] = "; ".join(smtp_to)
            emails = [smtp_to]
            if smtp_cc is not "":
                msg["CC"] = "; ".join(smtp_cc)
                emails += smtp_cc
            if smtp_bcc is not "":
                msg["BCC"] = "; ".join(smtp_bcc)
                emails += smtp_bcc

            # Added priority to subject
            priority = ""
            if coherence:
                if coherence == 1:
                    priority = "[Info] "
                elif 1 < coherence < 4:
                    priority = "[Warning] "
                else:
                    priority = "[Danger] "
            msg["Subject"] = priority + smtp_subject

            body = MIMEText(smtp_body, "html")
            msg.attach(body)
            # logging.debug(msg.as_string())
            # If dictionary of new posts exists, it creates attachment
            if "new" in result:
                try:
                    if extension == "c":
                        smtp_new += ".csv"
                        smtp_file_new += ".csv"
                        CsvConverter.save_dictionary_to_file(smtp_file_new, result["new"], "new")
                    elif extension == "x":
                        smtp_new += ".xml"
                        smtp_file_new += ".xml"
                        XmlConverter.save_dictionary_to_file(smtp_file_new, result["new"])
                    elif extension == "j":
                        smtp_new += ".json"
                        smtp_file_new += ".json"
                        JsonConverter.save_dictionary_to_file(smtp_file_new, result["new"])
                    logging.info("- Attaching file: {0}".format(str(smtp_file_new)))
                    with open(smtp_file_new, "rb") as fil:
                        part = MIMEApplication(fil.read(), "text/plain", filename=smtp_new)
                    part.add_header("Content-Disposition", "attachment", filename=smtp_new)
                    msg.attach(part)
                except OSError, e:
                    logging.error("- Error walking dir '%s': %s" % (dir, e))
                    raise OSError
            # If dictionary of post changes exists, it creates attachment
            if "mod" in result:
                try:
                    if extension == "c":
                        smtp_mod += ".csv"
                        smtp_file_mod += ".csv"
                        CsvConverter.save_dictionary_to_file(smtp_file_mod, result["mod"], "mod")
                    elif extension == "x":
                        smtp_mod += ".xml"
                        smtp_file_mod += ".xml"
                        XmlConverter.save_dictionary_to_file(smtp_file_mod, result["mod"])
                    elif extension == "j":
                        smtp_mod += ".json"
                        smtp_file_mod += ".json"
                        JsonConverter.save_dictionary_to_file(smtp_file_mod, result["mod"])
                    logging.info("- Attaching file: {0}".format(str(smtp_file_mod)))
                    with open(smtp_file_mod, "rb") as fil:
                        part = MIMEApplication(fil.read(), "text/plain", filename=smtp_mod)
                    part.add_header("Content-Disposition", "attachment", filename=smtp_mod)
                    msg.attach(part)
                except OSError, e:
                    logging.error("- Error walking dir '%s': %s" % (dir, e))
                    raise OSError
Esempio n. 2
0
                    part.add_header("Content-Disposition", "attachment", filename=smtp_mod)
                    msg.attach(part)
                except OSError, e:
                    logging.error("- Error walking dir '%s': %s" % (dir, e))
                    raise OSError
            # If dictionary of deletes exists, it creates attachment
            if "del" in result:
                try:
                    if extension == "c":
                        smtp_del += ".csv"
                        smtp_file_del += ".csv"
                        CsvConverter.save_dictionary_to_file(smtp_file_del, result["del"], "del")
                    elif extension == "x":
                        smtp_del += ".xml"
                        smtp_file_del += ".xml"
                        XmlConverter.save_dictionary_to_file(smtp_file_del, result["del"])
                    elif extension == "j":
                        smtp_del += ".json"
                        smtp_file_del += ".json"
                        JsonConverter.save_dictionary_to_file(smtp_file_del, result["del"])
                    logging.info("- Attaching file: {0}".format(str(smtp_file_del)))
                    with open(smtp_file_del, "rb") as fil:
                        part = MIMEApplication(fil.read(), "text/plain", filename=smtp_del)
                    part.add_header("Content-Disposition", "attachment", filename=smtp_del)
                    msg.attach(part)
                except OSError, e:
                    logging.error("- Error walking dir '%s': %s" % (dir, e))
                    raise OSError

            # ################### #
            # KEYWORDS DICTIONARY #