/
CheckForChange.py
executable file
·109 lines (86 loc) · 3.27 KB
/
CheckForChange.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
#!/usr/bin/python
import difflib
import smtplib
import time
import urllib2
################################################################################
# -- Get reference page
# -- Every 'secondsDelay' seconds download new page and compare with reference
# -- If different, send email message and update reference page with new page
remoteURL = 'http://www.foo.com/bar.html'
localFile = 'ref.html'
localURL = 'file:./' + localFile
username = 'your_username@googlemail.com'
password = 'your_password'
sendTo = ['foo@bar.com','bar@foo.com']
secondsDelay = 30
################################################################################
def emailChanges(refHTML, newHTML, diffList):
subject = 'Change in site: %s' % remoteURL
msg = ('From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n'
% (username, ', '.join(sendTo), subject) )
msg += '-------------------------------------------------------\r\n'
msg += '\r\n'
msg += '\r\n'
for line in diffList:
msg += line
msg += '\r\n'
msg += '\r\n'
msg += '\r\n'
msg += '-------------------------------------------------------\r\n'
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(username, password)
server.sendmail(username, sendTo, msg)
server.quit()
################################################################################
def getHTML( url ):
req = urllib2.Request( url )
try:
response = urllib2.urlopen( req )
except:
print 'Uh-oh... urlopen raised an exception...'
#if hasattr( e, 'reason' ):
# print 'Failed to reach server'
# print 'Reason: ', e.reason
#elif hasattr( e, 'code' ):
# print 'Server failed to fulfil request'
# print 'Error code: ', e.code
return False
else:
return response.read()
################################################################################
def writeHTML( fname, html ):
f = open( fname, 'w' )
f.write( html )
f.close()
################################################################################
def main():
print 'Getting initial reference file...'
refHTML = getHTML( remoteURL )
while( refHTML == False ):
print '...retrying...'
refHTML = getHTML( remoteURL )
writeHTML( localFile, refHTML )
while True:
print 'Getting latest page...', time.strftime('%a, %d %b %Y %H:%M:%S',time.localtime())
newHTML = getHTML( remoteURL )
while( newHTML == False ):
print '...retrying...'
newHTML = getHTML( remoteURL )
refHTML = getHTML( localURL )
diffList = difflib.context_diff(
newHTML.splitlines(),
refHTML.splitlines() )
listOfDiffs = list(diffList)
numDiffs = len( listOfDiffs )
if numDiffs == 0:
print "No change..."
time.sleep(secondsDelay)
else:
print "Page changed: sending email"
emailChanges(refHTML, newHTML, listOfDiffs)
print "Updating reference page..."
writeHTML( localFile, newHTML )
if __name__ == '__main__':
main();