#!/usr/bin/python from datetime import datetime import base64 import hmac import httplib import sha import urllib ACCESS_KEY_ID="xxxxxxxxxxxxxxxxxxxx" SECRET_ACCESS_KEY="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" # Downloads the next document from PDF Alchemy. # Returns the documentId on success, or None on failure. def downloadNext(): # Generate the HMAC signature. date=datetime.utcnow().strftime("%a, %d %b %Y %H:%M:%S GMT") request="/"+ACCESS_KEY_ID+"/outqueue/next" clearText="GET\n"+ACCESS_KEY_ID+"\n"+date+"\n"+request signature=base64.encodestring(hmac.new(SECRET_ACCESS_KEY,clearText,sha).digest()).strip() # Request the next document from PDF Alchemy. conn=httplib.HTTPConnection("www.pdfalchemy.com") headers={"date":date, "X-PDF-Alchemy-Signature":signature} conn.request("GET",request,None,headers) resp = conn.getresponse() if resp.status >= 300: print str(resp.status)+": "+resp.reason+": "+resp.read() conn.close() else: filename=resp.getheader("Content-Disposition").split('"')[1] file=open(filename,"wb") file.write(resp.read()) file.close() resp.close() conn.close() print "Downloaded '%s'." % filename return resp.getheader("X-PDF-Alchemy-ID") # Removes documentId from the PDF Alchemy outqueue. def deleteDocument(documentId): # Generate the HMAC signature. date=datetime.utcnow().strftime("%a, %d %b %Y %H:%M:%S GMT") request="/"+ACCESS_KEY_ID+"/outqueue/"+urllib.quote(documentId) clearText="DELETE\n"+ACCESS_KEY_ID+"\n"+date+"\n"+request signature=base64.encodestring(hmac.new(SECRET_ACCESS_KEY,clearText,sha).digest()).strip() # Remove the document from PDF Alchemy. conn=httplib.HTTPConnection("www.pdfalchemy.com") headers={"date":date, "X-PDF-Alchemy-Signature":signature} conn.request("DELETE",request,None,headers) resp = conn.getresponse() if resp.status >= 300: print str(resp.status)+": "+resp.reason+": "+resp.read() else: print "Deleted '%s'." % documentId conn.close() documentId="start" # Just something non-empty to kick off the while lopp. while documentId: documentId=downloadNext() if documentId: deleteDocument(documentId)