Last active
April 30, 2016 14:35
-
-
Save ask-compu/cadc07a6a3f93c7b6663452b747f66bd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import print_function | |
import hexchat | |
import dbus | |
import re | |
__module_name__ = 'Spoti.py' | |
__module_version__ = '0.0.3' | |
__module_description__ = 'Spotify controller' | |
__module_author__ = 'Scout @ irc.geekshed.net/codecrew' | |
_busname = 'org.mpris.MediaPlayer2.spotify' | |
# thanks to http://xchatdata.net/Scripting/TextFormatting | |
def colorDecode(word): | |
_B = re.compile('%B', re.IGNORECASE) | |
_C = re.compile('%C', re.IGNORECASE) | |
_R = re.compile('%R', re.IGNORECASE) | |
_O = re.compile('%O', re.IGNORECASE) | |
_U = re.compile('%U', re.IGNORECASE) | |
return _B.sub('\002', _C.sub('\003', _R.sub('\026', _O.sub('\017', _U.sub('\037', word))))) | |
def spotiCheck(): | |
bus = dbus.SessionBus() | |
if bus.name_has_owner(_busname): | |
return bus.get_object(_busname, '/org/mpris/MediaPlayer2') | |
else: | |
print('Spotify is not running') | |
return None | |
def getMeta(): | |
player = spotiCheck() | |
if player: | |
info = player.Get('org.mpris.MediaPlayer2.Player', 'Metadata', | |
dbus_interface='org.freedesktop.DBus.Properties') | |
for k, v in info.items(): | |
if isinstance(v, list): | |
info[k] = ', '.join(v) | |
return info | |
else: | |
return None | |
def spNext(word, word_eol, userdata): | |
player = spotiCheck() | |
if player: | |
player.Next(dbus_interface='org.mpris.MediaPlayer2.Player') | |
return hexchat.EAT_ALL | |
def spPrev(word, word_eol, userdata): | |
player = spotiCheck() | |
if player: | |
prevSong = getMeta()['xesam:title'] | |
player.Previous(dbus_interface='org.mpris.MediaPlayer2.Player') | |
# Check to see if we actually went back a song | |
curSong = getMeta()['xesam:title'] | |
if prevSong == curSong: | |
player.Previous(dbus_interface='org.mpris.MediaPlayer2.Player') | |
return hexchat.EAT_ALL | |
def spToggleplay(word, word_eol, userdata): | |
player = spotiCheck() | |
if player: | |
player.PlayPause(dbus_interface='org.mpris.MediaPlayer2.Player') | |
return hexchat.EAT_ALL | |
def spNowPlaying(word, word_eol, userdata): | |
info = getMeta() | |
if info: | |
msg = 'is listening to %U' + info.get('xesam:title', 'nothing') + '%O' | |
if 'xesam:artist' in info: | |
msg += ' by %B' + info['xesam:artist'] + '%O' | |
if 'xesam:album' in info: | |
msg += ' from %B' + info['xesam:album'] + '%O' | |
if 'xesam:url' in info: | |
msg += ' - ' + info['xesam:url'] | |
msg = msg.replace(u'\u2013', "-") | |
hexchat.command('ME %s' % colorDecode(msg.encode('ascii', 'replace'))) | |
return hexchat.EAT_ALL | |
hexchat.hook_command('skip', spNext) | |
hexchat.hook_command('prev', spPrev) | |
hexchat.hook_command('play', spToggleplay) | |
hexchat.hook_command('pause', spToggleplay) | |
hexchat.hook_command('np', spNowPlaying) | |
print(__module_name__, __module_version__, 'has been loaded') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment