-
Notifications
You must be signed in to change notification settings - Fork 0
/
erase.py
executable file
·103 lines (87 loc) · 3.15 KB
/
erase.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
import os
import lxml.html
# Single xpath helper
def xpath0(htmlelement, xpath):
results = htmlelement.xpath(xpath)
if len(results) == 0:
return None
elif len(results) == 1:
return results[0]
else:
raise ValueError('The xpath matched more than one node')
lxml.html.HtmlElement.xpath0 = xpath0
del(xpath0)
PIWIK = lxml.html.fromstring('''<!-- Piwik -->
<script type="text/javascript">
var pkBaseURL = (("https:" == document.location.protocol) ? "https://piwik.thomaslevine.com/" : "http://piwik.thomaslevine.com/");
document.write(unescape("%3Cscript src='" + pkBaseURL + "piwik.js' type='text/javascript'%3E%3C/script%3E"));
</script><script type="text/javascript">
try {
var piwikTracker = Piwik.getTracker(pkBaseURL + "piwik.php", 8);
piwikTracker.trackPageView();
piwikTracker.enableLinkTracking();
} catch( err ) {}
</script><noscript><p><img src="http://piwik.thomaslevine.com/piwik.php?idsite=8" style="border:0" alt="" /></p></noscript>
<!-- End Piwik Tracking Code -->
''')
def add_piwik(html):
"Add Piwik if it isn't already there."
if html.xpath0('//img[@src="http://piwik.thomaslevine.com/piwik.php?idsite=8"]') == None:
body = html.xpath0('//body')
body.append(PIWIK)
added = True
else:
# Already added
added = False
return added, html
def remove_watermark(html):
watermark = html.xpath0(
'//div[@style="position:absolute;left:50;top:50;background:white"]'
'[font[@color="black"]]'
)
if watermark is not None:
watermark.getparent().remove(watermark)
removed = True
else:
removed = False
return removed, html
def edit_files():
removed_watermark = []
added_piwik = []
for filename in os.listdir(SITE_ROOT):
if filename[-5:] != '.html':
continue
absolute_path = os.path.join(SITE_ROOT, filename)
f = open(absolute_path, 'r')
html = lxml.html.fromstring(f.read())
f.close()
removed_watermark_from_file, html = remove_watermark(html)
added_piwik_to_file, html = add_piwik(html)
if removed_watermark_from_file:
removed_watermark.append(filename)
if added_piwik_to_file:
added_piwik.append(filename)
text_out = lxml.html.tostring(html)
f = open(absolute_path, 'w')
f.write(text_out)
f.close()
print 'Removed the watermark from these files:'
print '\n '.join(removed_watermark)
print 'Added the piwik analytics snippet to these files:'
print '\n '.join(added_piwik)
def http_headers():
print 'HTTP/1.1 200 OK'
print 'Content-Type: text/plain'
print '\n'
def main():
os.system('cd /home/www.fadelee.com && git init')
# Don't update before; this results in stupid backups
# os.system('cd /home/www.fadelee.com && git commit . -m new\ upload')
edit_files()
os.system('cd /home/www.fadelee.com && git commit . -m removed\ watermark')
# The only bit of configuration: Set this to the site's directory.
SITE_ROOT = os.path.join('/', 'home', 'www.fadelee.com')
if __name__ == '__main__':
if 'QUERY_STRING' in os.environ:
http_headers()
main()