Skip to content
Browse files

Initial commit, works with Exaile, Clementine, and Amarok. Only issue…

… is position/length
  • Loading branch information...
0 parents commit 611d43005dc53313c65d87ed3ac2c2547ce29752 @duckinator committed Mar 18, 2011
Showing with 114 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. +113 −0 xchat-mpris.py
1 .gitignore
@@ -0,0 +1 @@
+xchat-mpris-player.txt
113 xchat-mpris.py
@@ -0,0 +1,113 @@
+import xchat, dbus, os, inspect
+
+__module_name__ = "xchat-mpris"
+__module_version__ = "0.1"
+__module_description__ = "Fetches information from MPRIS-compliant music players"
+
+conf_file = 'xchat-mpris-player.txt'
+
+FILE = inspect.currentframe().f_code.co_filename # I died a bit inside...
+DIR = os.path.dirname(FILE)
+CONF = os.path.join(DIR, conf_file)
+
+bus = dbus.SessionBus()
+
+player = ''
+
+def isConfigured():
+ return (os.path.exists(CONF) and open(CONF).read() != '')
+
+def loadConfig():
+ global player
+ if isConfigured():
+ player = open(CONF).read()
+ return True
+ return False
+
+def saveConfig():
+ with open(CONF, 'w') as f:
+ f.write(player)
+
+def parseSongPosition(time):
+ #return (int(time) / 1000, int(time) % 1000)
+ return (0, int(time))
+
+def parseSongLength(time):
+ return (0, int(time))
+
+def getSongInfo():
+ try:
+ remote_object = bus.get_object("org.mpris.%s" % (player), "/Player")
+ iface = dbus.Interface(remote_object, "org.freedesktop.MediaPlayer")
+ #xchat.prnt(iface.GetMetadata()["title"])
+ #return ("", "", "", "", 0)
+
+ #if iface.IsPlaying():
+ data = iface.GetMetadata()
+ title = data["title"]
+ album = data["album"]
+ artist = data["artist"]
+ pos = parseSongPosition(iface.PositionGet())
+ length = parseSongLength(data["time"])
+ #if length > 0:
+ # length = '%d:%02d' % length
+ #else:
+ # length = "0:00"
+
+ return (artist, title, album, pos, length)
+ #else:
+ # return 0
+ except dbus.exceptions.DBusException:
+ return 1
+
+def getPlayerVersion():
+ try:
+ remote_object = bus.get_object("org.mpris.%s" % (player), "/")
+ iface = dbus.Interface(remote_object, "org.freedesktop.MediaPlayer")
+ version = iface.Identity()
+
+ except dbus.exceptions.DBusException:
+ xchat.command("DBus Exception")
+ pass
+
+def mprisPlayerVersion(word, word_eol, userdata):
+ xchat.command("ME is using %s as their current media player" % getPlayerVersion())
+ return xchat.EAT_ALL
+
+def mprisNp(word, word_eol, userdata):
+ xchat.command("ME is listening to %s - %s [%s] [%s/%s]" % getSongInfo())
+ return xchat.EAT_ALL
+
+def mprisPlayer(word, word_eol, userdata):
+ global player
+ if len(word) > 1:
+ oldplayer = player
+ player = word[1]
+ if player == '':
+ pass
+ elif oldplayer != '' and oldplayer != player:
+ xchat.prnt("Media player changed from \"%s\" to \"%s\"" % (oldplayer, player))
+ else:
+ xchat.prnt("Media player set to \"%s\"" % player)
+ saveConfig()
+ return xchat.EAT_ALL
+ else:
+ pass
+ xchat.prnt("USAGE: %s <player name>, set default meda player." % word[0])
+ return xchat.EAT_ALL
+
+if isConfigured():
+ loadConfig()
+
+xchat.prnt("MPRIS now playing script initialized")
+xchat.prnt("Use /player <player name> to specify the media player you are using.")
+xchat.prnt("Use /np to send information on the current song to the active channel.")
+xchat.prnt("Also provides: /next, /prev, /play, /pause, /stop")
+xchat.hook_command("PLAYER", mprisPlayer, help="Usage: PLAYER <player name>, set default player.\nOnly needs to be done initially and when changing players.")
+xchat.hook_command("NP", mprisNp, help="Usage: NP, send information on current song to the active channel")
+#xchat.hook_command("NEXT", mprisNext, help="Usage: NEXT, play next song")
+#xchat.hook_command("PREV", mprisPrev, help="Usage: PREV, play previous song")
+#xchat.hook_command("PLAY", mprisPlay, help="Usage: PLAY, play the music")
+#xchat.hook_command("PAUSE", mprisPause, help="Usage: PAUSE, pause the music")
+#xchat.hook_command("STOP", mprisStop, help="Usage: STOP, hammer time!")
+xchat.hook_command("PLAYERVERSION", mprisPlayerVersion, help="Usage: PLAYERVERSION, version of the media player you are using")

0 comments on commit 611d430

Please sign in to comment.
Something went wrong with that request. Please try again.