forked from ecprice/newsdiffs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
monitor_website_status.py
49 lines (40 loc) · 1.54 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
#!/usr/bin/python
import urllib2
from BeautifulSoup import BeautifulSoup
from StringIO import StringIO
from datetime import datetime, timedelta
import dateutil.parser
import subprocess
WEBSITE = 'http://www.newsdiffs.org/browse/'
if datetime.now().hour < 8: #Overnight, less frequent updates
MAX_TIME = timedelta(minutes=60)
else:
MAX_TIME = timedelta(minutes=30)
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].findChild('a').getText()
date = dateutil.parser.parse(datestr)
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())