#!/usr/bin/perl use strict; use warnings; use Digest::HMAC_SHA1; use LWP::UserAgent; use POSIX qw(strftime); # Define our Access Key Id, and Secret Access Key. use constant ACCESS_KEY_ID => 'xxxxxxxxxxxxxxxxxxxx'; use constant SECRET_ACCESS_KEY => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; # Downloads the next document from PDF Alchemy. # Returns the documentId on success. sub downloadNext { # Generate the HMAC signature. my $date=strftime '%a, %d %b %Y %H:%M:%S GMT', gmtime; my $request='/'.ACCESS_KEY_ID.'/outqueue/next'; my $hmac=Digest::HMAC_SHA1->new(SECRET_ACCESS_KEY); $hmac->add("GET\n".ACCESS_KEY_ID."\n$date\n$request"); my $signature=$hmac->b64digest; while (length($signature)%4) { $signature.='='; } # Request the next document from PDF Alchemy. my @headers=('Date'=>$date,'X-PDF-Alchemy-Signature'=>$signature); my $browser=LWP::UserAgent->new; my $response=$browser->get("http://www.pdfalchemy.com$request",@headers); if ($response->is_error) { print $response->as_string; return; } # Write the response to file. my($filename)=($response->header('Content-Disposition') =~ m/"([^"]+)"/); open(FILE,'>',$filename) or die $!; binmode FILE, ':raw'; print FILE $response->content;; close(FILE); print "Downloaded '$filename'.\n"; # Return the documentId. return $response->header('X-PDF-Alchemy-ID'); } # Removes documentId from the PDF Alchemy outqueue. sub deleteDocument { my $documentId=shift(@_); # Generate the HMAC signature. my $date=strftime '%a, %d %b %Y %H:%M:%S GMT', gmtime; my $request='/'.ACCESS_KEY_ID."/outqueue/$documentId"; my $hmac=Digest::HMAC_SHA1->new(SECRET_ACCESS_KEY); $hmac->add("DELETE\n".ACCESS_KEY_ID."\n$date\n$request"); my $signature=$hmac->b64digest; while (length($signature)%4) { $signature.='='; } # Remove the document from PDF Alchemy. my @headers=('Date'=>$date,'X-PDF-Alchemy-Signature'=>$signature); my $browser=LWP::UserAgent->new; my $req=HTTP::Request->new(DELETE=>"http://www.pdfalchemy.com$request",HTTP::Headers->new(@headers)); my $response=$browser->request($req); print $response->as_string if $response->is_error; print "Removed '$documentId' from PDF Alchemy.\n" if $response->is_success; } my $documentId; do { $documentId=downloadNext; deleteDocument $documentId if $documentId; } while $documentId;