html/orphamp.pl


   1 #!/usr/bin/perl
   2 
   3 
   4 # You can freely redistribute this script, but don't change the IRSSI string
   5 # ------------------------------------------------------------------------------
   6 # now playing script for irssi and Orpheus mp3 player (recommended 1.4 or higher)
   7 # lsof needed critically! (with correct permissions)
   8 # mp3info needed, not cricital, but recommended (http://ibiblio.org/mp3info/)
   9 # script painfully made by Wohmatak :)
  10 # ------------------------------------------------------------------------------
  11 
  12 
  13 use Irssi;
  14 use vars qw($VERSION %IRSSI);  
  15 
  16 $VERSION = '0.9';
  17 
  18 %IRSSI = (
  19         authors         => 'Wohmatak',
  20         contact         => 'wohmatak@iglu.cz',
  21         name            => 'orphamp',
  22         description     => 'Displays the song played by orpheus',
  23         license         => 'GPL',
  24         url             => 'http://irssi.org/',
  25         changed         => 'Wed',
  26         command         => '/np',
  27 	
  28 ); 
  29 
  30 Irssi::settings_add_str("misc", "np_lang", "en");
  31 Irssi::settings_add_int("misc", "show_npinfo", 1); 
  32 
  33 sub info {
  34 ### onload message
  35 print "--- Wohmatak's Orpheus now playing script loaded! ---\nTo show now playing song, use /np command";
  36 print "You need lsof and mp3info in order to use orphamp script.";
  37 ### feedback
  38 print "Feedback appreciated at wohmatak <at> iglu.cz, ICQ 70713105 or on IRCnet...";
  39 ### should I show info?
  40 print "-----------------------------------------------------";
  41 print "If you don't want to see this welcome message anymore, check show_npinfo irssi setting";
  42 print "If you want now playing sentence in czech, check np_land irssi setting (currently en/cz supported)";
  43 print "-----------------------------------------------------";
  44 }
  45 
  46 sub void {
  47 
  48 ### check, whether lsof works
  49 if (!`lsof -Fc -b -S 2`) { die "lsof command hasn't been found on your computer! Please check whether it is installed & has correct permissions... Orphamp deactivated";}
  50 ### lsof command
  51 $raw = `lsof -S 2 -n -P -b | grep mpg123 | grep -i mp3 | grep REG | tail -n 1`;
  52 ### split after /
  53 @split = split(/\//,$raw);
  54 ### count the number of splits
  55 $pocet = $#split;
  56 ### filename into one variable & newline department
  57 $filename = "";
  58 for ($i=1; $i<=$pocet; ++$i) {
  59 $filename .= "/";
  60 $filename .= @split[$i];
  61 }
  62 chomp($filename);
  63 
  64 ### check, whether mp3info is installed
  65 if (`mp3info` && $filename) {
  66 
  67     ## mp3info command, std_err to /dev/null 
  68     ## (we don't want those ugly error messages in irssi window, do we?:)
  69     $artist = `mp3info -p %a "$filename" 2> /dev/null`;
  70     $song = `mp3info -p %t "$filename" 2> /dev/null`;
  71     $album = `mp3info -p %l "$filename" 2> /dev/null`;
  72     if (!$album) { $album = "unknown album";}
  73     $year = `mp3info -p %y "$filename" 2> /dev/null`;
  74     if (!$year) { $year = "unknown year";}
  75 
  76 
  77     ## if there's no artist and song, display info from orpheus infopipe file (orpheus 1.4 needed)
  78     if (!$artist && !$song) 
  79     {
  80 	    $nazev = `cat ~/.orpheus/currently_playing`;
  81 	    $message = "prehrava ".$nazev.""; 
  82     }
  83     
  84     else 
  85     {
  86 	if (Irssi::settings_get_str("np_lang")  eq"en") 
  87 	{
  88 		     $message = "listens to ".$song." by ".$artist." ([".$year."] ".$album.")";
  89 	}
  90 	elsif (Irssi::settings_get_str("np_lang")  eq "cz") 
  91 	{
  92 		$message = "posloucha ".$song." od ".$artist." ([".$year."] ".$album.")";
  93 	}
  94     }
  95 
  96 
  97 }
  98 
  99 
 100 ### mp3info is not present (or we're playing a CD track)
 101 else {
 102     if ($filename) 
 103     {
 104 		 print "mp3info is not installed! please get it if you want to use orphamp script (http://ibiblio.org/mp3info/)"; 
 105     }
 106 
 107     $nazev = `cat ~/.orpheus/currently_playing`;
 108     	if (Irssi::settings_get_str("np_lang") eq "en") 
 109 	{
 110 		     $message = "listens to ".$nazev.""; 
 111 	}
 112 	elsif (Irssi::settings_get_str("np_lang")  eq "cz") 
 113 	{
 114 		$message = "posloucha ".$nazev.""; 
 115 	}
 116 }
 117 
 118 ### echo the message to channel
 119 my ($data, $server, $witem) = @_;
 120 if ($witem && ($witem->{type} eq "CHANNEL" || $witem->{type} eq "QUERY"))
 121     { $witem->command("/me $message"); } 
 122     else { print "* ".$message;}
 123 }
 124 
 125 if (Irssi::settings_get_int("show_npinfo")) {
 126 		info();
 127 		}
 128 
 129 Irssi::command_bind('np','void');
 130 Irssi::command_bind('npinfo','info');