File indexing completed on 2024-04-28 04:48:05

0001 #! /usr/bin/env python
0002 
0003 # XChat now playing script for Amarok 2
0004 # by Sam Lade, 2010-2011
0005 # This script is public domain.
0006 
0007 # Usage:
0008 # In XChat:
0009 # Place the script in ~/.xchat2/ and it will be loaded automatically on start
0010 # (load manually with /py load xchat-nowplaying.py)
0011 # Type /playing to post the currently playing track in the current channel.
0012 #
0013 # For other clients:
0014 # Use with /exec -out command, e.g. to bind (in irssi):
0015 # /alias np exec -out - /home/myself/xchat-nowplaying.py
0016 #
0017 # Adjust the format of the playing string to taste.
0018 # Set a lastfmuser if desired and the script will link to your user page.
0019 # Requires the python-dbus module.
0020 lastfmuser = ""
0021 
0022 import dbus
0023 
0024 def get_playing():
0025     try:
0026         amarokbus = dbus.SessionBus().get_object("org.mpris.amarok", "/TrackList")
0027         tracknumber = amarokbus.GetCurrentTrack()
0028         data = amarokbus.GetMetadata(tracknumber)
0029         title = unicode(data[dbus.String("title")]).encode("utf-8")
0030         artist = unicode(data[dbus.String("artist")]).encode("utf-8")
0031         album = unicode(data[dbus.String("album")]).encode("utf-8")
0032         date = unicode(data[dbus.String("year")]).encode("utf-8")
0033 
0034         if not title:
0035             title = u"?"
0036 
0037         if artist:
0038             artist = " by {0}".format(artist)
0039 
0040         if (album != "") & (date != ""):
0041             extra = " [{0}, {1}]".format(album, date)
0042         elif (album != "") | (date != ""):
0043             extra = " [{0}{1}]".format(album, date)
0044         else:
0045             extra = ""
0046 
0047         if lastfmuser:
0048             lastfm = " -- see http://www.last.fm/user/{0} for more".format(lastfmuser)
0049         else:
0050             lastfm = ""
0051 
0052         playing = "{0}{1}{2}{3}".format(title, artist, extra, lastfm)
0053         del amarokbus
0054         return playing
0055     except dbus.exceptions.DBusException:
0056         return "__DBusException__"
0057 
0058 
0059 try:
0060     import xchat
0061 
0062 except ImportError:
0063     res = get_playing()
0064     if res != "__DBusException__":
0065         print "Playing:", res
0066     else:
0067         import sys
0068         sys.exit(1)
0069 
0070 else:
0071     __module_name__ = "amarok-nowplaying"
0072     __module_version__ = "0.1.3"
0073     __module_description__ = "now playing script for Amarok 2"
0074     print "Starting", __module_name__, __module_version__
0075 
0076 
0077     def nowplaying_command_hook(word, word_eol, userdata):
0078         res = get_playing()
0079         if res != "__DBusException__":
0080             xchat.command("me " + "is listening to " + res)
0081         else:
0082             print "DBus exception occurred; most likely Amarok is not running."
0083         return xchat.EAT_ALL
0084 
0085     xchat.hook_command("playing",nowplaying_command_hook)