Merge branch 'master' of ssh://git.camperquake.de:22003/quotesite
[quotesite.git] / quotesite / AmazonGrabber.pm
1 # (c) 2007 by Ralf Ertzinger <ralf@camperquake.de>
2 # licensed under GNU GPL v2
3 #
4 # Grabber for Amazon
5
6 package quotesite::AmazonGrabber;
7
8 use quotesite::GrabberBase;
9 @ISA = qw(quotesite::GrabberBase);
10
11 use LWP::Simple qw(!get);
12 use HTML::TokeParser;
13 use Data::Dumper;
14 use Encode;
15
16 use strict;
17
18 sub new {
19     my $class = shift;
20     my $self = $class->SUPER::new();
21
22     $self->{'NAME'} = 'amazon';
23     $self->{'PATTERNS'} = ['(https?://(?:[-a-zA-Z0-9_.]+\.)*amazon\.(?:com|de|co\.uk|fr)/.*[dg]p(?:/product)?/([[:alnum:]]{10}))'];
24
25     bless($self, $class);
26     $self->_prepare_parameters();
27
28     return $self;
29 }
30
31 sub _parse {
32     my $self = shift;
33     my $url = shift;
34     my $pattern = shift;
35     my $content;
36     my $metadata = {};
37     my $p;
38     my $t;
39     my $t2;
40
41     $url =~ m|$pattern|;
42     $url = $1;
43
44     $metadata->{'URL'} = $url;
45     $metadata->{'ID'} = $2;
46     $metadata->{'TYPE'} = 'quote';
47     $metadata->{'SOURCE'} = $self->{'NAME'};
48     $metadata->{'CONTENT'} = undef;
49
50     # Get the HTML file containing the quote
51     unless(defined($content = LWP::Simple::get($url))) {
52         $self->error('Could not download quote');
53         return undef;
54     }
55
56     $self->debug($content);
57
58     $p = HTML::TokeParser->new(\$content);
59
60     OUTER: while ($t = $p->get_tag('h1')) {
61         if (exists($t->[1]->{'class'}) && ($t->[1]->{'class'} eq 'parseasinTitle')) {
62             $metadata->{'CONTENT'} = encode('utf8', decode('iso8859-1', $p->get_text('/h1')));
63             $metadata->{'CONTENT'} =~ s/^\s*//;
64             $metadata->{'CONTENT'} =~ s/\s*$//;
65             last OUTER;
66         }
67     }
68
69     unless(defined($metadata->{'CONTENT'})) {
70         $self->error('Could not extract quote content');
71         return undef;
72     }
73
74     return $metadata;
75 }
76
77 1;