How To - Perl Script for Xchat to Show song playing in GMusicBrowser

VastOne

This is a perl script created by VISDO member McLovin to run in Xchat to show what song is playing in your IRC channel

Save the following as /home/yourusername/.xchat2/gmplayer.pl


#!/usr/bin/perl

use strict;
use Net::DBus;

# Gm player XChat script
# (c) Copyright 2013 - McLovin <McLovinsblog@gmail.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

my $script_name        = "gmusic XChat";
my $script_version     = " 0.0.6";
my $script_description = "Displays the current Gmusic playing song in XChat.";
my $bus = Net::DBus->session;
#check for gmusicbrowser on Dbus
gm_live();

# message string
my $playing ="is currently listening to"; #replace with your own message

Xchat::register($script_name,$script_version,$script_description,"");

Xchat::print("Loaded \002".$script_name.$script_version."\003:");
Xchat::hook_command("gm_start", "gm_live");
Xchat::hook_command("gm_play", "gm_play");
Xchat::hook_command("gm_pause", "gm_pause");
Xchat::hook_command("gm_next", "gm_next");
Xchat::hook_command("gm_prev", "gm_prev");
Xchat::hook_command("gm_browse", "gm_browse");
Xchat::hook_command("gm_volup", "gm_volup");
Xchat::hook_command("gm_voldown", "gm_voldown");
Xchat::hook_command("gm_help", "gm_help");

#print "/me utilise $script_name $script_version \n";

sub gm_play{
   if (gm_run()){
      my $service = $bus->get_service('org.gmusicbrowser');
      my $object = $service->get_object('/org/gmusicbrowser', 'org.gmusicbrowser');
      my $info= $object->CurrentSong;
      # hash keys are : album, artist, disc, length, title, track
      # $info hash contains the keys value above. you can change below to display information you need. keep "me" (command will generate the slash).
      Xchat::command "me $playing $info->{artist} - $info->{title} - $info->{album} in GMusicBrowser";
   }
}

sub gm_next{
# Skip to the next track.
   if (gm_run()){
      my $service = $bus->get_service('org.gmusicbrowser');
      my $object = $service->get_object('/org/gmusicbrowser', 'org.gmusicbrowser');
      $object->RunCommand("NextSong");
      Xchat::print("Skipped to next track.");
      return 1;
   }
}

sub gm_pause{
# Play pause switch
   if (gm_run()){
      my $service = $bus->get_service('org.gmusicbrowser');
      my $object = $service->get_object('/org/gmusicbrowser', 'org.gmusicbrowser');
      $object->RunCommand("PlayPause");
      Xchat::print("play/pause track.");
      return 1;
   }
}

sub gm_prev{
# Rewind to the previous track.
   if (gm_run()){
      my $service = $bus->get_service('org.gmusicbrowser');
      my $object = $service->get_object('/org/gmusicbrowser', 'org.gmusicbrowser');
      $object->RunCommand("PrevSong");
      Xchat::print("rewinded to previous track.");
      return 1;
   }
}

sub gm_browse{
# Open a browser window.
   if (gm_run()){
      my $service = $bus->get_service('org.gmusicbrowser');
      my $object = $service->get_object('/org/gmusicbrowser', 'org.gmusicbrowser');
      $object->RunCommand("Browser");
      Xchat::print("opening Gmusic browser.");
      return 1;
   }
}

sub gm_help{
   # Display help screen.
   Xchat::print("\002\037".$script_name." Help:\037\002");
   Xchat::print("  * Script Version:    ".$script_version);
   Xchat::print(" \002Commands:\002");
   Xchat::print("  * /gm_start   - starts gmusic browser when it has been closed.");
   Xchat::print("  * /gm_play   - Display the current song playing to a channel.");
   Xchat::print("  * /gm_next   - Skip to the next track.");
   Xchat::print("  * /gm_prev   - Skip to the previous track.");
   Xchat::print("  * /gm_pause   - Pause/start playback.");
   Xchat::print("  * /gm_browse   - Open Gmusic UI window.");
   Xchat::print("  * /gm_help   - Display this help screen.");
   return 1;
}

sub gm_live{
my $localbus = Net::DBus->find;
my $localservice = $localbus->get_service("org.freedesktop.DBus");
my $localobject = $localservice->get_object("/org/freedesktop/DBus");
my $gtrouver=0;
  foreach (@{$localobject->ListNames}) {
    if (m/gmusicbrowser/){
      Xchat::print "found gmusic browser\n";
      $gtrouver=1;
      last;
    }
    }
 
}

sub gm_run{
my $localbus = Net::DBus->find;
my $localservice = $localbus->get_service("org.freedesktop.DBus");
my $localobject = $localservice->get_object("/org/freedesktop/DBus");
my $gtrouver=0;
  foreach (@{$localobject->ListNames}) {
    if (m/gmusicbrowser/){
      #Xchat::print "found gmusic browser\n";
      $gtrouver=1;
      last;
    }
    }
    unless ($gtrouver){
   Xchat::print "You need to restart gmusicbrowser use gm_start.\n";
   }
  return $gtrouver;
}


To run it in Xchat, make sure that GMusicBrowser is playing a song and enter this

/gm_play

Well Done McLovin!
VSIDO      VSIDO Change Blog    

    I dev VSIDO

jedi

Awesome job McLovin!!!  I love spamming xchat!!!  Thanks for this!!
Forum Netiquette

"No matter how smart you are you can never convince someone stupid that they are stupid."  Anonymous

McLovin

No Problem, Thanx for reposting it, I thought I had put it in the right spot, but then again, I could be very wrong on where I put it. I'm gonna start working on a new in python, as I think I may be able to get more fine grained control in Python, I'm most likely gonna use one I have for rhythmbox for reference, and I plan on adding length and position to the output.
Visit my DeviantArt page and My Blog