Fixed QDB.us support master
authorMatt Mullins <mmullins@mmlx.us>
Sun, 18 Dec 2011 01:12:18 +0000 (17:12 -0800)
committerMatt Mullins <mmullins@mmlx.us>
Sun, 18 Dec 2011 01:12:18 +0000 (17:12 -0800)
  * QDB.us was returning the iPhone random page rather than the requested quote
    without setting a URL

  * Changed page parsing to match actual results

commands.yaml

index 0b9bf10..017f0f1 100644 (file)
@@ -851,46 +851,42 @@ BASH: |-
 QDB: |-
   sub {
     my( $kernel, $heap, $who, $what, $src, $dest, $replypath ) = @_;
 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;
     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;
     $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;
       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/&lt;/</gi;
-          $text =~ s/&gt;/>/gi;
-          $text =~ s/&amp;/&/gi;
-          $text =~ s/&quot;/"/gi;
-          $text =~ s/&nbsp;/ /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/&lt;/</gi;
+        $text =~ s/&gt;/>/gi;
+        $text =~ s/&amp;/&/gi;
+        $text =~ s/&quot;/"/gi;
+        $text =~ s/&nbsp;/ /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 {
         my $quote = $quotes[ int(rand($#quotes+1)) ];
         $kernel->post( $src, $replypath, $quote->[0].":\n".$quote->[1], $dest );
       } else {