forked from ecprice/newsdiffs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
monitor_website_status.py
54 lines (45 loc) · 1.67 KB
/
monitor_website_status.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
#!/usr/bin/python
import subprocess
import urllib2
from datetime import datetime, timedelta
from StringIO import StringIO
import dateutil.parser
from BeautifulSoup import BeautifulSoup
WEBSITE = 'http://www.newsdiffs.org/browse/'
if datetime.now().hour < 8: #Overnight, less frequent updates
MAX_TIME = timedelta(minutes=120)
else:
MAX_TIME = timedelta(minutes=60)
def send_alert_email(subject, body):
email = 'Subject: %s\n\n%s' % (subject, body)
p = subprocess.Popen(['/usr/bin/msmtp', '-t'] + EMAILS,
stdin=subprocess.PIPE)
p.communicate(email)
if p.wait():
print 'Bad return code:', p.returncode
def get_update_time():
html = urllib2.urlopen(WEBSITE)
soup = BeautifulSoup(html)
datestr = soup.findAll('td')[1].getText()
datestr = datestr.replace('midnight', '12:00am').replace('noon', '12:00pm')
try:
date = dateutil.parser.parse(datestr)
except ValueError:
print datestr
raise
return date
if __name__ == '__main__':
try:
update_time = get_update_time()
time_since_update = datetime.now() - update_time
print 'Update time:', time_since_update
if time_since_update > MAX_TIME:
send_alert_email('Trouble with newsdiffs.org',
'No updates since %s\n%s is too long' %
(update_time, time_since_update))
except Exception, e:
import traceback
traceback.print_exc()
send_alert_email('Trouble with newsdiffs.org',
'Cannot check website\n%s' % traceback.format_exc())