1341b960b444e82ea0ee33bb0964d1a634274b31
[quotesite.git] / quotesite / PolitBashOrgGrabber.pm
1 # (c) 2007 by Ralf Ertzinger <ralf@camperquake.de>
2 # licensed under GNU GPL v2
3 #
4 # Grabber for polit-bash.org
5
6 package PolitBashOrgGrabber;
7
8 use GrabberBase;
9 @ISA = qw(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'} = 'polit-bash.org';
23     $self->{'PATTERNS'} = ['(http://(?:[-a-zA-Z0-9_.]+\.)*polit-bash\.org/\?(\d+))'];
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(sprintf('http://polit-bash.org/?%s', $2)))) {
52         $self->error('Could not download quote');
53         return undef;
54     }
55     $content = decode("iso-8859-1", $content);
56
57     $p = HTML::TokeParser->new(\$content);
58
59     OUTER: while ($t = $p->get_tag('p')) {
60         if (exists($t->[1]->{'class'}) && ($t->[1]->{'class'} eq 'quote')) {
61             $self->debug("Found p");
62             $metadata->{'CONTENT'} = $p->get_text('/p');
63             last OUTER;
64         }
65     }
66
67     unless(defined($metadata->{'CONTENT'})) {
68         $self->error('Could not extract quote content');
69         return undef;
70     }
71
72     $metadata->{'CONTENT'} = encode("utf-8", $metadata->{'CONTENT'});
73
74     return $metadata;
75 }
76
77 1;