html/myimdb.pl


   1 #######################################################################
   2 # myimdb.pl
   3 #
   4 # Author: Tim Van Wassenhove <timvw@users.sourceforge.net>
   5 #
   6 # Redistribution and use in source and binary forms, with or without
   7 # modification, are permitted provided that the following conditions
   8 # are met:
   9 # 1. Redistributions of source code must retain the above copyright
  10 #    notice, this list of conditions and the following disclaimer.
  11 # 2. Redistributions in binary form must reproduce the above copyright
  12 #    notice, this list of conditions and the following disclaimer in the
  13 #    documentation and/or other materials provided with the distribution.
  14 # 3. The name of the author may not be used to endorse or promote products
  15 #    derived from this software without specific prior written permission.
  16 #
  17 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  18 # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  19 # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  20 # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  21 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  22 # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  24 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26 # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27 #
  28 #######################################################################
  29 
  30 use strict;
  31 use Irssi;
  32 use LWP::UserAgent;
  33 use vars qw($VERSION %IRSSI);
  34 
  35 $VERSION = '1.01';
  36 %IRSSI = (
  37 	authors      => 'Tim Van Wassenhove',
  38 	contact     => 'timvw@users.sourceforge.net',
  39 	name        => 'myimdb',
  40   description => 'Query imdb',
  41 	license     => 'BSD',
  42 	url         => 'http://home.mysth.be/~timvw',
  43 	changed     => '13-03-04 01:43',
  44 );
  45 
  46 # Perform the query and return the results
  47 sub imdb_query {
  48 
  49 	my $query = shift;
  50 
  51 	my $ua = LWP::UserAgent->new;
  52 	$ua->agent("irssi/0.1 ");
  53 
  54   # Request the page
  55 	my $request = HTTP::Request->new(GET => "http://us.imdb.com/Title?=$query");
  56 	my $result = $ua->request($request);
  57 	my $content = $result->content;
  58 	
  59 	# Parse the returned page
  60 	my @lines = split("<LI>",$content);
  61 	my @results = ();
  62 	push(@results,"Results for $query on imdb:");
  63 	my $counter = 1;
  64 	foreach(@lines) {
  65 		if ($counter < 4 && $_ =~ /^\<A HREF=\"\/title\/tt(\d+)\/\"\>(.*?)\<\/A\>\<\/LI\>$/) {
  66 			push(@results,"http://us.imdb.com/title/tt$1 $2");
  67 			++$counter;
  68 		}
  69 	}
  70 	push(@results,"--");
  71 
  72 	return @results;
  73 }
  74 
  75 # Output the results
  76 sub results_write {
  77   my ($server,$target,@lines) = @_;
  78   foreach(@lines) {
  79 		$server->command("MSG $target $_");
  80 	}
  81 }
  82 
  83 # Handle what others say in public
  84 sub message_public {
  85 	my ($server,$msg,$nick,$address,$target) = @_;
  86 	if ($msg =~ /^\!imdb\s+(.+)$/) {
  87 		my @lines = imdb_query($1);
  88 		results_write($server,$target,@lines);
  89 	}
  90 }
  91 
  92 # Handle what we say in public
  93 sub message_own_public {
  94 	my ($server,$msg,$target) = @_;
  95 	message_public($server,$msg,$server->{nick},0,$target);
  96 }
  97 
  98 # Handle what others say in private
  99 sub message_private {
 100 	my ($server,$msg,$nick,$address) = @_;
 101 	message_public($server,$msg,$nick,$address,$nick);
 102 }
 103 
 104 # Handle what we say in private
 105 sub message_own_private {
 106 	my ($server,$msg,$target,$otarget) = @_;
 107 	message_public($server,$msg,$server->{nick},0,$target);
 108 }
 109 
 110 # Connect the signals with the functions
 111 Irssi::signal_add('message public','message_public');
 112 Irssi::signal_add('message own_public','message_own_public');
 113 Irssi::signal_add('message private','message_private');
 114 Irssi::signal_add('message own_private','message_own_private');