#!/usr/bin/perl

# -----------------------------------------------------------------------------
# audioscrobbler.pl - (c) Frank 'yogan' Blendinger (yogan@intoxicatedmind.net)
# -----------------------------------------------------------------------------
# 
# Gets your last played song from the audioscrobbler.com textfeed and prints it
# either to the current channel/query or privately to you. You need an account
# on audioscrobbler.com and an running plugin for you favorite media player.
# 
# A quick and dirty hack, done without any perl or irssi-scripting knowledge.
# Pure coincidence if it works. ;)
#
# -----------------------------------------------------------------------------
#
# USAGE:
# 	/anp [-p]
# 	  Prints your currently playing song to the active channel/query. If
# 	  the parameter -p is given, it will only be shown to yourself.
# 	
# 	Don't forget to /set audioscrobbler_nick <your-audioscrobbler-nick>
#
# TODO:
# 	- fix crashing when audioscrobbler.com not reachable (user-definable
# 	  timeout)
# 	- /anp <nick> - see what other users are playing
# 	- last5 command
#
# HISTORY:
# 	- 2004-10-08 - v0.1
# 	  Initial release
# 	  
# 	- 2004-10-13 - v0.2
# 	  Instead of two commands (/anp and /anppriv), /anp now has a -p
# 	  parameter to choose private display.
#
# 	- 2004-12-23 - v0.2.1
# 	  Small bugfix. Stupid audioscrobbler won't fill the first line
# 	  in the textfeed anymore. Now just using the second line. Please
# 	  note that this could still show some quite old song, if you haven't
# 	  submitted a new one yet. Only thing I could do about that is checking
# 	  the timestamp, but who give's a shit anyway?
#
# -----------------------------------------------------------------------------




use vars qw($VERSION %IRSSI);
$VERSION = '0.2.1';
%IRSSI = (
        authors         => 'Frank \'yogan\' Blendinger',
        contact         => 'fb\@intoxicatedmind.net',
        name            => 'audioscrobbler',
        description     => 'Displays your last played song, getting info from audioscrobbler.com',
        license         => 'GPL',
        url             => 'http://yogan.meinungsverstaerker.de/irssi/',
        changed         => '2004-12-23',
); 


use Irssi qw(
	command_bind
	settings_get_int settings_add_int
	settings_get_str settings_add_str
);
use POSIX;
use LWP::Simple;


sub playing {

	# Switch to private mode if -p parameter was given
	my ($data, $server, $witem) = @_;
	$mode = "public";
	if ($data =~ /-p/ ) { $mode = "private"; }

	# Load settings
	$asnick = settings_get_str('audioscrobbler_nick');
	#$timeout = settings_get_int('audioscrobbler_timeout');

	# Build the message to print
	#$message = `lynx -connect_timeout=$timeout -dump http://ws.audioscrobbler.com/txt/recent/$asnick`;
	my $url = 'http://ws.audioscrobbler.com/txt/recent/'.$asnick;
	my $message = get($url);
	
	if ($message =~ /The user $asnick does not exist/) {
		print "%R>>%n The user $asnick does not exist - did you set audioscrobbler_nick correctly?";
		return;
	}
	
	$message = `echo \"$message\" | head -2 | tail -1`;
	$message =~ s/\n//;
	if ($message eq "") { $message = "No song playing at the moment (or audioscrobbler.com is not reachable)."; }
	else { $message = "np: ".$message; }
	
	
	# Echo the message to channel/query or privately
	if ( $mode eq "private" ) {
		if ($witem && ($witem->{type} eq "CHANNEL" || $witem->{type} eq "QUERY"))
			{ $witem->command("/echo $message"); }
		else { print '%R>>%n '.$message; }
	} else {
		if ($witem && ($witem->{type} eq "CHANNEL" || $witem->{type} eq "QUERY")) { 
			if ($message eq "No song playing at the moment (or audioscrobbler.com is not reachable).") {
				$witem->command("/echo $message");
			} else { $witem->command("/me $message"); }
		} else { print '%R>>%n '.$message; }
	}

}


settings_add_str($IRSSI{name}, 'audioscrobbler_nick', 'your_audioscrobbler_nickname' );
command_bind('anp','playing');

print CLIENTCRAP '%R>>%n '.$IRSSI{name}.' '.$VERSION.' loaded';
if (settings_get_str('audioscrobbler_nick') eq "your_audioscrobbler_nickname") {
	print CLIENTCRAP '%R>>%n Set your nickname first: /set audioscrobbler_nick your_audioscrobbler_nickname';
}


