html/imdb.pl


   1 use Irssi;
   2 use LWP::UserAgent;
   3 use strict;
   4 use vars qw($VERSION %IRSSI $cache);
   5 
   6 $VERSION = '1.01';
   7 %IRSSI = (
   8     authors 	=> 'Eric Jansen',
   9     contact 	=> 'chaos@sorcery.net',
  10     name 	=> 'imdb',
  11     description => 'Automatically lookup IMDB-numbers in nicknames',
  12     license 	=> 'GPL',
  13     modules	=> 'LWP::UserAgent',
  14     url		=> 'http://xyrion.org/irssi/',
  15     changed 	=> 'Sat Mar  1 12:39:49 CET 2003'
  16 );
  17 
  18 my $ua = new LWP::UserAgent;
  19 $ua->agent('Irssi; ' . $ua->agent);
  20 
  21 # Set the timeout to one second, so it won't freeze the client too long on laggy connections
  22 $ua->timeout(1);
  23 
  24 sub event_nickchange {
  25 
  26     my ($channel, $nick, $old_nick) = @_;
  27 
  28     # Lookup any 7-digit number in someone elses nick
  29     if($nick->{'nick'} ne $channel->{'ownnick'}->{'nick'} && $nick->{'nick'} =~ /\D(\d{7})(?:\D|$)/) {
  30 
  31 	my $id = $1;
  32 
  33 	# See if we know the title already
  34 	if(defined $cache->{$id}) {
  35 
  36 	    # Print it
  37 	    $channel->printformat(MSGLEVEL_CRAP, 'imdb_lookup', $old_nick, $cache->{$id}->{'title'}, $cache->{$id}->{'year'});
  38 	}
  39 
  40 	# Otherwise, contact IMDB
  41 	else {
  42 
  43 	    # Fetch the movie detail page
  44     	    my $req = new HTTP::Request(GET => "http://us.imdb.com/Title?$id");
  45 	    my $res = $ua->request($req);
  46 
  47 	    # Get the title and year from the fetched page
  48     	    if($res->is_success && $res->content =~ /<title>(.+?) \((\d+)\)<\/title>/) {
  49 
  50 		my ($title, $year) = ($1, $2);
  51 
  52 		# Decode special characters in the title
  53 		$title =~ s/&#(\d+);/pack('U*', $1)/eg;
  54 
  55 		# Print it
  56 		$channel->printformat(MSGLEVEL_CRAP, 'imdb_lookup', $old_nick, $title, $year);
  57 
  58 		# And cache it
  59 		$cache->{$id} = {
  60 		    'title'	=> $title,
  61 		    'year'	=> $year
  62 		};
  63 	    }
  64 	}
  65     }
  66 }
  67 
  68 Irssi::theme_register([
  69     'imdb_lookup', '{nick $0} is watching {hilight $1} ($2)'
  70 ]);
  71 Irssi::signal_add('nicklist changed', 'event_nickchange');