from __future__ import print_function
"""Evolved from https://gist.github.com/sours/5869011
Now also supoports Hexchat and Linux."""
# Customize this:
#COMMAND_PREFIX = "say"
#COMMAND_PREFIX = "me is now listening"
COMMAND_PREFIX = "me a ouvir:"
debugging = False
try:
import xchat
except ImportError:
try:
import hexchat
xchat = hexchat
except ImportError:
debugging = True
import platform
if platform.system() == 'Windows':
import win32gui
else:
import dbus
__module_name__ = "Spotify"
__module_version__ = "1.3"
__module_description__ = "Spotify now playing"
if debugging:
EAT_ALL = 3
else:
EAT_ALL = xchat.EAT_ALL
def prnt(s):
if debugging:
print(s)
else:
xchat.prnt(s)
prnt("Loaded %s version %s" % (__module_name__, __module_version__))
def spotify_windows(word, wordeol, userdata):
hwnd = win32gui.FindWindow("SpotifyMainWindow", None)
if not hwnd:
prnt("Spotify is not running")
return EAT_ALL
song_and_artist = win32gui.GetWindowText(hwnd)
if not song_and_artist:
prnt("No title found")
return EAT_ALL
if song_and_artist == "Spotify":
prnt("No track playing")
return EAT_ALL
if song_and_artist.startswith("Spotify - "):
song_and_artist = song_and_artist[:len("Spotify - ")]
song_and_artist = song_and_artist.decode('mbcs').encode('utf-8')
final_text = "%s %s" % (COMMAND_PREFIX, song_and_artist)
if debugging:
print(final_text)
else:
xchat.command(final_text)
return EAT_ALL
def spotify_linux(word, wordeol, userdata):
"""From http://stackoverflow.com/questions/33883360/get-spotify-currently-playing-track"""
try:
session_bus = dbus.SessionBus()
spotify_bus = session_bus.get_object("org.mpris.MediaPlayer2.spotify",
"/org/mpris/MediaPlayer2")
spotify_properties = dbus.Interface(spotify_bus,
"org.freedesktop.DBus.Properties")
metadata = spotify_properties.Get("org.mpris.MediaPlayer2.Player", "Metadata")
song = metadata['xesam:title']
artist = metadata['xesam:artist'][0]
except Exception:
prnt("Can't get Spotify current song data")
else:
final_text = "%s %s - %s" % (COMMAND_PREFIX, song, artist)
if debugging:
print(final_text)
else:
xchat.command(final_text)
finally:
return EAT_ALL
if platform.system() == 'Windows':
spotify = spotify_windows
else:
spotify = spotify_linux
def unload_cb(userdata):
prnt("Unloaded %s" % (__module_name__,))
if debugging:
spotify('', '', '')
else:
xchat.hook_command("spotify", spotify, help="/spotify - Spotify now playing")
xchat.hook_unload(unload_cb)