A now playing script for players supporting the mpris dbus interface.
Author: Johannes Nixdorf
— Version: 0.3
— License: BSD-2-Clause
For WeeChat ≥ 0.3.0, requires: python-dbus.
Tags: music, py2
Added: 2011-08-28
— Updated: 2011-09-17
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 | # -*- coding: utf-8 -*- ### # Copyright (c) 2011, Johannes Nixdorf <mixi@shadowice.org> # 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. # # 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. ### ### # ChangeLog: # 0.3: # * don't excpect every metadata key to be available # 0.2: # * fix an error when the player isn't running # * don't display the song if it playing is paused/stopped # * fix an error when the player is changed while the plugin is loaded # * make fewer dbus calls # 0.1: # * first version ### import dbus try: import weechat weechat.register( 'mpris_np', 'Johannes Nixdorf <mixi@shadowice.org>', '0.3', 'BSD', 'Print information on the currently played song', '', '' ) except ImportError: print 'This script must be called from inside weechat' class NoPlayerException(Exception): pass def getDBusName(): dbus_root = bus.get_object( 'org.freedesktop.DBus', '/' ) dbus_if = dbus.Interface( dbus_root, dbus_interface='org.freedesktop.DBus' ) # TODO: err with multiple matches -> configurable? for name in dbus_if.ListNames(): if name.startswith( 'org.mpris.' ): return name raise NoPlayerException def getPlayerIf(): player = bus.get_object( getDBusName(), '/Player' ) return dbus.Interface( player, dbus_interface='org.freedesktop.MediaPlayer' ) def getRootIf(): root = bus.get_object( getDBusName(), '/' ) return dbus.Interface( root, dbus_interface='org.freedesktop.MediaPlayer' ) def getMetadata( name, player_if ): return player_if.GetMetadata()[name] def getIdent( root_if ): return root_if.Identity() def getPlayingState( player_if ): return player_if.GetStatus()[0] def getPlayingStateString(x): if x == 0: return 'Playing' elif x == 1: return 'Paused' elif x == 2: return 'Stopped' def print_info( data, buffer, args ): try: player_if = getPlayerIf() root_if = getRootIf() state = getPlayingState( player_if ) if state == 0: artist = '' title = '' album = '' try: artist = getMetadata( 'artist', player_if ) except KeyError: pass try: title = getMetadata( 'title', player_if ) except KeyError: pass try: album = getMetadata( 'album', player_if ) except KeyError: pass if title == '': string = 'np: no title available' else: string = 'np: ' if artist != '': string += '"' + artist + '" - ' if title != '': string += '"' + title + '"' if album != '': string += ' on "' + album + '"' string += ' using "' + getIdent( root_if ) + '"' else: string = 'np: "' + getIdent( root_if ) + '": ' + getPlayingStateString( state ) except NoPlayerException: string = 'np: no player running' #weechat.prnt( buffer, string ) weechat.command( buffer, string ) return weechat.WEECHAT_RC_OK if __name__ == '__main__': global bus bus = dbus.SessionBus() weechat.hook_command( 'np', 'Print information on the currently played song', '', '', '', 'print_info', '' ) |