forked from ahebrank/pommo_mailbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmailbot.py
101 lines (84 loc) · 2.3 KB
/
mailbot.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
import urllib2 as ul2
import urllib
import cookielib
import json
import datetime
import re
class Mailbot:
base_url = 'FULL URL FOR BASE POMMO FOLDER'
login_url = base_url + '/index.php'
status_url = base_url + '/ajax/status_poll.php'
restart_url = base_url + '/ajax/status_cmd.php?cmd=restart'
username = 'A POMMO ADMIN USERNAME'
password = 'PASSWORD'
# the number of minutes since the last update
# to trigger a restart of the MTA
threshold = 3
cookies = cookielib.LWPCookieJar()
opener = False
content = ""
status = False
def __init__(self):
# attempt to login
values = {'username' : self.username,
'password' : self.password,
'referer' : '/newsletter/admin.php',
'submit' : 'Log In' }
data = urllib.urlencode(values)
handlers = [
ul2.HTTPHandler(),
ul2.HTTPSHandler(),
ul2.HTTPCookieProcessor(self.cookies)
]
self.opener = ul2.build_opener(*handlers)
req = ul2.Request(self.login_url, data)
self.url_open(req)
def url_open(self, request):
try:
response = self.opener.open(request)
self.content = response.read()
except:
self.content = None
return False
else:
return True
def get_status(self):
req = ul2.Request(self.status_url)
if not self.url_open(req):
return 0
self.status = json.loads(self.content)
if self.status['status'] != 1:
"""
1 => Pommo::_T('Processing'),
2 => Pommo::_T('Stopped'),
3 => Pommo::_T('Frozen'),
4 => Pommo::_T('Finished')
"""
return 0
i = -1
err = True
while err:
# find the last timestamp in the logging mechanism
last_notice = self.status['notices'][i]
logtime = last_notice.split(' ')[0]
try:
last_notice_time = datetime.datetime.strptime(logtime, '%H:%M:%S')
except:
i -= 1
else:
current_time = datetime.datetime.now().replace(year = 1900, month = 1, day = 1)
# too long since last update?
if (current_time - last_notice_time) > datetime.timedelta(minutes = self.threshold):
return str(current_time - last_notice_time)
err = False
return 0
def restart(self):
req = ul2.Request(self.restart_url)
response = self.opener.open(req)
self.content = response.read()
if __name__ == '__main__':
m = Mailbot()
timediff = m.get_status()
if timediff != 0:
print "Last status %s ago; restarting mailer" % (timediff)
m.restart()