From e96845932973fdb6e93fd4891a0805fcdc013ede Mon Sep 17 00:00:00 2001 From: Ralf Ertzinger Date: Sat, 27 Feb 2010 15:40:11 +0100 Subject: [PATCH] Add grabber for totalbash.org --- quotesite/TotalBashGrabber.pm | 72 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 quotesite/TotalBashGrabber.pm diff --git a/quotesite/TotalBashGrabber.pm b/quotesite/TotalBashGrabber.pm new file mode 100644 index 0000000..2c15eb8 --- /dev/null +++ b/quotesite/TotalBashGrabber.pm @@ -0,0 +1,72 @@ +# (c) 2010 by Ralf Ertzinger +# licensed under GNU GPL v2 +# +# Grabber for totalbash.org + +package TotalBashGrabber; + +use GrabberBase; +@ISA = qw(GrabberBase); + +use LWP::Simple qw(!get); +use HTML::TokeParser; +use Data::Dumper; +use Encode; + +use strict; + +sub new { + my $class = shift; + my $self = $class->SUPER::new(); + + $self->{'NAME'} = 'TotalBash'; + $self->{'PATTERNS'} = ['(http://(?:[-a-zA-Z0-9_.]+\.)*totalbash\.org/(\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; + + $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 + unless(defined($content = LWP::Simple::get(sprintf('http://www.totalbash.org/%s', $2)))) { + $self->error('Could not download quote'); + return undef; + } + + $p = HTML::TokeParser->new(\$content); + + while ($t = $p->get_tag('div')) { + if (exists($t->[1]->{'class'}) && ($t->[1]->{'class'} eq 'beitrag_p')) { + $metadata->{'CONTENT'} = $p->get_text('/div'); + $metadata->{'CONTENT'} =~ s/^\s*//mg; + } + } + + unless(defined($metadata->{'CONTENT'})) { + $self->error('Could not extract quote content'); + return undef; + } + + return $metadata; +} + +1; -- 1.8.3.1