New script to notify owners of bad files.
This commit is contained in:
parent
9d48014cc8
commit
a4c64699da
84
mail_notify.py
Executable file
84
mail_notify.py
Executable file
@ -0,0 +1,84 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf8 -*-
|
||||
|
||||
import MosDb
|
||||
import MosReport
|
||||
import datetime
|
||||
import os.path
|
||||
import sys
|
||||
from email.message import EmailMessage
|
||||
from email.mime.text import MIMEText
|
||||
from email.headerregistry import Address
|
||||
from email.utils import formatdate
|
||||
from subprocess import Popen, PIPE
|
||||
|
||||
# Default: last hour
|
||||
LAST_NOTIFICATION_SENT = datetime.datetime.now().timestamp() - 7200
|
||||
if os.path.isfile(".last_notification"):
|
||||
with open(".last_notification", "r") as f:
|
||||
LAST_NOTIFICATION_SENT = f.readline().strip()
|
||||
f.close()
|
||||
|
||||
db = MosDb.MosDb()
|
||||
|
||||
items = db.get_daily_oa("-1 day", 2)
|
||||
#items = db.get_daily_oa("-1 day")
|
||||
db.finish()
|
||||
|
||||
notifications = {}
|
||||
for i in items:
|
||||
if i["createdStamp"] < LAST_NOTIFICATION_SENT:
|
||||
# Old, ignore
|
||||
continue
|
||||
if not i["lu"]:
|
||||
# No LU data, ignore
|
||||
continue
|
||||
print("Got: " + i["name"])
|
||||
if abs(float(i["ldiff"])) < 2:
|
||||
print("LU OK. ({} / {})".format(i["lu"], i["ldiff"]))
|
||||
continue
|
||||
|
||||
print("Differs more than 2!!!! ({} / {})".format(i["lu"], i["ldiff"]))
|
||||
email = i["createdByEmail"]
|
||||
emails_list = ["markus.birth@weltn24.de", "sebastian.erb@weltn24.de"]
|
||||
if email:
|
||||
emails_list += [email]
|
||||
|
||||
for e in emails_list:
|
||||
if not e in notifications:
|
||||
notifications[e] = []
|
||||
notifications[e].append(i)
|
||||
|
||||
print(repr(notifications))
|
||||
|
||||
for e in notifications:
|
||||
print("### Mail to: " + e)
|
||||
i = notifications[e]
|
||||
(output_txt, output_csv) = MosReport.get_report(i)
|
||||
print(output_txt)
|
||||
|
||||
sys.exit(0)
|
||||
|
||||
msg = EmailMessage()
|
||||
msg["Date"] = formatdate(localtime=True)
|
||||
msg["Subject"] = "Lautheitsbericht {:%Y-%m-%d}".format(datetime.datetime.now())
|
||||
msg["From"] = Address("Lautheitsüberwachung", "broadcast-support", "weltn24.de")
|
||||
msg["To"] = (
|
||||
Address("serb", "sebastian.erb", "weltn24.de"),
|
||||
Address("mbirth", "markus.birth", "weltn24.de")
|
||||
)
|
||||
|
||||
msg.set_content(output_text)
|
||||
msg.make_mixed()
|
||||
|
||||
att = MIMEText(output_csv, _subtype="csv", _charset="utf-8")
|
||||
att.add_header("Content-Disposition", "attachment", filename="lautheit.csv")
|
||||
|
||||
msg.attach(att)
|
||||
|
||||
#print(repr(clips))
|
||||
print(msg.as_string())
|
||||
|
||||
p = Popen(["/usr/sbin/sendmail", "-oi", "markus.birth@weltn24.de", "sebastian.erb@weltn24.de"], stdin=PIPE)
|
||||
p.communicate(msg.as_bytes())
|
||||
|
Loading…
x
Reference in New Issue
Block a user