- Alter GermanBashGrabber to use LWP::UserAgent
[quotesite.git] / quotesite / GermanBashGrabber.pm
1 # (c) 2007 by Ralf Ertzinger <ralf@camperquake.de>
2 # licensed under GNU GPL v2
3 #
4 # Grabber for german-bash.org
5
6 package GermanBashGrabber;
7
8 use GrabberBase;
9 @ISA = qw(GrabberBase);
10
11 use LWP::UserAgent;
12 use HTML::TokeParser;
13 use Data::Dumper;
14
15 use strict;
16
17 sub new {
18     my $class = shift;
19     my $self = $class->SUPER::new();
20
21     $self->{'NAME'} = 'germanbash';
22     $self->{'PATTERNS'} = ['(http://(?:[-a-zA-Z0-9_.]+\.)*german-bash\.org/(\d+))',
23                            '(http://(?:[-a-zA-Z0-9_.]+\.)*german-bash\.org/action/show/id/(\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 $ua = LWP::UserAgent->new('agent' => 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)');
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     $content = $ua->get(sprintf('http://german-bash.org/%s', $2));
52     unless($content->is_success) {
53         $self->error('Could not download quote');
54         return undef;
55     }
56     $content = $content->decoded_content();
57
58     $p = HTML::TokeParser->new(\$content);
59
60     while ($t = $p->get_tag('div')) {
61         if (exists($t->[1]->{'class'}) && ($t->[1]->{'class'} eq 'zitat')) {
62             $metadata->{'CONTENT'} = $p->get_text('/div');
63             $metadata->{'CONTENT'} =~ s/^\s*//mg;
64             last;
65         }
66     }
67
68     unless(defined($metadata->{'CONTENT'})) {
69         $self->error('Could not extract quote content');
70         return undef;
71     }
72
73     return $metadata;
74 }
75
76 1;