-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnew_sendAlertMail.py
More file actions
150 lines (138 loc) · 5.54 KB
/
new_sendAlertMail.py
File metadata and controls
150 lines (138 loc) · 5.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/usr/bin/env python
import requests
import asyncio
import pathlib
import ssl
import websockets
import sys
import smtplib
import json
import yaml
from email.message import EmailMessage
import MySQLdb
import datetime as dt
import traceback
serverstats = {}
eventTypes={}
eventFile = open('metagame.yaml','r')
eventTypes=yaml.load(eventFile.read())
worldIDToName={'13':'Cobalt'}
rawHtmlFile = ""
config = yaml.load(open('config.yaml','r').read())['Config']
async def hello():
global serverstats,rawHtmlFile, config
async with websockets.connect('wss://push.planetside2.com/streaming?environment=ps2&service-id=s:'+config['ServiceId']+'',ssl=True) as ws:
name='{ "service":"event", "action":"subscribe", "worlds":["'+config['WorldId']+'"], "eventNames":["MetagameEvent"] }'
await ws.send(name)
print (f"> {name}")
while True :
event = await ws.recv()
f = open("log.txt",'a')
eventJ = json.loads(event)
if 'payload' in eventJ:
alert = False
if eventJ['payload']['metagame_event_id'] in eventTypes['Events']:
if "lock" in str(eventTypes['Events'][eventJ['payload']['metagame_event_id']]):
alert = True
event = eventTypes['Events'][eventJ['payload']['metagame_event_id']]
event['state'] = eventJ['payload']['metagame_event_state_name']
print(event)
print(eventTypes['Events'][eventJ['payload']['metagame_event_id']])
event = "<table border='1'><tr><td>"+event['description']+"</td><td>"+event['name']+"</td><td>"+event['state']+"</td></tr></table>"
event = rawHtmlFile.replace('content',event)
continent = "unkown"
if alert:
continent = str(eventTypes['Events'][eventJ['payload']['metagame_event_id']]).split('lock ')[1].split('"')[0]
print(continent)
sendEventMail(event,alert,continent)
else:
event = eventTypes['Events'][eventJ['payload']['metagame_event_id']]
event['state'] = eventJ['payload']['metagame_event_state_name']
print(event)
event = "<table border='1'><tr><td>"+event['description']+"</td><td>"+event['name']+"</td><td>"+event['state']+"</td></tr></table>"
event = rawHtmlFile.replace('content',event)
sendEventMail(event,alert,"unkown")
#print (event['meta_event_state_name'])
#if "detail" not in event and "online" in event:
# if serverstats != event['online']:
# serverstats=event['online']
# sendServerStateChangeMail()
if "heartbeat" not in json.dumps(eventJ):
f.write(json.dumps(eventJ))
print(event)
f.close()
def connect():
global rawHtmlFile
f = open("alert_mail.html",'r')
rawHtmlFile = f.read()
f.close()
try:
asyncio.get_event_loop().run_until_complete(hello())
except:
print(str(traceback.format_exc()))
sendErrorMail(str(traceback.format_exc()))
connect()
def sendEventMail(event,alert,continent):
msg = EmailMessage()
msg['From'] = config['MailSenderAddress']
mail_domain = config['MailDomain']
server = smtplib.SMTP(config['MailServerIP'],25,"mail_domain",10)
server.set_debuglevel(1)
msg.add_alternative(event,subtype='html')
#msg.set_content(json.dumps(event))
db = MySQLdb.connect(host=config['DatabaseIP'],db=config['DatabaseUser'].password=config['DatabasePassword'])
cnx = db.cursor()
time = dt.datetime.now().hour
endNotification=""
if 'ended' in event:
endNotification=' and planetside2Settings.endNotification=1'
if alert:
if config['UseDatabase']:
continent = "planetside2Settings."+continent.lower()+"Notification=1 and"
print(continent)
cnx.execute('Select email from user Left Join planetside2Settings on user.id=planetside2Settings.userId where planetside2Settings.notificationEnabled=1 and user.id = planetside2Settings.userId and planetside2Settings.notificationStartHour <= '+str(time)+' and planetside2Settings.notificationEndHour>='+str(time) + endNotification + ' and ' + continent +" planetside2Settings.notificationEnabled=1")
recipients=[]
print(cnx)
for row in cnx.fetchall():
if row[0] != None:
recipients.append(row[0])
else:
recipients = config['Recipients']
if 'ended' in event and !config['EndAlertNotification']:
recipients = []
print(recipients)
if recipients != [] :
msg['Subject']="WARNING Alert has started in Planetside 2 on "
msg['To']="Alert Subscribers"
server.sendmail(msg['From'],recipients,msg.as_string())
else:
if config['UseDatabase']:
cnx.execute('Select email from user Left Join planetside2Settings on user.id=planetside2Settings.userId where planetside2Settings.notificationEnabled=1 and user.id = planetside2Settings.userId and planetside2Settings.alertNotificationOnly=0 and planetside2Settings.notificationStartHour<='+str(time)+' and planetside2Settings.notificationEndHour>='+str(time)+endNotification)
print(cnx)
recipients=[]
for row in cnx.fetchall():
if row[0] != None:
recipients.append(row[0])
elif !config['Alert']:
recipients = config['Recipients']
if 'ended' in event and !config['EndAlertNotification']:
recipients = []
print(recipients)
if recipients != [] :
msg['Subject']="New Event has started"
msg['To']="EventSubscribers"
server.sendmail(msg['From'],recipients,msg.as_string())
server.quit()
def sendErrorMail(error):
msg = EmailMessage()
msg['From'] = config['ErrorSender']
maildomain = config['MailDomain']
server = smtplib.SMTP(config['MailServerIP'],25,maildomain,10)
server.set_debuglevel(1)
msg['Subject'] = "An error occured"
msg['To'] = "Admin"
msg.set_content(error)
server.sendmail(msg['From'],config['ErrorRecipient'],msg.as_string())
#server = sys.argv[1]
connect()
#main(server)