Created
October 3, 2008 04:03
-
-
Save willb/14500 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# encoding: utf-8 | |
""" | |
addmint.py | |
Adds Mint analytics to a static HTML page; requires BeautifulSoup. | |
(I use this for Aperture exports.) | |
Created by Will Benton on 2008-07-15. | |
Copyright (c) 2008 Aition Technologies, LLC and Will Benton. All rights reserved. | |
""" | |
import sys | |
import getopt | |
from BeautifulSoup import BeautifulSoup, Tag, Comment | |
help_message = ''' | |
The help message goes here. | |
''' | |
# edit this next line or else pass the --url option | |
minturl = "http://YOURSERVER.com/mint/" | |
files = [] | |
class Usage(Exception): | |
def __init__(self, msg): | |
self.msg = msg | |
def process(filename): | |
global minturl | |
f = open(filename) | |
lines = f.read() | |
soup = BeautifulSoup(''.join(lines)) | |
head = soup.head | |
mtag = Tag(soup, "script") | |
mtag["src"] = "%s/?js" % minturl | |
mtag["type"] = "text/javascript" | |
head.insert(0, mtag) | |
f.close() | |
f = open(filename, "w") | |
f.write(soup.prettify()) | |
f.close() | |
def main(argv=None): | |
global minturl, files | |
if argv is None: | |
argv = sys.argv | |
try: | |
try: | |
opts, args = getopt.getopt(argv[1:], "hu:v", ["help", "url="]) | |
except getopt.error, msg: | |
raise Usage(msg) | |
# option processing | |
for option, value in opts: | |
if option == "-v": | |
verbose = True | |
if option in ("-h", "--help"): | |
raise Usage(help_message) | |
if option in ("-u", "--url"): | |
minturl = value | |
for f in args: | |
process(f) | |
except Usage, err: | |
print >> sys.stderr, sys.argv[0].split("/")[-1] + ": " + str(err.msg) | |
print >> sys.stderr, "\t for help use --help" | |
return 2 | |
if __name__ == "__main__": | |
sys.exit(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment