65 lines
2.0 KiB
Python
65 lines
2.0 KiB
Python
"""
|
||
MusicPD plugin for weechat.
|
||
|
||
Adds a /song command that will /me your currently playing song.
|
||
Respects MPD_HOST env var.
|
||
"""
|
||
|
||
from os import environ
|
||
from mpd import MPDClient
|
||
|
||
import weechat
|
||
|
||
weechat.register('weepd', 'phryk', '0.0.0.0-alpha', 'BSD', 'MusicPD plugin', 'shutdown', 'UTF-8')
|
||
weechat.hook_command('song', 'send the currently playing song to the current channel', 'FUCK-A', 'FUCK-B', 'FUCK-C', 'song', 'FUCK-D')
|
||
|
||
def shutdown():
|
||
pass
|
||
|
||
def song(*args, **kwargs):
|
||
|
||
host = 'localhost'
|
||
port = 6600
|
||
password = None
|
||
|
||
if 'MPD_HOST' in environ:
|
||
if '@' in environ['MPD_HOST']:
|
||
password, host = environ['MPD_HOST'].split('@', 1)
|
||
else:
|
||
host = environ['MPD_HOST']
|
||
|
||
if ':' in host:
|
||
host, port = host.split(':', 1)
|
||
else:
|
||
port = 6600
|
||
|
||
client = MPDClient(use_unicode=True)
|
||
if host.startswith('/'): # '/' in host might be better, but this is what python-mpd2 currently does, see: https://github.com/Mic92/python-mpd2/issues/120
|
||
client.connect(host) # acshully is AF_UNIX socket
|
||
else:
|
||
client.connect(host, port)
|
||
if password:
|
||
client.password(password)
|
||
|
||
status = client.status()
|
||
song = client.currentsong()
|
||
client.close()
|
||
|
||
checkset = set(song)
|
||
if {'artist', 'title', 'album', 'date'}.issubset(checkset):
|
||
command = f"/me ~ {song['artist']} – {song['title']} :: {song['album']} ({song['date']})"
|
||
elif {'artist', 'title', 'album'}.issubset(checkset):
|
||
command = f"/me ~ {song['artist']} – {song['title']} :: {song['album']}"
|
||
elif {'artist', 'title'}.issubset(checkset):
|
||
command = f"/me ~ {song['artist']} – {song['title']}"
|
||
elif 'title' in checkset:
|
||
command = f"/me ~ {song['title']}"
|
||
else:
|
||
command = f"/me ~ {song['file'].split('/')[-1]}"
|
||
|
||
if 'volume' in status:
|
||
command += f" @ {status['volume']}%"
|
||
|
||
weechat.command(weechat.current_buffer(), command)
|
||
return weechat.WEECHAT_RC_OK
|