#!/usr/bin/perl # Filename: photoreflect # Author: David Ljung Madison # See License: http://MarginalHacks.com/License/ # Description: Downloads a set of photoreflect images without watermarks use strict; ## Thumbnail pages #http://www.photoreflect.com/pr3/OrderPage.aspx?pi=0R1G000Z000000&po=0&c=3UJ95V ## A photoreflect image page URL looks like this: # http://www.photoreflect.com/pr3/OrderPage.aspx?pi=0R1G0011000000&po=10&c=3UJ95V ## The watermarked image can be found by looking for 'startingImgSrc': # WAS: http://www.photoreflect.com/prbin/prpv.dll?photo?s=0&i=0R1G000Z000010&p=3UJ95V # NOW: http://www.photoreflect.com/prbin/prpv.dll?photo?s=0&i=0R1G0011000000&p=&e= ## And s=-1 gives us the non-watermarked image # http://www.photoreflect.com/prbin/prpv.dll?photo?s=-1&i=0R1G0011000000&p=&e= # We could get the number of images from the image page: # "var imageCount = 138;" my $IMAGE = 'http://www.photoreflect.com/prbin/prpv.dll?photo?s=-1&i=%{pi}&p=%c'; #my $SAVE = '%{pi}_%c.jpg'; ## We don't need the %c for the save! my $SAVE = '%{pi}.jpg'; # Downloader my $GET = "GET -H 'user-agent: Mozilla/5.0'"; my $LYNX = "lynx -source"; # Pick one my $FETCH = $LYNX; ################################################## # Setup the variables ################################################## my $PROGNAME = $0; $PROGNAME =~ s|.*/||; my ($BASENAME,$PROGNAME) = ($0 =~ m|(.*)/(.+)|) ? ($1?$1:'/',$2) : ('.',$0); ################################################## # Usage ################################################## sub fatal { foreach my $msg (@_) { print STDERR "[$PROGNAME] ERROR: $msg\n"; } exit(-1); } sub usage { foreach my $msg (@_) { print STDERR "ERROR: $msg\n"; } print STDERR < \tDownload a set of photoreflect images without watermarks \t-d\tSet debug mode Go to the first image page of a photoreflect photo set. You *must* go to this image by clicking on the thumbnail page, do not navigate to it from another image using the next/prev buttons. The second arg is the number of images in the set, or the number you want. You will likely need to put the url in quotes (depending on your shell) USAGE exit -1; } sub parse_args { my $opt = {}; while (my $arg=shift(@ARGV)) { if ($arg =~ /^-h$/) { usage(); } if ($arg =~ /^-d$/) { $MAIN::DEBUG=1; next; } if ($arg =~ /^-/) { usage("Unknown option: $arg"); } if (!$opt->{url}) { $opt->{url} = $arg; next; } if ($arg =~ /^\d+$/) { $opt->{cnt} = $arg; next; } usage(); } usage("No url defined") unless $opt->{url}; $opt; } sub debug { return unless $MAIN::DEBUG; foreach my $msg (@_) { print STDERR "[$PROGNAME] $msg\n"; } } ################################################## # URL wrangling ################################################## sub parseQuery { my ($url) = @_; my %ret; return \%ret unless $url =~ s/.*\?//; foreach my $kv ( split('&',$url) ) { my ($k,$v) = split('=',$kv,2); $ret{$k}=$v; $ret{"orig_$k"}=$v; } return \%ret; } sub replaceQuery { my ($url,$q) = @_; while ($url =~ s/%{([^}]+)}/$q->{$1}/g || $url =~ s/%([^{])/$q->{$1}/g) {}; $url; } sub interrupt { die("[$PROGNAME] Interrupted\n"); } $SIG{INT} = \&interrupt; $SIG{TERM} = \&interrupt; $SIG{HUP} = \&interrupt; $SIG{QUIT} = \&interrupt; $SIG{EXIT} = \&interrupt; $SIG{__DIE__} = \&interrupt; sub getURL { my ($url,$to) = @_; system("$FETCH \Q$url\E > \Q$to\E"); my ($exit,$signal,$dump) = ($? >> 8, $? & 127, $? & 128); fatal("Error from $GET:\n code: $!") if $exit; fatal("Core dump for:\n code: $!") if $dump; interrupt() if $signal; } sub inc { my ($q) = @_; # Increment the values. Add one to pi, and increment each piece of c $q->{pi} =~ s/(\d{1,4})$//; my ($num,$len) = ($1,length($1)); $num++; $q->{pi} .= sprintf("%0.${len}d",$num); my @c = split(//,$q->{c}); @c = map { $_++; substr($_,-1,1); } @c; # Every 10 we start over?? @c = split(//,$q->{orig_c}) if !($num%10); # For every 100 we add one to the third letter?? my $hun = $num%10 ? 0 : int($num/100); while ($hun--) { $c[2]++; } $c[2] = substr($c[2],-1,1); # Every 1000, what happens??? $q->{c} = join('', @c); } ################################################## # Main code ################################################## sub main { my $opt = parse_args(); debug("$opt->{url}\n"); my $query = parseQuery($opt->{url}); usage("URL missing 'pi=...' query") unless $query->{pi}; usage("'pi=...' query must end in a number") unless $query->{pi} =~ /\d$/; $query->{c} |= 0; # Photoreflect doesn't use 'c=' always. usage("URL missing 'c=...' query") unless defined $query->{c}; debug(" pi = $query->{pi}"); debug(" c = $query->{c}"); my $cnt=0; while ($cnt++< $opt->{cnt}) { my $img = replaceQuery($IMAGE,$query); debug("Img: $img"); my $save = replaceQuery($SAVE,$query); print "$save\n"; getURL($img,$save); #unless $cnt<109; inc($query); } } main();