-
Notifications
You must be signed in to change notification settings - Fork 2
/
sites.py
69 lines (54 loc) · 1.75 KB
/
sites.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
'''
Used as a centralized way to send email over an SMTP server.
'''
import os
import urllib2
import shutil
from BeautifulSoup import BeautifulSoup
def processSitesInFile(sitesLines):
"""
Iterate over the lines from the sites.txt and create a
dictionary that maps the sites' names to their url.
sitesLines -- list of Strings directly taken from the file
"""
sites = {}
for site in sitesLines:
site = site.strip()
# Ignore lines starting with # as comments
if site.startswith("#"):
continue
try:
parts = site.split("=")
if len(parts) == 2:
sites[parts[0].strip()] = parts[1].strip()
else:
print "Unknown site format: {0}".format(site)
except exception as err:
print "Error: {0}".format(err)
return sites
def checkSite(siteDict, site):
"""
Download the site and diff it with the old version when it was downloaded
before.
siteDict -- the dictionary of name->url
site -- the name of the site as used in the siteDict
return: result of the diff
"""
diff = None
url = siteDict[site]
hash = url.__hash__()
content = urllib2.urlopen(url).read()
prettyContent = BeautifulSoup(content).prettify()
if not os.path.exists(site + ".old"):
file = open(site + ".old", "w")
file.write(prettyContent)
file.close()
else:
oldfile = site + ".old"
newfile = site + ".new"
file = open(newfile, "w")
file.write(prettyContent)
file.close()
diff = os.popen("diff -uw " + oldfile + " " + newfile).read()
shutil.move(newfile, oldfile)
return diff