From 5ac855980b32d2995ad48b7a27cc28630306bc15 Mon Sep 17 00:00:00 2001 From: Ralf Ertzinger Date: Fri, 24 Oct 2008 18:25:18 +0200 Subject: [PATCH] - Add iBashGrabber --- quotesite.pl | 10 ++++--- quotesite/Base.pm | 19 ++++++++++++- quotesite/iBashGrabber.pm | 72 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 96 insertions(+), 5 deletions(-) create mode 100644 quotesite/iBashGrabber.pm diff --git a/quotesite.pl b/quotesite.pl index cfbc3aa..a6414a9 100644 --- a/quotesite.pl +++ b/quotesite.pl @@ -4,7 +4,7 @@ # licensed under GNU GPL v2 use strict; -use Irssi 20020324 qw (command_bind command_runsub signal_add_first signal_add_last); +use Irssi 20020324 qw (command_bind command_runsub signal_add_first signal_add_last window_find_refnum); use vars qw($VERSION %IRSSI); use XML::Simple; use Data::Dumper; @@ -43,7 +43,7 @@ sub write_irssi { $text[0] = 'quotesite: ' . $text[0]; - if (defined $witem) { + if (defined($witem) && ref($witem)) { $witem->print(sprintf(shift(@text), @text), MSGLEVEL_CLIENTCRAP); } else { Irssi::print(sprintf(shift(@text), @text)); @@ -83,7 +83,9 @@ sub check_for_link { # Offer the message to all Grabbers in turn foreach $g (@grabbers) { + # $g->pushio(sub{ write_irssi($witem, @_); }); ($m, $p) = $g->get($message); + # $g->popio(); while (defined($m)) { write_irssi($witem, '%%R>>> %%Y%s%%N %%G%s', $m->{'SOURCE'}, $m->{'ID'}); @@ -346,13 +348,13 @@ sub cmdhandler { foreach (@grabbers) { $_->setdebug(1); } - write_irssi(undef, 'Enabled debugging'); + write_irssi($item, 'Enabled debugging'); } elsif ($params[0] eq 'nodebug') { $debug = 0; foreach (@grabbers) { $_->setdebug(0); } - write_irssi(undef, 'Disabled debugging'); + write_irssi($item, 'Disabled debugging'); } } diff --git a/quotesite/Base.pm b/quotesite/Base.pm index d8a4e98..4445389 100644 --- a/quotesite/Base.pm +++ b/quotesite/Base.pm @@ -8,7 +8,7 @@ use Data::Dumper; sub new { my $class = shift; - my $self = {'_DEBUG' => 0, '_OUT' => sub {print shift}}; + my $self = {'_DEBUG' => 0, '_OUT' => sub {print shift}, '_OUTSTACK' => []}; bless($self, $class); @@ -91,6 +91,23 @@ sub setio { $self->{'_OUT'} = $io; } +sub pushio { + my $self = shift; + my $io = shift; + + push(@{$self->{'_OUTSTACK'}}, $self->{'_OUT'}); + $self->setio($io); +} + +sub popio { + my $self = shift; + my $io = pop(@{$self->{'_OUTSTACK'}}); + + if (defined($io)) { + $self->setio($io); + } +} + sub getconfstr { my $self = shift; my $s = 'Options for ' . $self->{'NAME'} . ":\n"; diff --git a/quotesite/iBashGrabber.pm b/quotesite/iBashGrabber.pm new file mode 100644 index 0000000..04bd07d --- /dev/null +++ b/quotesite/iBashGrabber.pm @@ -0,0 +1,72 @@ +# (c) 2007 by Ralf Ertzinger +# licensed under GNU GPL v2 +# +# Grabber for german-bash.org + +package iBashGrabber; + +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'} = 'ibash.de'; + $self->{'PATTERNS'} = ['(http://(?:[-a-zA-Z0-9_.]+\.)*ibash.de/zitat_(\d+)\.html)']; + + 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.ibash.de/zitat_%s.html', $2)))) { + $self->error('Could not download quote'); + return undef; + } + + $p = HTML::TokeParser->new(\$content); + + while ($t = $p->get_tag('td')) { + if (exists($t->[1]->{'class'}) && ($t->[1]->{'class'} eq 'quote')) { + $metadata->{'CONTENT'} = $p->get_text('/td'); + Encode::from_to($metadata->{'CONTENT'}, 'iso-8859-1', 'utf8'); + } + } + + unless(defined($metadata->{'CONTENT'})) { + $self->error('Could not extract quote content'); + return undef; + } + + return $metadata; +} + +1; -- 1.8.3.1