weepd/weepd.py

65 lines
2.0 KiB
Python
Raw Permalink Normal View History

2020-06-05 14:34:30 +00:00
"""
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)
2020-06-15 21:12:46 +00:00
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)
2020-06-05 14:34:30 +00:00
if password:
client.password(password)
status = client.status()
song = client.currentsong()
2020-06-05 15:58:57 +00:00
client.close()
2020-06-05 14:34:30 +00:00
checkset = set(song)
2023-05-11 10:45:23 +00:00
if {'artist', 'title', 'album', 'date'}.issubset(checkset):
2020-06-05 14:34:30 +00:00
command = f"/me ~ {song['artist']} {song['title']} :: {song['album']} ({song['date']})"
2023-05-11 10:45:23 +00:00
elif {'artist', 'title', 'album'}.issubset(checkset):
2020-06-05 14:34:30 +00:00
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']}"
2020-06-05 14:34:30 +00:00
else:
command = f"/me ~ {song['file'].split('/')[-1]}"
if 'volume' in status:
command += f" @ {status['volume']}%"
2020-06-15 21:12:46 +00:00
weechat.command(weechat.current_buffer(), command)
2020-06-05 14:34:30 +00:00
return weechat.WEECHAT_RC_OK