shlomi-fish-homepage / t2 / open-source / bits-and-bobs / nowplay-xchat.pl

#!/usr/bin/perl -w

use strict;

# use Xmms::Remote ();
use Net::DBus ();

IRC::register("Xmms Song Displayer", "1.0", "", "");
IRC::add_command_handler("nowplay", "display_now_playing_song");

# my $remote = Xmms::Remote->new();

my $bus = Net::DBus->find;

# Get a handle to the HAL service

sub get_amarok_dbus_title
{
    my $amarok = eval { $bus->get_service("org.kde.amarok") };
    if ($@ || (!$amarok))
    {
        return;
    }

    my $tracklist = $amarok->get_object("/TrackList", "org.freedesktop.MediaPlayer");
    if (!$tracklist)
    {
        return;
    }
    
    my $track_idx = $tracklist->GetCurrentTrack();
    if ($track_idx < 0)
    {
        return;
    }
    my $track = $tracklist->GetMetadata($track_idx);
    return
        join(" - ", 
            grep { lc($_) ne "unknown" }
            @{$track}{qw(artist title)}
        );
}

sub get_amarok_title
{
    my $t = `dcop amarok player nowPlaying`;
    chomp($t);
    return $t;
}

sub get_kaffeine_title
{
    my $artist = `dcop kaffeine KaffeineIface artist`;
    chomp($artist);
    my $t = `dcop kaffeine KaffeineIface title`;
    chomp($t);
    
    if ($artist || $t)
    {
        return "$artist - $t";
    }
    else
    {
        return;
    }
}

=begin foo

sub get_xmms_title
{
    # Check for amaroK
    my $pos = $remote->get_playlist_pos();
    return $remote->get_playlist_title($pos);
}

=end foo

=cut

sub display_now_playing_song
{
    my $title = get_amarok_dbus_title() || get_kaffeine_title() || get_amarok_title();
    IRC::command("/me is listening to $title");
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.