QDB: |-
sub {
my( $kernel, $heap, $who, $what, $src, $dest, $replypath ) = @_;
-
+
unless( $what =~ /^([0-9]+)|(random)$/ ) {
$kernel->post( $src, $replypath, "Quote # should be a postive integer.", $dest );
return;
}
use LWP::UserAgent;
- my $ua = LWP::UserAgent->new;
+ my $ua = LWP::UserAgent->new( agent => "Mozilla/5.0 (Ubuntu; X11; Linux x86_64; rv:8.0) Gecko/20100101 Firefox/8.0" );
$ua->timeout(5);
my $req = HTTP::Request->new( GET => 'http://qdb.us/?'.$what );
my $res = $ua->request( $req );
my @quotes;
- if( $res->is_success ) {
+ if( $res->is_success or $res->code == "404" ) {
my $content = $res->content;
$content =~ s/[\n\r]//g;
-
- if( $content =~ s/<table[^>]*?class="quote">(.*?)<\/table>//i ) {
- my $table = $1;
- my( $num, $body );
- while( $table =~ s/<tr[^>]*>(.*?)<\/tr>//i ) {
- my $text = $1;
- $text =~ s/<br \/>/\n/gi;
- $text =~ s/<[^>]*>//gi;
- $text =~ s/</</gi;
- $text =~ s/>/>/gi;
- $text =~ s/&/&/gi;
- $text =~ s/"/"/gi;
- $text =~ s/ / /gi;
- if( $text =~ /^(#[0-9]+)/ ) {
- $num = $1;
- } elsif( $text eq "-+" ) {
- if( defined $num && defined $body ) {
- push @quotes, [$num, $body];
- }
- undef $num;
- undef $body;
- } else {
- $body = $text;
- }
+
+ while( $content =~ s/<span[^>]*?class=qt id=qt(\d+)>(.*?)<\/span>//i ) {
+ my $num = $1;
+ my $text = $2;
+ my $body;
+ $text =~ s/<br \/>/\n/gi;
+ $text =~ s/<[^>]*>//gi;
+ $text =~ s/</</gi;
+ $text =~ s/>/>/gi;
+ $text =~ s/&/&/gi;
+ $text =~ s/"/"/gi;
+ $text =~ s/ / /gi;
+
+ $body = $text;
+ if( defined $num && defined $body ) {
+ push @quotes, [$num, $body];
}
+ undef $num;
+ undef $body;
+ }
+ if( $#quotes > -1 ) {
my $quote = $quotes[ int(rand($#quotes+1)) ];
$kernel->post( $src, $replypath, $quote->[0].":\n".$quote->[1], $dest );
} else {