#!/usr/local/bin/python # # FilemarkMaker.py # # Maps files to data URI's and inserts them into an HTML file so they may # be bookmarked in a browser. # import sys import exceptions import base64 # Check the inputs. if len( sys.argv ) == 2: inFileName=sys.argv[1] outFileName = inFileName + "_filemark.html" else: sys.stderr.write( "Usage: python FilemarkMaker.py .\n" ) sys.exit() # File must be in binary mode on Windows (DOS) systems. try: fin=open( inFileName, "rb" ) data=fin.read() fin.close() except Exception, e: print "Cannot open the file %s: %s\n" % (inFileName, e) sys.exit() # Convert each 6-bit chunk of input data to encoded ascii data. try: encodedData = base64.b64encode( data ) except Exception, e: print "Problem converting data to base64 encoding: %s\n" % e sys.exit() # Determine the file type from the file suffix. contentType = "application/octet-stream" if inFileName.endswith( "gif" ): contentType = "image/gif" elif inFileName.endswith( "jpg" ) or inFileName.endswith( "jpeg" ): contentType = "image/jpeg" elif inFileName.endswith( "png" ): contentType = "image/png" elif inFileName.endswith( "pdf" ): contentType = "application/pdf" elif inFileName.endswith( "html" ) or inFileName.endswith( "htm" ) or \ inFileName.endswith( "shtml" ) or inFileName.endswith( "php" ): contentType = "text/html" elif inFileName.endswith( "txt" ): contentType = "text/plain" elif inFileName.endswith( "rtf" ) or inFileName.endswith( "rtfd" ): contentType = "application/rtf" elif inFileName.endswith( "doc" ): # M$ stuff contentType = "application/msword" elif inFileName.endswith( "ppt" ): contentType = "application/vnd.ms-powerpoint" elif inFileName.endswith( "xls" ): contentType = "application/vnd.ms-excel" # Data has been converted to a MIME type using the data URI scheme: RFC 2397, http://tools.ietf.org/html/rfc2397 uri="data:" + contentType + ";charset=utf-8;base64," + encodedData try: fout=open( outFileName, "w" ) prelude="Link this" step1="Step 1 Right click on this link in Safari and choose \'Add link to bookmarks\'.
" % uri step2="Step 2 Sync your iPhone
" step3="Step 3 On the iPhone click the bookmark to view the file!
" credits="Ported to python from the perl script FileMarkMaker.pl by Insanely Great Tees and Jamie Wilkinson" postlude="" html=prelude+step1+step2+step3+credits+postlude fout.write( html ) fout.close() except Exception, e: print "Problem opening the file %s: %s\n" % (outFileName, e) sys.exit() print "Converted file %s\n" % inFileName print "Open the file %s with your browser and follow the instructions.\n" % outFileName