#!/usr/bin/python from datetime import datetime import base64 import hmac import httplib from os import listdir, path import sha import sys import urllib ACCESS_KEY_ID="xxxxxxxxxxxxxxxxxxxx" SECRET_ACCESS_KEY="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" # Uploads a single file to PDF Alchemy. def uploadFile(filename): print "Uploading '%s':" % path.basename(filename), sys.stdout.flush() # Generate the HMAC signature. date=datetime.utcnow().strftime("%a, %d %b %Y %H:%M:%S GMT") request="/"+ACCESS_KEY_ID+"/inqueue/"+urllib.quote(path.basename(filename)) clearText="PUT\n"+ACCESS_KEY_ID+"\n"+date+"\n"+request signature=base64.encodestring(hmac.new(SECRET_ACCESS_KEY,clearText,sha).digest()).strip() # Upload the file to PDF Alchemy. conn=httplib.HTTPConnection("www.pdfalchemy.com") headers={"date":date, "X-PDF-Alchemy-Signature":signature} file=open(filename,"rb") conn.request("PUT",request,file.read(),headers) file.close() resp = conn.getresponse() if resp.status >= 300: print str(resp.status)+": "+resp.reason+":", print resp.read() conn.close() # Uploads all convertable files from a single directory, to PDF Alchemy. def uploadFilesInDir(dirname): for filename in listdir(dirname): ext=path.splitext(filename)[1] if ext==".doc" or ext==".docx" or ext==".rft" or ext==".txt": uploadFile(path.normpath(dirname+'/'+filename)) for arg in sys.argv[1:]: if path.isdir(arg): uploadFilesInDir(arg) else: uploadFile(arg)