Skip to content

Instantly share code, notes, and snippets.

@hdf
Created August 2, 2014 17:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hdf/c4ada7686f6674a0a9c9 to your computer and use it in GitHub Desktop.
Save hdf/c4ada7686f6674a0a9c9 to your computer and use it in GitHub Desktop.
import os, sys, re
dir = 'files'
if len(sys.argv) > 1:
dir = sys.argv[1]
trackingcode = """\g<1>
<a title="Web Analytics" href="http://clicky.com/100759434"><img alt="Web Analytics" src="//static.getclicky.com/media/links/badge.gif" border="0" /></a>
<script src="//static.getclicky.com/js" type="text/javascript"></script>
<script type="text/javascript">try{ clicky.init(100759434); }catch(e){}</script>
<noscript><p><img alt="Clicky" width="1" height="1" src="//in.getclicky.com/100759434ns.gif" /></p></noscript>
\g<2>"""
pattern = re.compile("(<!--end\ footer-->).*(<\/body>)", re.I | re.M | re.S)
for root, subFolders, files in os.walk(dir):
for file in files:
if re.search('\.htm|\.html', file):
filename = os.path.join(root, file)
f = open(filename, 'r+')
s = f.read()
f.seek(0)
f.write(pattern.sub(trackingcode, s))
f.truncate()
f.close()
print filename + " updated."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment