-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwebmo.py
More file actions
118 lines (89 loc) · 3.15 KB
/
webmo.py
File metadata and controls
118 lines (89 loc) · 3.15 KB
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
#!/usr/bin/env python
"""
Webmo
Enter full URL and monitor frequency in seconds to get started
"""
import sys
import urllib2
from datetime import datetime
import time
from bs4 import BeautifulSoup
import smtplib
from email.mime.text import MIMEText
from ConfigParser import SafeConfigParser
# Get current time in pretty format
def pretty_time():
now = datetime.now()
pretty_time_format = now.strftime("%Y-%m-%d %H:%M:%S")
return pretty_time_format
# Query the content from given URL
def website_content_query(website_url):
try:
soup = BeautifulSoup(urllib2.urlopen(website_url))
website_content = str(soup)
except urllib2.HTTPError or urllib2.URLError:
print "There is an error with the request"
return
return website_content
# Send email notification
def send_notification(result):
# Load and parse config file
parser = SafeConfigParser()
parser.read('settings.cfg')
recipient_email_str = parser.get('email', 'recipient')
recipient_email_list = recipient_email_str.split()
gmail_user = parser.get('email', 'gmail_user')
gmail_pwd = parser.get('email', 'gmail_pwd')
# Create minimal message
msg = MIMEText(result)
msg['From'] = gmail_user
msg['To'] = recipient_email_str
msg['Subject'] = "Notification"
# Send message
mail_server = smtplib.SMTP('smtp.gmail.com', 587)
mail_server.ehlo()
mail_server.starttls()
mail_server.ehlo()
mail_server.login(gmail_user, gmail_pwd)
mail_server.sendmail(gmail_user, recipient_email_list, msg.as_string())
mail_server.close()
return
def main(argv=None):
if argv is None:
argv = sys.argv
#0 Check and parse user input
if len(argv) < 3:
print "Please enter website URL and query frequency"
exit()
website_url = argv[1]
monitor_frequency = argv[2]
#1 Initial query for the website content to compare against new query
old_content = website_content_query(website_url)
while True:
#2 Compare length of old and new content and process the result
temp_new_content = website_content_query(website_url)
# If there is an error with query result, skip this round of check
if temp_new_content is None:
time.sleep(int(monitor_frequency))
continue
else:
new_content = temp_new_content
# Send notification if there is any difference
if len(new_content) != len(old_content):
current_time = pretty_time()
result = current_time + " - Updated - " + website_url + "\n"
# Send email notification
send_notification(result)
# The updated content becomes old content for successive query
old_content = new_content
# Record unchanged if there is no difference
else:
current_time = pretty_time()
result = current_time + " - Unchanged - " + website_url + "\n"
#3 Log result
with open('./report.log', 'a') as f:
f.write(result)
#4 Wait for specified time period
time.sleep(int(monitor_frequency))
if __name__ == "__main__":
sys.exit(main())