Add cif data as attachment to log email
This commit is contained in:
parent
30faef09c8
commit
feb9775fe2
@ -2310,7 +2310,7 @@ pis:
|
||||
- code: 6253
|
||||
stops: [wsb,dmh,wmn,sal]
|
||||
- code: 6257
|
||||
stops: [gcr,yae,bpw,fit,bri,kyn,olf,bth,boa,tro,web,fro]
|
||||
stops: [gcr,yae,bpw,fit,bri,kyn,olf,bth,boa,tro,wsb,fro]
|
||||
|
||||
## Non Passenger Codes
|
||||
- code: 0015
|
||||
|
@ -14,96 +14,62 @@
|
||||
# program. If not, see
|
||||
# https://git.fjla.uk/OwlBoard/db-manager/src/branch/main/LICENSE
|
||||
|
||||
import smtplib, ssl, os
|
||||
import email, smtplib, ssl, os
|
||||
from email import encoders
|
||||
from email.mime.base import MIMEBase
|
||||
from email.mime.multipart import MIMEMultipart
|
||||
from email.mime.text import MIMEText
|
||||
from email.utils import formatdate
|
||||
|
||||
import logger as log
|
||||
|
||||
cif_file_path = "cif_data"
|
||||
smtpHost = os.getenv("OWL_EML_HOST")
|
||||
smtpPort = os.getenv("OWL_EML_PORT")
|
||||
smtpUser = os.getenv("OWL_EML_USER")
|
||||
smtpPass = os.getenv("OWL_EML_PASS")
|
||||
smtpFrom = os.getenv("OWL_EML_FROM")
|
||||
|
||||
log.out("mailer.py: Mailer module loaded", "DBUG")
|
||||
|
||||
def submitLogs():
|
||||
text :str = fetchLogs()
|
||||
cif_data = fetchCifData()
|
||||
sendMail(text, cifData)
|
||||
sendMail(text)
|
||||
|
||||
def fetchLogs():
|
||||
with open("dbman-log", "r") as tmpfile:
|
||||
return tmpfile.read()
|
||||
|
||||
def fetchCifData():
|
||||
try:
|
||||
with open(cif_file_path, "r") as f:
|
||||
return f.read()
|
||||
except Exception as e:
|
||||
log.out("mailer.fetchCifData: CIF Data is not readable")
|
||||
print(e)
|
||||
|
||||
def deleteLogs():
|
||||
if os.path.exists("dbman-log"):
|
||||
os.remove("dbman-log")
|
||||
print("Tidied log file")
|
||||
else:
|
||||
print("No logfile to tidy")
|
||||
if os.path.exists(cif_file_path):
|
||||
os.remove(cif_file_path)
|
||||
else:
|
||||
print("No cifData file to tidy")
|
||||
|
||||
#def sendMail(messageBody :str):
|
||||
# smtpHost = os.getenv("OWL_EML_HOST")
|
||||
# smtpPort = os.getenv("OWL_EML_PORT")
|
||||
# smtpUser = os.getenv("OWL_EML_USER")
|
||||
# smtpPass = os.getenv("OWL_EML_PASS")
|
||||
# smtpFrom = os.getenv("OWL_EML_FROM")
|
||||
# context = ssl.create_default_context()
|
||||
# message = f"""Subject: OwlBoard-dbman-logs
|
||||
|
||||
|
||||
#{messageBody}"""
|
||||
# try:
|
||||
# server = smtplib.SMTP(smtpHost,smtpPort)
|
||||
# server.ehlo()
|
||||
# server.starttls(context=context) # Secure the connection
|
||||
# server.ehlo()
|
||||
# server.login(smtpUser, smtpPass)
|
||||
# server.sendmail(smtpFrom, "server-notification-receipt@fjla.uk", message)
|
||||
# except Exception as e:
|
||||
# # Print any error messages to stdout
|
||||
# print(e)
|
||||
# finally:
|
||||
# server.quit()
|
||||
# deleteLogs()
|
||||
|
||||
def sendMail(messageBody, cif_data):
|
||||
smtpHost = os.getenv("OWL_EML_HOST")
|
||||
smtpPort = os.getenv("OWL_EML_PORT")
|
||||
smtpUser = os.getenv("OWL_EML_USER")
|
||||
smtpPass = os.getenv("OWL_EML_PASS")
|
||||
smtpFrom = os.getenv("OWL_EML_FROM")
|
||||
def sendMail(msg_body :str):
|
||||
message = MIMEMultipart()
|
||||
message['From'] = smtpFrom
|
||||
message['To'] = "server-notification-receipt@fjla.uk"
|
||||
message['Subject'] = "OwlBoard - dbmanager Logs"
|
||||
filename = "cif_data"
|
||||
message.attach(MIMEText(msg_body, "plain"))
|
||||
with open(filename, "rb") as attachment:
|
||||
part = MIMEBase("application", "octet-stream")
|
||||
part.set_payload(attachment.read())
|
||||
encoders.encode_base64(part)
|
||||
part.add_header(
|
||||
"Content-Disposition",
|
||||
f"attachment; filename= {filename}",
|
||||
)
|
||||
message.attach(part)
|
||||
text = message.as_string()
|
||||
context = ssl.create_default_context()
|
||||
msg = MIMEMultipart()
|
||||
msg['From'] = smtpFrom
|
||||
msg['To'] = "server-notification-receipt@fjla.uk"
|
||||
msg['Date'] = formatdate(localtime=True)
|
||||
msg['Subject'] = "OwlBoard - dbmanager Logs"
|
||||
msg.attach(MIMEText(messageBody))
|
||||
if cif_data:
|
||||
part = MIMEText(cif_data, 'plain')
|
||||
part.add_header('Content-Disposition', 'attachment', filename="CIF-DATA")
|
||||
msg.attach(part)
|
||||
try:
|
||||
with smtplib.SMTP(smtpHost, smtpPort) as server:
|
||||
server.ehlo()
|
||||
server.starttls(context=context) # Secure the connection
|
||||
server.ehlo()
|
||||
server.starttls(context=context)
|
||||
server.login(smtpUser, smtpPass)
|
||||
server.sendmail(smtpFrom, "server-notification-receipt@fjla.uk", msg.as_string())
|
||||
server.sendmail(smtpFrom, "server-notification-receipt@fjla.uk", text)
|
||||
except Exception as e:
|
||||
# Print any error messages to stdout
|
||||
print(e)
|
||||
print("mailer.sendMail: Error sending message", e)
|
||||
finally:
|
||||
deleteLogs()
|
||||
|
||||
deleteLogs()
|
@ -14,7 +14,7 @@
|
||||
# program. If not, see
|
||||
# https://git.fjla.uk/OwlBoard/db-manager/src/branch/main/LICENSE
|
||||
|
||||
version = "2023.6.3"
|
||||
version = "2023.6.4"
|
||||
print(f"main.py: Initialising db-manager v{version}")
|
||||
|
||||
#Third Party Imports
|
||||
|
13
src/pis.py
13
src/pis.py
@ -69,8 +69,11 @@ def getTiploc(crs :str):
|
||||
query = {
|
||||
'3ALPHA': CRS
|
||||
}
|
||||
res = mongo.query("stations", query)
|
||||
print(res)
|
||||
if 'TIPLOC' in res:
|
||||
return res['TIPLOC']
|
||||
return "UNKNOWN"
|
||||
try:
|
||||
res = mongo.query("stations", query)
|
||||
#print(res)
|
||||
if 'TIPLOC' in res:
|
||||
return res['TIPLOC']
|
||||
except Exception as e:
|
||||
log.out("pis.getTiploc: Error finding tiploc:", query)
|
||||
print("ERROR", e)
|
@ -49,7 +49,7 @@ def isUpdateRequired():
|
||||
timetableLength = mongo.getLength("timetable")
|
||||
log.out(f"timetable.isUpdateRequired: timetable collection contains {timetableLength} documents", "DBUG")
|
||||
timetableUpdateTime = mongo.metaCheckTime("timetable")
|
||||
log.out(f"timetable.isUpdateRequired: Timetable last updated at {timetableUpdateDate}", "INFO")
|
||||
log.out(f"timetable.isUpdateRequired: Timetable last updated at {timetableUpdateTime}", "INFO")
|
||||
timetableDataAge = helpers.getAgeInSeconds(timetableUpdateTime)
|
||||
if (timetableDataAge >= twoDayinSecs and isAfter0800) or REBUILD:
|
||||
log.out(f"timetable.isUpdateRequired: timetable collection requires rebuild", "INFO")
|
||||
@ -64,9 +64,10 @@ def getTimetable(full :bool = False):
|
||||
response = requests.get(downloadUrl, auth=(CORPUS_USER, CORPUS_PASS))
|
||||
mongo.incrementCounter("schedule_api")
|
||||
log.out(f"timetable.getTimetable: Fetch (Full:{full}) response: {response.status_code}", "DBUG")
|
||||
decompressed = zlib.decompress(response.content, 16+zlib.MAX_WBITS)
|
||||
with open(filePath, "wb") as f:
|
||||
f.write(response.content)
|
||||
return zlib.decompress(response.content, 16+zlib.MAX_WBITS)
|
||||
f.write(decompressed)
|
||||
return decompressed
|
||||
|
||||
def loopTimetable(data):
|
||||
listify = data.splitlines()
|
||||
|
Reference in New Issue
Block a user