# rb_nowplaying.py - Print now playing status and whatnot # # Copyright (C) 2010 Elizabeth Jennifer Myers. All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are # met: # # 1) Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # # 2) Redistributions in binary form must reproduce the above # copyright notice, this list of conditions and the following # disclaimer in the documentation and/or other materials provided # with the distribution. # # 3) Neither the name of the author nor the name of the contributors # may be used to endorse or promote products derived from this # software without specific prior written permission. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. np_help_text = "/np - Song currently playing" prev_help_text = "/prev - Play previous song" next_help_text = "/next - Play next song" toggle_help_text = "/toggle - Toggle play/pause" __module_name__ = "rb_control" __module_version__ = "0.1" __module_description__ = "Control rhythmbox using xchat! :P" import xchat import dbus from os import path def get_dbus_obj(): try: bus = dbus.SessionBus() except: xchat.prnt("dbus is shitting itself or something.") return (None, None) try: playerobj = bus.get_object("org.gnome.Rhythmbox", "/org/gnome/Rhythmbox/Player") shellobj = bus.get_object("org.gnome.Rhythmbox", "/org/gnome/Rhythmbox/Shell") except: xchat.prnt("I'm not very convinced rhythmbox is even running, dude...") return (None, None) player = dbus.Interface(playerobj, "org.gnome.Rhythmbox.Player") shell = dbus.Interface(shellobj, "org.gnome.Rhythmbox.Shell") return (player, shell) def convert_time(seconds): hours = seconds / 3600 seconds -= 3600 * hours minutes = seconds / 60 seconds -= 60 * minutes if hours == 0: return "%02d:%02d" % (minutes, seconds) else: return "%02d:%02d" % (hours, minutes, seconds) def rb_nowplaying(word, word_eol, userdata): # Obtain dbus crud player, shell = get_dbus_obj() if player == None or shell == None: return xchat.EAT_NONE # Ascertain the current song playing via dbus try: nowplaying = shell.getSongProperties(player.getPlayingUri()) except: xchat.command("say I'm not listening to anything.") return xchat.EAT_NONE # Get the various attributes and insert filler where appropriate artist = nowplaying['artist'] if artist == '': artist = "Unknown artist" title = nowplaying['title'] if title == '': title = "Unknown song" album = nowplaying['album'] if album == '': album = "Unknown album" duration = nowplaying['duration'] if duration != 0: duration = convert_time(duration) else: duration = "?:??" genre = nowplaying['genre'] if genre == '': genre = "Unknown" bitrate = nowplaying['bitrate'] if bitrate == 0: bitrate = "Unknown" else: bitrate = str(bitrate) + "k" # Try to obtain the format specifier try: f = open(path.expanduser('~') + "/.xchat2/rb_format.txt", 'r') npformat = f.read().rstrip("\r\n") f.close() except: npformat = "me np: {artist} - {title} (album: {album}) ({duration}) (genre: {genre}) (bitrate: {bitrate})" npformat = npformat.format( artist = artist, title = title, album = album, duration = duration, genre = genre, bitrate = bitrate) # Bang. xchat.command(npformat) def rb_next(word, word_eol, userdata): # Obtain dbus crud player, shell = get_dbus_obj() if player == None or shell == None: return xchat.EAT_NONE try: player.next() except: xchat.prnt("There was a problem communicating with rhythmbox (is rhythmbox running?)") def rb_prev(word, word_eol, userdata): # Obtain dbus crud player, shell = get_dbus_obj() if player == None or shell == None: return xchat.EAT_NONE try: player.previous() except: xchat.prnt("There was a problem communicating with rhythmbox (is rhythmbox running?)") def rb_toggle(word, word_eol, userdata): # Obtain dbus crud player, shell = get_dbus_obj() if player == None or shell == None: return xchat.EAT_NONE try: if player.getPlaying(): self.player.playPause(False) else: self.player.playPause(True) except: xchat.prnt("There was a problem communicating with rhythmbox (is rhythmbox running?)") xchat.hook_command("np", rb_nowplaying, help=np_help_text) xchat.hook_command("next", rb_next, help=next_help_text) xchat.hook_command("prev", rb_prev, help=prev_help_text) xchat.hook_command("toggle", rb_toggle, help=toggle_help_text) xchat.prnt("Script loaded.")