forked from Galts-Gulch/avarice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotifier.py
188 lines (154 loc) · 5.78 KB
/
notifier.py
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import http.client
import logging
import os
import urllib
from logging import handlers
import genconfig as gc
class Wrapper:
def Run():
TextFile.Simulator()
TextFile.Trader()
if gc.Simulator.Verbose:
Printer.Simulator()
if gc.Trader.Verbose:
Printer.Trader()
if gc.Notifier.Pushover.Simulator:
Pushover.Simulator()
if gc.Notifier.Pushover.Trader:
Pushover.Trader()
if gc.Notifier.TlsSMTP.Simulator:
TlsSMTP.Simulator()
if gc.Notifier.TlsSMTP.Trader:
TlsSMTP.Trader()
if gc.Notifier.SMTP.Simulator:
SMTP.Simulator()
if gc.Notifier.SMTP.Trader:
SMTP.Trader()
class PrintHandler(logging.Handler):
def __init__(self):
logging.Handler.__init__(self)
def emit(self, record):
print(record.name.upper() + ':', record.message)
class PushoverHandler(logging.Handler):
def __init__(self):
logging.Handler.__init__(self)
def emit(self, record):
conn = http.client.HTTPSConnection("api.pushover.net:443")
conn.request("POST", "/1/messages.json",
urllib.parse.urlencode({
"token": gc.Notifier.Pushover.AppToken,
"user": gc.Notifier.Pushover.UserKey,
"message": record.name.upper() + ': ' + record.message,
}), {"Content-type": "application/x-www-form-urlencoded"})
conn.getresponse()
class TlsSMTPHandler(logging.handlers.SMTPHandler):
def emit(self, record):
import smtplib
try:
from email.utils import formatdate
except ImportError:
formatdate = self.date_time
port = self.mailport
if not port:
port = smtplib.SMTP_PORT
smtp = smtplib.SMTP(self.mailhost, port)
msg = self.format(record)
msg = "From: %s\r\nTo: %s\r\nSubject: %s\r\nDate: %s\r\n\r\n%s" % (
self.fromaddr,
self.toaddrs,
self.getSubject(record),
formatdate(), msg)
if self.username:
smtp.ehlo()
smtp.starttls()
smtp.ehlo()
smtp.login(self.username, self.password)
smtp.sendmail(self.fromaddr, self.toaddrs, msg)
smtp.quit()
class Printer:
def Simulator():
logger = logging.getLogger('simulator')
logger.setLevel(logging.DEBUG)
simprinthandler = PrintHandler()
simprinthandler.setLevel(logging.DEBUG)
logger.addHandler(simprinthandler)
def Trader():
logger = logging.getLogger('trader')
logger.setLevel(logging.DEBUG)
tradeprinthandler = PrintHandler()
tradeprinthandler.setLevel(logging.DEBUG)
logger.addHandler(tradeprinthandler)
class TextFile:
def Simulator():
os.makedirs(gc.Notifier.TextFile.Path, exist_ok=True)
logger = logging.getLogger('simulator')
logger.setLevel(logging.DEBUG)
simhandler = handlers.TimedRotatingFileHandler(
gc.Notifier.TextFile.Path + '/' + gc.Notifier.TextFile.SimName,
when='h', interval=gc.Notifier.TextFile.RolloverTime,
backupCount=gc.Notifier.TextFile.BackupCount)
simhandler.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(message)s')
simhandler.setFormatter(formatter)
logger.addHandler(simhandler)
def Trader():
os.makedirs(gc.Notifier.TextFile.Path, exist_ok=True)
logger = logging.getLogger('trader')
logger.setLevel(logging.DEBUG)
tradehandler = handlers.TimedRotatingFileHandler(
gc.Notifier.TextFile.Path + '/' + gc.Notifier.TextFile.TradeName,
when='h', interval=gc.Notifier.TextFile.RolloverTime,
backupCount=gc.Notifier.TextFile.BackupCount)
tradehandler.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(message)s')
tradehandler.setFormatter(formatter)
logger.addHandler(tradehandler)
class Pushover:
def Simulator():
logger = logging.getLogger('simulator')
logger.setLevel(logging.DEBUG)
simpushoverhandler = PushoverHandler()
simpushoverhandler.setLevel(logging.DEBUG)
logger.addHandler(simpushoverhandler)
def Trader():
logger = logging.getLogger('trader')
logger.setLevel(logging.DEBUG)
tradepushoverhandler = PushoverHandler()
tradepushoverhandler.setLevel(logging.DEBUG)
logger.addHandler(tradepushoverhandler)
class TlsSMTP:
def Simulator():
logger = logging.getLogger('simulator')
logger.setLevel(logging.DEBUG)
simgm = TlsSMTPHandler((gc.Notifier.TlsSMTP.Host, gc.Notifier.TlsSMTP.Port),
gc.Notifier.TlsSMTP.To, [gc.Notifier.TlsSMTP.To],
'Avarice Simulator', (gc.Notifier.TlsSMTP.Username,
gc.Notifier.TlsSMTP.Password))
simgm.setLevel(logging.DEBUG)
logger.addHandler(simgm)
def Trader():
logger = logging.getLogger('trader')
logger.setLevel(logging.DEBUG)
tradergm = TlsSMTPHandler((gc.Notifier.TlsSMTP.Host, gc.Notifier.TlsSMTP.Port),
gc.Notifier.TlsSMTP.To, [gc.Notifier.TlsSMTP.To],
'Avarice Trader', (gc.Notifier.TlsSMTP.Username,
gc.Notifier.TlsSMTP.Password))
tradergm.setLevel(logging.DEBUG)
logger.addHandler(tradergm)
class SMTP:
def Simulator():
logger = logging.getLogger('simulator')
logger.setLevel(logging.DEBUG)
simsmtphandler = handlers.SMTPHandler(
gc.Notifier.SMTP.Host, gc.Notifier.SMTP.From, gc.Notifier.SMTP.To,
'Avarice ' + 'Simulator')
simsmtphandler.setLevel(logging.DEBUG)
logger.addHandler(simsmtphandler)
def Trader():
logger = logging.getLogger('trader')
logger.setLevel(logging.DEBUG)
tradersmtphandler = handlers.SMTPHandler(
gc.Notifier.SMTP.Host, gc.Notifier.SMTP.From, gc.Notifier.SMTP.To,
'Avarice ' + 'Trader')
tradersmtphandler.setLevel(logging.DEBUG)
logger.addHandler(tradersmtphandler)