# (c) 2007 by Ralf Ertzinger # licensed under GNU GPL v2 # # Grabber for german-bash.org package quotesite::GermanBashGrabber; use quotesite::GrabberBase; @ISA = qw(quotesite::GrabberBase); use LWP::UserAgent; use HTML::TokeParser; use Data::Dumper; use strict; sub new { my $class = shift; my $self = $class->SUPER::new(); $self->{'NAME'} = 'germanbash'; $self->{'PATTERNS'} = ['(http://(?:[-a-zA-Z0-9_.]+\.)*german-bash\.org/(\d+))', '(http://(?:[-a-zA-Z0-9_.]+\.)*german-bash\.org/action/show/id/(\d+))']; bless($self, $class); $self->_prepare_parameters(); return $self; } sub _parse { my $self = shift; my $url = shift; my $pattern = shift; my $content; my $metadata = {}; my $p; my $t; my $ua = LWP::UserAgent->new('agent' => 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)'); $url =~ m|$pattern|; $url = $1; $metadata->{'URL'} = $url; $metadata->{'ID'} = $2; $metadata->{'TYPE'} = 'quote'; $metadata->{'SOURCE'} = $self->{'NAME'}; $metadata->{'CONTENT'} = undef; # Get the HTML file containing the quote $content = $ua->get(sprintf('http://german-bash.org/%s', $2)); unless($content->is_success) { $self->error('Could not download quote'); return undef; } $content = $content->decoded_content(); $p = HTML::TokeParser->new(\$content); while ($t = $p->get_tag('div')) { if (exists($t->[1]->{'class'}) && ($t->[1]->{'class'} eq 'zitat')) { $metadata->{'CONTENT'} = $p->get_text('/div'); $metadata->{'CONTENT'} =~ s/^\s*//mg; last; } } unless(defined($metadata->{'CONTENT'})) { $self->error('Could not extract quote content'); return undef; } return $metadata; } 1;