Skip to content

Instantly share code, notes, and snippets.

@e000
Created January 8, 2011 01:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save e000/770416 to your computer and use it in GitHub Desktop.
Save e000/770416 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import dbus
import xchat
__module_name__ = 'xchat-rhythmbox'
__module_author__ = 'e'
__module_version__ = '0.1'
print "Connecting to RhythmBox dbus service."
bus = dbus.SessionBus()
autoAnnounce = True
player = bus.get_object("org.gnome.Rhythmbox", "/org/gnome/Rhythmbox/Player")
shell = bus.get_object("org.gnome.Rhythmbox", "/org/gnome/Rhythmbox/Shell")
currentUri = player.getPlayingUri()
def utf8(value):
if isinstance(value, unicode):
return value.encode("utf-8")
assert isinstance(value, str)
return value
command = lambda c: xchat.command(utf8(c))
def nowPlaying(word, word_eol, n):
global currentUri
currentUri = player.getPlayingUri()
command("ME %s" % fmtSong(currentUri))
return xchat.EAT_ALL
def fmtSong(uri):
if uri:
song = shell.getSongProperties(uri)
out = "is listening to \x02%s\x02 - \x02%s\x02 [%s/%s] @ [%skbps]" % (
song['artist'], song['title'], sectots(player.getElapsed()), sectots(song['duration']), song['bitrate']
)
else:
out = "is not listening to anything at the moment."
return out
sectots = lambda secs: '%i:%02i:%02i' % (sec // 3600, sec // 60 % 60, sec % 60) if secs > 3600 else '%i:%02i' % (secs // 60 % 60, secs % 60)
def unload(_):
print "Unloaded [\x02%s\x02 v%s] by \x02%s\x02 successfully." % (__module_name__, __module_version__, __module_author__)
def poll(_):
global currentUri
uri = player.getPlayingUri()
if uri != currentUri:
currentUri = uri
if uri:
song = shell.getSongProperties(uri)
out = "is listening to \x02%s\x02 - \x02%s\x02 @ [%skbps]" % (
song['artist'], song['title'], song['bitrate']
)
command('ME %s' % out)
return 1
def toggleAnnounce(*a):
global autoAnnounce, pollTimer
if autoAnnounce:
if pollTimer:
xchat.unhook(pollTimer)
pollTimer = None
autoAnnounce = False
print "Auto updating songs disabled."
else:
if pollTimer:
xchat.unhook(pollTimer)
pollTimer = xchat.hook_timer(1000, poll)
autoAnnounce = True
print "Auto updating songs."
return xchat.EAT_ALL
xchat.hook_command("NP", nowPlaying, help="Tells what you're listening to to the channel")
xchat.hook_command("ANNOUNCE", toggleAnnounce, help="Toggles the Auto Announcer")
if autoAnnounce:
pollTimer = xchat.hook_timer(1000, poll)
print "Auto updating song status enabled."
else:
print "Not auto updating song status, type /ANNOUNCE to enable."
pollTimer = None
xchat.hook_unload(unload)
print "Loaded [\x02%s\x02 v%s] by \x02%s\x02 successfully." % (__module_name__, __module_version__, __module_author__)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment