#!/usr/bin/env python # -*- coding: utf-8 -*- ''' amarok2_np_dbus.py "NowPlaying" plugin for XChat by Jason "zcat" Farrell (farrellj@gmail.com), 2008 Distributed under the terms of the GNU General Public License, v2 or later ''' __module_name__ = "amarok2_np_dbus" __module_version__ = "0.3.2" __module_description__ = "Displays the current song now playing in amarok2 via dbus (RIP DCOP)" import xchat import dbus import os.path import urllib def amarok_is_ready(): ''' determine if dbus is ok, amarok is running, and set a dirty dbus proxy global ''' global amarokPlayer try: sessbus = dbus.SessionBus() #if "org.kde.amarok" in sessbus.list_names() amarokPlayer = sessbus.get_object('org.kde.amarok', '/Player') # connect proxy to the Player object return True except: return False def seconds_to_hms(s): ''' e.g.: convert 65 seconds to "01:05" ''' rstr = "" hours = s / (60*60) s = s % (60*60) minutes = s / 60 seconds = s % 60 if hours > 0: rstr += "%0d:%02d:%02d" % (hours, minutes, seconds) else: rstr += "%0d:%02d" % (minutes, seconds) return rstr def usage(): print '''/np [OPTION]... : default is to show channel your currently playing song me : print song only to your console, instead of to channel noalbum : suppress Album metadata noextra : suppress "(X:XX/X:XX) (rating: X/X)" extra info help : this help''' def now_playing(word, word_eol, userdata): shout = True; showalbum = True; showextra = True; if 'help' in word: usage() return xchat.EAT_ALL if 'me' in word: shout = False if 'noalbum' in word: showalbum = False if 'noextra' in word: showextra = False if amarok_is_ready(): #print "amarok2 is playing" meta = amarokPlayer.GetMetadata() # call GetMetadata method of amarok's dbus Player object if len(meta) == 0: print "Amarok2 is running, but no song is playing (no metadata available)" else: # ♪♬♫ Artist - Title - Album (1:27/3:42) (rating: 6/10) npstr = "" if not (meta['artist'] and meta['title']): # show a pretty filename if no meta npstr += "%s" % (str(urllib.unquote(os.path.basename(str(meta['location']))))) else: if meta['artist'] and not meta['title']: npstr += "%s" % (str(meta['artist'])) elif meta['title'] and not meta['artist']: npstr += "%s" % (str(meta['title'])) else: npstr += "%s - %s" % (str(meta['artist']), str(meta['title'])) if meta['album'] and showalbum: npstr += " - %s" % (str(meta['album'])) if showextra: npstr += " (%s/%s)" % (seconds_to_hms(amarokPlayer.PositionGet()/1000), seconds_to_hms(meta['time'])) if showextra: npstr += " (rating: %d/5)" % meta['rating'] if shout: xchat.command("me ♪♫ %s" % npstr) else: print "♪♫ %s" % npstr else: print "Amarok2 does not appear to be running." return xchat.EAT_ALL xchat.hook_command('np', now_playing) xchat.prnt('♪♫ ' + __module_name__ + ' version ' + __module_version__ + ' loaded. Use /np to display the song you\'re playing; /np help for HELP')