1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
|
package MojoMojo::Formatter::Amazon;
eval "use Net::Amazon";
my $eval_res=$@;
=head2 module_loaded
Return true if the module is loaded.
=cut
sub module_loaded { $eval_res ? 0 : 1 }
our $VERSION='0.01';
=head1 NAME
MojoMojo::Formatter::Amazon - Include Amazon objects on your page.
=head1 DESCRIPTION
This is an url formatter. it takes urls containing amazon and
/-/ or /ASIN/ and make a pretty formatted link to that object
in the amazon web store.
It automatically handles books/movies/dvds and formats them as
apropriate. You can also pass 'small' as a parameter after the
url, and it will make a thumb link instead of a blurb.
=head1 METHODS
=head2 format_content_order
Format order can be 1-99. The Amazon formatter runs on 5.
=cut
sub format_content_order { 5 }
=head2 format_content
calls the formatter. Takes a ref to the content as well as the
context object.
=cut
sub format_content {
my ($class,$content,$c)=@_;
return unless $class->module_loaded;
my @lines=split /\n/,$$content;
my $pod;$$content="";
foreach my $line (@lines) {
if ($line =~ m/(\{\{?:http:\/\/(?:www\.){0,1}amazon\.com(?:\/.*){0,1}(?:\/dp\/|\/gp\/product\/))(.*?)(?:\/.*|$)\}\}/) {
my $item=$class->get($1,$c->config->{amazon_id});
unless (ref($item)) {
$$content.=$line."\n";
next;
}
if ($2) {
next unless $class->can($2);
$$content.=$class->$2($item);
} else {
$$content.=$class->blurb($item);
}
} else {
$$content .=$line."\n";
}
}
}
=head2 get <asin>
Connects to amazon and retrieves a L<Net::Amazon> object
based on the supplied ASIN number.
=cut
sub get {
my ($class,$id,$amazon_id,$secret_key)=@_;
#FIXME: devel token should be set in formatter config.
my $amazon=Net::Amazon->new(token=>$amazon_id,secret_key=>$secret_key);
my $response=$amazon->search(asin=>$id);
return "Unable to connect to amazon." unless $response->is_success;
($property)=$response->properties;
return "No property object" unless $property;
return $property;
}
=head2 small <property>
Renders a small version of the formatter.
=cut
sub small {
my ($class,$property)=@_;
return "!".$property->ImageUrlMedium.
'!:http://www.amazon.com/exec/obidos/ASIN/'.$property->Asin."/feed-20\n";
}
=head2 blurb <property>
renders a full width blurb of the product, suitable for reviews and
such.
=cut
sub blurb {
my ($class,$property)=@_;
my $method=ref $property;
$method =~ s/.*:://;
return "<div class=\"amazon\">!<".$property->ImageUrlSmall.
'!:http://www.amazon.com/exec/obidos/ASIN/'.$property->Asin."/feed-20\n\n".
"h1. ".$property->ProductName."\n\n".
'"buy at amazon for '.$property->OurPrice.'":'.
'http://www.amazon.com/exec/obidos/ASIN/'.$property->Asin."/feed-20\n\n".
($method && ($class->can($method) ? $class->$method($property) :"<br/>\n\n")).
"</div>";
}
=head2 DVD <property>
Product information suitable for DVD movies.
=cut
sub DVD {
my ($class,$property) = @_;
return " -- ??".join(',',$property->directors).'?? ('.$property->year .")\n\n";
}
=head2 Book <property>
Product information suitable for books.
=cut
sub Book {
my ($class,$property) = @_;
return " -- ??".join(',',$property->authors).'?? ('.$property->year .")\n\n";
}
=head2 Music <property>
Product information suitable for music CDs.
=cut
sub Music {
my ($class,$property) = @_;
return " -- ??".join(',',$property->artists).'?? ('.$property->year .")\n\n";
}
=head1 SEE ALSO
L<MojoMojo>, L<Module::Pluggable::Ordered>, L<Net::Amazon>.
=head1 AUTHORS
Marcus Ramberg <mramberg@cpan.org
=head1 LICENSE
This library is free software. You can redistribute it and/or modify
it under the same terms as Perl itself.
=cut
1;
|