Irssi – Wikipedia script

Simple script based on Google.pl to query wikipedia. Print to channel as public message or keep the results private (default). Specify max number of results or which result out of many you want. E.g If somebody needs info about the java programming language. You could first do a private search like this

/wiki -5 java

You will get 5 articles back from wikipedia, and you will see that the second article is named “Java (programming language)” and that is probably the one you want. Then you could do

/wiki -p #2 java

and the second result will be sent as a public message to the active channel.

wiki.pl (4.4 kB)

# - Wiki.pl

# - You have to modify this line to the path
# - of your LWP-dir

use lib '/System/Library/Perl/Extras/5.8.8/LWP';

use Irssi;
use LWP::UserAgent;
use strict;
use vars qw($VERSION %IRSSI);

$VERSION = '0.2.1';
%IRSSI = (
    authors     => 'Hersson',
    homepage    => 'www.hersson.net',
    name        => 'Wiki',
    description => 'This script queries wikipedia.org and returns a short version of the result with a link to the article',
    license     => 'Public Domain',
);

## Usage:
## /wiki [-p, prints to current window] [-<number>, number of searchresults returned | #<number> which search result to return] search-criteria1 search-criteria2 ...
##
## The Wiki.pl script is based on Google.pl
##
## History:
## - Sun Mar 1 2009
##      Version 0.1 - Initial release
##      Version 0.2 - Added link to articles
## - Mon Mar 2 2009
##      Version 0.2.1 - Fixed problem when article url
##                      contained %
## -------------------------------



sub cmd_wiki {

    my ($data, $server, $witem) = @_;

#   #-------------------------------------------------
    my $nr_results = 1; # Default number of results returned
    my $spesific_res = 0; # Default specific result is disabled
    my $prefix = "/wiki"; # Message printed before results
    my $url = "http://en.wikipedia.org";
    my $mode = "quiet"; # Default mode
#   #------------------------------------------------- 

#   # If user supplied nr_results, activate his setting
    if ( $data =~ /-(\d\s)/ ) { $nr_results = $1 };
    if ($data =~ /-10/) { $nr_results = 10 };
    $data =~ s/-\d+//g; # remove nr_results from $data

#   # If user supplied spesific result activate his setting
    if ( $data =~ /#(\d\s)/ ) { $spesific_res = $1-1 };
    if ( $data =~ /#10/) { $spesific_res = 9};
    $data =~ s/#\d+//g; # remove spesific_res from $data
   
#   # If user supplied spesific result make sure to get that many, but not more
    if ($spesific_res != 0) {$nr_results = $spesific_res + 1};

#   # Switch to public mode
#   # and return error msg if invalid window
    if ( $data =~ /-p/ ) {
        $mode = "public";
        if ( ! $witem ) {
          Irssi::active_win()->print("Must be run run in a valid window (CHANNEL|QUERY)");
          return;
        }
    }
    $data =~ s/-p//g; # remove -p from $data

#   # Get query as written by user
    my $written_query = $data;

#   # Format the query-string
    $data =~ s/\s/+/g;


    my $query = $data;


#   # Initialize LWP
    my $ua = new LWP::UserAgent;
    $ua->agent("AgentName/0.1 " . $ua->agent);

#   # Do the actual seach
        my $req = new HTTP::Request GET => $url . '/w/index.php?title=Special%3ASearch&ns0=1&search=' . $query . '&=MediaWiki+search&fulltext=Advanced+search';
        my $res = $ua->request($req);
        my $content = $res->content;

#   # Replace <br> with newlines
         $content =~ s/\<br\>/\n/g;

 
#   # Make an arry of lines
    my @lines = split("\n", $content);

#   # Array for the results
    my @results;
   
    my $nr_found = 0;
    foreach (@lines) {
#       # Get the interesting stuff
        if ($_ =~ m/\<div class\=\'searchresult\'\>.*?\<span class\=\'searchmatch\'\>/){
                       
#           # Get url to article
            my $article_url;
            if ($_ =~ m/"(.+?)"/) {
                $article_url = $url . $1;

#               #Check for % and change them to %%
                            $article_url =~ s/\%/%%/g;
            }

           
#           #Remove tags
            $_ =~ s/\<.+?\>//g;
           
#           #Remove wikipedia's info of a redirect link
            $_ =~ s/\(redirect.+?\){1,2}//;
           
#           #Substitue multiple spaces with ' - 'between title and main text
            $_ =~ s/\s{3,}/ - /;
           
#           #Cut the crap at the end
            $_ =~ s/\W{4,}$/... /;
           
            if ($nr_found < $nr_results) {
                if ($prefix ne ""){
                    $results[$nr_found] = $prefix . " " . $written_query . " >> " . $_ . $article_url;
                } else {
                     $results[$nr_found] = $_ . $article_url;
                }
                $nr_found++;
            }
           
        }
       
    }

#   # Prevent script from printing empty lines at the end
    if (@results < $nr_results) {$nr_results = @results;}

#   # Print result to current window if public-mode specified
#   # else display a private notice of returned results
    if ( $mode eq "public") {
        if ($spesific_res != 0 && $nr_results > $spesific_res) {
            $witem->command("/SAY $results[$spesific_res]");   
        } else {
            for (my $i=0; $i<$nr_results; $i++) {
                $witem->command("/SAY $results[$i]");
            }
        }
    }
    else {
        if ($spesific_res != 0 && $nr_results > $spesific_res) {
            Irssi::active_win()->print("$results[$spesific_res]");
        } else {
            for (my $i=0; $i<$nr_results; $i++) {
                Irssi::active_win()->print("$results[$i]");
            }
        }

    }

}

Irssi::command_bind('wiki', 'cmd_wiki');
  • Twitter
  • Facebook
  • del.icio.us
  • Digg
  • Google Bookmarks
  • StumbleUpon
  • FriendFeed

One thought on “Irssi – Wikipedia script”

  1. The script does not return any results… Do you have idea how to make it work?

    I always used it..

    It was a great script!

    Thanks and best regards!

Leave a Reply

Your email address will not be published. Required fields are marked *


three × 2 =

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>