html/amaroknp.pl
1 #!/usr/bin/perl
2
3 # amaroknp.pl (0.1)
4
5 # This is a simple script for irssi which (attempts to) show the current song played
6 # by amaroK in the current channel or query window. It should output something like
7 # this: <@mynick> np: Artist - Song (1:34 / 4:05). This script has been tested to work
8 # with amaroK 0.9, KDE 3.2.1 and irssi 0.8.9 (on Linux). Might not work with older
9 # versions of amaroK.
10
11 # !! The only thing you might want to change is $dcopbin (line 45) !!
12 # !! and the format of $output (line 102) !!
13
14 # TODO
15 # - Cleaning up this mess :)
16 # - Setting options
17 # - Simple controls (play, pause, next, previous..)
18
19 use vars qw($VERSION %IRSSI);
20 use Irssi;
21 use strict;
22 $VERSION = '0.10';
23 %IRSSI = (
24 authors => 'Tuukka Lukkala',
25 contact => 'ragdim at mbnet dot fi',
26 name => 'amaroknp',
27 description => 'Shows the song playing in amaroK in the active window (channel or query).',
28 license => 'GPL',
29 url => 'http://koti.mbnet.fi/ragdim/amaroknp/',
30 changed => 'Tue Mar 30 23:20 EET 2004',
31 );
32
33 # !! Adjust this to the full path of dcop if it's not in your PATH. eg. /opt/kde/bin/dcop !!
34 my $dcopbin = "dcop";
35
36 # Let's check if we have dcop..
37 if (!`$dcopbin`) {
38 die "Couldn't find dcop executable.. Make sure dcop is in your PATH or edit dcoppath in the script";
39 }
40
41 sub cmd_amaroknp_help {
42
43 print "";
44 print "amaroknp script v0.1 for irssi";
45 print "To announce the current song, type: /amarok";
46 print "Requires KDE and amaroK";
47 print "";
48
49 }
50
51 sub cmd_amaroknp {
52
53 my ($data, $server, $witem) = @_;
54 my $playing; # Whether amaroK is playing something or not
55 my $song; # The song currently playing
56 my $timenow; # Current position in the song
57 my $timetotal; # Total length of teh song
58 my $minutes; # Current time converted to full minutes
59 my $seconds; # Current time converted to seconds
60 my $minutestotal; # Total amount of minutes
61 my $secondstotal; # Total amount of seconds
62 my $output; # The format in which all this is going -> channel
63 my $amaroktest; # To see if amarok is running or not
64 my $amarokNOTrunning; # Don't ask me.. :z
65
66 # ..and if amaroK is running (fix)
67 $amaroktest = `$dcopbin amarok 2> /dev/null`;
68 chomp($amaroktest);
69 if ($amaroktest =~ s/^$/No such application: 'amarok'/) {
70 print "amaroK isn't running?";
71 $amarokNOTrunning="nope";
72 }
73 # if amaroK is running, let's get teh infos!
74 if (!$amarokNOTrunning) {
75 $playing = `$dcopbin amarok default isPlaying`;
76 chomp($playing);
77 if ($playing eq 'false') {
78 print "amaroK isn't playing anything =I";
79 }
80
81 else {
82 # Get some infos
83 $song = `$dcopbin amarok default nowPlaying`;
84 chomp($song);
85 $timenow = `$dcopbin amarok default trackCurrentTime`;
86 # Converting times to a more readable format
87 $minutes = ($timenow/60)%60;
88 $seconds = $timenow%60;
89 # Add the leading zero
90 if ($seconds < 10) {
91 $seconds = "0" . $seconds;
92 }
93 $timetotal = `$dcopbin amarok default trackTotalTime`;
94 $timetotal = $timetotal/1000;
95 $minutestotal = ($timetotal/60)%60;
96 $secondstotal = $timetotal%60;
97 # Here too
98 if ($secondstotal < 10) {
99 $secondstotal = "0" . $secondstotal;
100 }
101 # The way it's gonna show up when we do /amarok
102 $output = "np: $song ($minutes:$seconds / $minutestotal:$secondstotal)";
103 }
104
105 if ($output) {
106 if ($witem && ($witem->{type} eq "CHANNEL" || $witem->{type} eq "QUERY")) {
107 $witem->command("MSG ".$witem->{name}." $output");
108 }
109 else {
110 Irssi::print("This is not a channel/query window :b");
111 }
112 }
113 }
114 }
115 Irssi::command_bind('amarok', 'cmd_amaroknp');
116 Irssi::command_bind('amarokhelp', 'cmd_amaroknp_help');