poezio/plugins/mpd_client.py

39 lines
2 KiB
Python
Raw Normal View History

2011-11-06 19:25:30 +00:00
# a plugin adding a command to manipulate an MPD instance
from plugin import BasePlugin
from common import shell_split
from os.path import basename as base
import tabs
2011-11-06 19:25:30 +00:00
import mpd
class Plugin(BasePlugin):
def init(self):
self.add_tab_command(tabs.ConversationTab, 'mpd', self.command_mpd, "Usage: /mpd [full]\nMpd: sends a message showing the current song of an MPD instance. If full is provided, the message is more verbose.", self.completion_mpd)
self.add_tab_command(tabs.MucTab, 'mpd', self.command_mpd, "Usage: /mpd [full]\nMpd: sends a message showing the current song of an MPD instance. If full is provided, the message is more verbose.", self.completion_mpd)
self.add_tab_command(tabs.PrivateTab, 'mpd', self.command_mpd, "Usage: /mpd [full]\nMpd: sends a message showing the current song of an MPD instance. If full is provided, the message is more verbose.", self.completion_mpd)
2011-11-06 19:25:30 +00:00
def command_mpd(self, args):
args = shell_split(args)
2011-11-06 19:31:19 +00:00
c = mpd.MPDClient()
c.connect(host=self.config.get('host', 'localhost'), port=self.config.get('port', '6600'))
password = self.config.get('password', '')
if password:
c.password(password)
2011-11-06 19:25:30 +00:00
current = c.currentsong()
artist = current.get('artist', 'Unknown artist')
album = current.get('album', 'Unknown album')
title = current.get('title', base(current.get('file', 'Unknown title')))
2011-11-06 19:25:30 +00:00
s = '%s - %s (%s)' % (artist, title, album)
2011-11-06 19:25:30 +00:00
if 'full' in args:
if 'elapsed' in current and 'time' in current:
current_time = float(c.status()['elapsed'])
pourcentage = int(current_time / float(current['time']) * 10)
s += ' \x192}[\x191}' + '-'*(pourcentage-1) + '\x193}+' + '\x191}' + '-' * (10-pourcentage-1) + '\x192}]\x19o'
2011-11-06 19:25:30 +00:00
if not self.core.send_message('%s' % (s,)):
self.core.information('Cannot send result (%s)' % s, 'Error')
2011-11-06 19:25:30 +00:00
def completion_mpd(self, the_input):
return the_input.auto_completion(['full'], quotify=False)