# MT-WAIR # # A "What am I reading" plugin for MT # # Ricardo Cerqueira, 2003 # Minor updates by Noel Llopis 2004-2005 package MT::Plugin::MTWAIR; use strict; use XML::Simple; use LWP::UserAgent; use MT::Template::Context; use Data::Dumper; use LWP::Simple; use POSIX qw(tmpnam); use MT::Image; my %data_cache; ## If you use amazon.co.uk to buy & search for your books, uncomment # the following line and comment the one below #my $europe="&locale=uk"; my $europe=""; MT::Template::Context->add_container_tag( wair => \&wair ); MT::Template::Context->add_tag(wairName => sub { shift->stash('wairName'); } ); MT::Template::Context->add_tag(wairAuthor => sub { shift->stash('wairAuthor'); } ); MT::Template::Context->add_tag(wairImage => sub { shift->stash('wairImage'); } ); MT::Template::Context->add_tag(wairImageWidth => sub { shift->stash('wairImageWidth'); } ); MT::Template::Context->add_tag(wairImageHeight => sub { shift->stash('wairImageHeight'); } ); MT::Template::Context->add_tag(wairURL => sub { shift->stash('wairURL'); } ); sub wair { my ($ctx, $args) = @_; my $asin = $ctx->stash('mt-list_current') ? $ctx->stash('mt-list_current_item') : $args->{asin}; return $ctx->error("No ASIN provided!") unless $asin; my $img_size = $ctx->stash('mt-list_current') ? $ctx->stash('mt-list_current_item') : $args->{img_size}; if (!$img_size) { $img_size = "normal"; } my $bookdata = getData($asin, $img_size); if ($bookdata) { $ctx->stash("wairName" => $bookdata->{name}); $ctx->stash("wairAuthor" => $bookdata->{author}); $ctx->stash("wairImage" => $bookdata->{image}); $ctx->stash("wairImageWidth" => $bookdata->{image_width}); $ctx->stash("wairImageHeight" => $bookdata->{image_height}); $ctx->stash("wairURL" => $bookdata->{amazonurl}); } my $tokens = $ctx->stash('tokens'); my $builder = $ctx->stash('builder'); my $ret = $builder->build($ctx, $tokens); #return $ctx->error($builder->errstr) unless defined $ret; return "Unable to retrieve data from Amazon!" unless defined $ret; return $ret; } sub getData { my ($asin, $img_size) = @_; my $key = $asin.$img_size; if (!$data_cache{$key}) { my $result = getAmazonData($asin, $img_size); if ($result) { $data_cache{$key} = $result; } } return $data_cache{$key}; } sub getAmazonData { my ($asin, $img_size) = @_; print "Fetching $asin ($img_size)...\n"; sleep 0.1; my $ua = LWP::UserAgent->new( keep_alive => 0, timeout => 15, agent => 'Mozilla/5.0 (X11; U; Linux i686; rv:1.7.3)' ); my $host = $europe ? "xml-eu.amazon.com" : "xml.amazon.com"; my $url="http://$host/onca/xml3?t=webservices-20&dev-t=D1NAK9R26OAS2&AsinSearch=$asin&type=heavy&f=xml$europe"; #print "Querying Amazon for ASIN: $asin...\n"; my $response = $ua->get($url); if (!$response->is_success) { print "URL: $url\n"; print "Status: " . $response->status_line . "\n"; print Dumper($response); die "Error fetching data"; } my $xmldata = $response->content; my $book=XMLin($xmldata); my %result; $result{name} = $book->{Details}->{ProductName}; $result{author} = $book->{Details}->{Authors} ? $book->{Details}->{Authors}->{Author} : $book->{Details}->{Artists}->{Artist}; if ($img_size eq "large") { $result{image} = $book->{Details}->{ImageUrlLarge}; } elsif ($img_size eq "small") { $result{image} = $book->{Details}->{ImageUrlSmall}; } else { $result{image} = $book->{Details}->{ImageUrlMedium}; } $result{amazonurl} = "http://www.amazon.com/exec/obidos/ASIN/" . $book->{Details}->{Asin}; ($result{image_width}, $result{image_height}) = GetRemoteImageSize($result{image}); if (ref $result{author} eq 'ARRAY') { $result{author} = join ", ",@{$result{author}}; } return $result{name} ? \%result : undef; } sub GetRemoteImageSize { my ($url) = @_; print " Fetching image $url...\n"; my $tmpname = tmpnam(); my $fileext; # Preserve the filename extension if ($url =~ /(.+)?(\..+)/) { $fileext = $2; $tmpname .= $fileext; } if (!is_success(getstore ($url, $tmpname))) { print "Could not fetch $url\n"; return; } #print "Read $url into $tmpname\n"; my $img_type; if ($fileext =~ m/^\.gif$/i) { $img_type = 'gif'; } elsif ($fileext =~ m/^\.(jpg)|(jpeg)$/i) { $img_type = 'jpeg'; } elsif ($fileext =~ m/^\.png$/i) { $img_type = 'png'; } else { print "Extension not recognized.\n"; return; } my $img = MT::Image->new( Filename => $tmpname, Type => $img_type ) or return; return ($img->{width}, $img->{height}); END { if ($tmpname && -e $tmpname) { unlink($tmpname); } } } 1;