Added a command to fetch the version of a Debian package, and fixed urban dictionary...
authorpdbogen <pdbogen@088b83a4-0077-4247-935c-42ec02c2848b>
Sat, 20 Aug 2011 20:10:21 +0000 (20:10 +0000)
committerpdbogen <pdbogen@088b83a4-0077-4247-935c-42ec02c2848b>
Sat, 20 Aug 2011 20:10:21 +0000 (20:10 +0000)
git-svn-id: https://www.cernu.us/~pdbogen/svn/destult2@119 088b83a4-0077-4247-935c-42ec02c2848b

cmdaccess.yaml
commands.yaml

index b47ac09..4e48725 100644 (file)
@@ -1,4 +1,5 @@
 ---
+ACCESS: 2
 ACCESSLIST: 1
 ADVERTISE: 2
 APPEND: 1
@@ -27,6 +28,7 @@ REPLACE: 1
 SAY: 1
 SETPASSWORD: 2
 SIEVE: 1
+TRAP: 2
 TWITTER: 0
 TWITTERCLEAR: 2
 UNIGNORE: 2
index 4819f72..828e1ef 100644 (file)
@@ -541,45 +541,25 @@ DICT: |-
     }
   
     if( $dict =~ m/^urban/i ) {
+      use JSON;
+      use LWP::Simple;
       use HTML::Entities;
-      print( "DICT: Urban Dictionary", "\n" );
-  
-      my $key = "84d324f8a5fcae301ec4aefcd258dff2";
-      my $search = WWW::Search->new('UrbanDictionary', key=>$key );
-      
-      if( $num > 0 ) {
-        $search->maximum_to_retrieve( $num + 1 );
-      } else {
-        $search->maximum_to_retrieve( 1 );
-      }
-      $search->timeout( 10 );
-      # SOAP::Lite, which is the Urban Dictionary backend, is smart enough to escape queries on its own.
-      $search->native_query( $what );
-      $search->seek_result( $num );
-      if( !$search->response->is_success ) {
-        print( "DICT: Error while searching: ".$search->response->as_string(), "\n" );
-        $kernel->post( $src, $replypath, "Error while searching: ".$search->response->as_string(), $dest );
-        return;
-      }
-      my $result;
-      if( !( $result = $search->next_result() ) ) {
-        print( "DICT: No result", "\n" );
-        if( $num == 0 ) {
-          $kernel->post( $src, $replypath, "No result for '$what'.", $dest );
+      $num += 1;
+      my $page = get( 'http://www.urbandictionary.com/iphone/search/define?term='.$what );
+      my $response = decode_json $page;
+      if( $num > $response->{ "total" } ) {
+        if( $num == 1 ) {
+          $kernel->post( $src, $replypath, "There is no definition for '".$what."'", $dest );
         } else {
-          $kernel->post( $src, $replypath, "No entry #".$num." for '$what'.", $dest );
+          $kernel->post( $src, $replypath, "There is no definition #".$num." for '".$what."'", $dest );
         }
-       return;
-      } else {
-       my $hits = $search->approximate_result_count();
-        print( "DICT: Backend reports $hits hit".($hits==1?"":"s"), "\n" );
-      }
-      my $response = $result->{ 'word' }.": ".$result->{ 'definition' };
-      $response = decode_entities( $response );
-      $response =~ s/\n/ /g;
-      print( "DICT: Fetched result\n" );
-      $heap->{ 'DICT_cache' }->{ 'URBAN' }->{ "$num.$what" } = $response;
-      $kernel->post( $src, $replypath, $response, $dest );
+        return;
+      }
+      my $result = $response->{ "list" }->[ $num-1 ]->{ "word" }.": ".$response->{ "list" }->[ $num-1 ]->{ "definition" };
+      $result = decode_entities( $result );
+      $result =~ s/\n/ /g;
+      $heap->{ 'DICT_cache' }->{ 'URBAN' }->{ "$num.$what" } = $result;
+      $kernel->post( $src, $replypath, $result, $dest );
     } else {
       use Net::Dict;
       print( "DICT: Establish connection.\n" );
@@ -2147,3 +2127,19 @@ IMDB: |-
       $kernel->post( $src, $replypath, "An error occured: ".$imdb->error(), $dest );
     }
   }
+
+VERSION: |-
+  sub {
+    my( $kernel, $heap, $who, $what, $src, $dest, $replypath ) = @_;
+    $what =~ s/(^\s+)|(\s+$)//g;
+    my @args = split(/ /, $what);
+    if( (scalar @args) != 2 ) {
+      $kernel->post( $src, $replypath, "VERSION displays the current version of a Debian package.", $dest );
+      $kernel->post( $src, $replypath, "Usage: VERSION <release name> <package name>", $dest );
+      return;
+    }
+    use LWP::Simple;
+    my $page = get( "http://packages.debian.org/".join( '/', @args ) );
+    $page =~ m/<h1>(Package: .*?)<\/h1>/s;
+    $kernel->post( $src, $replypath, $1, $dest );
+  }