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
|
2012-07-07 02:39:01 +00:00
|
|
|
from os.path import basename as base
|
2012-05-05 13:45:26 +00:00
|
|
|
import tabs
|
2011-11-06 19:25:30 +00:00
|
|
|
import mpd
|
2013-03-11 01:09:04 +00:00
|
|
|
import threading
|
|
|
|
from select import select
|
|
|
|
from time import sleep
|
|
|
|
|
|
|
|
class UpdateThread(threading.Thread):
|
|
|
|
"""
|
|
|
|
Background thread that awaits mpd changes
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, plugin, xmpp):
|
|
|
|
threading.Thread.__init__(self)
|
|
|
|
self.plugin = plugin
|
|
|
|
self.xmpp = xmpp
|
|
|
|
self.alive = False
|
|
|
|
self.c = mpd.MPDClient()
|
|
|
|
|
|
|
|
def run(self, *args, **kwargs):
|
|
|
|
self.alive = True
|
2013-03-11 10:31:28 +00:00
|
|
|
current = None
|
2013-03-11 01:09:04 +00:00
|
|
|
while self.alive:
|
|
|
|
try:
|
|
|
|
self.c.connect(host=self.plugin.config.get('host', 'localhost'), port=self.plugin.config.get('port', '6600'))
|
|
|
|
password = self.plugin.config.get('password', '')
|
|
|
|
if password:
|
|
|
|
self.c.password(password)
|
|
|
|
self.c.send_idle()
|
|
|
|
select([self.c], [], [])
|
|
|
|
self.c.fetch_idle()
|
|
|
|
status = self.c.status()
|
|
|
|
if status['state'] == 'play' and self.alive:
|
|
|
|
song = self.c.currentsong()
|
2013-03-11 10:31:28 +00:00
|
|
|
if current != song:
|
|
|
|
self.xmpp.plugin['xep_0118'].publish_tune(artist=song.get('artist'),
|
|
|
|
length=song.get('time'), title=song.get('title'),
|
|
|
|
track=song.get('track'), block=False)
|
|
|
|
current = song
|
|
|
|
elif status['state'] != 'play':
|
2013-03-11 01:23:31 +00:00
|
|
|
self.xmpp.plugin['xep_0118'].stop(block=False)
|
2013-03-11 10:31:28 +00:00
|
|
|
current = None
|
2013-03-11 01:09:04 +00:00
|
|
|
self.c.disconnect()
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
finally:
|
|
|
|
try:
|
|
|
|
self.c.disconnect()
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
sleep(8)
|
2011-11-06 19:25:30 +00:00
|
|
|
|
|
|
|
class Plugin(BasePlugin):
|
|
|
|
def init(self):
|
2013-03-01 18:25:31 +00:00
|
|
|
for _class in (tabs.ConversationTab, tabs.MucTab, tabs.PrivateTab):
|
2013-03-08 21:53:35 +00:00
|
|
|
self.api.add_tab_command(_class, 'mpd', self.command_mpd,
|
2013-03-01 18:25:31 +00:00
|
|
|
usage='[full]',
|
|
|
|
help='Sends a message showing the current song of an MPD instance. If full is provided, the message is more verbose.',
|
|
|
|
short='Send the MPD status',
|
|
|
|
completion=self.completion_mpd)
|
2013-03-11 01:09:04 +00:00
|
|
|
if self.config.get('broadcast', 'true').lower() != 'false':
|
|
|
|
self.core.xmpp.register_plugin('xep_0118')
|
|
|
|
self.thread = UpdateThread(plugin=self, xmpp=self.core.xmpp)
|
|
|
|
self.thread.start()
|
|
|
|
|
|
|
|
def cleanup(self):
|
|
|
|
self.thread.alive = False
|
|
|
|
self.thread.c.disconnect()
|
|
|
|
self.core.xmpp.plugin['xep_0118'].stop(block=False)
|
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()
|
2011-11-06 20:36:43 +00:00
|
|
|
c.connect(host=self.config.get('host', 'localhost'), port=self.config.get('port', '6600'))
|
2011-11-06 19:29:46 +00:00
|
|
|
password = self.config.get('password', '')
|
|
|
|
if password:
|
2011-11-06 19:41:42 +00:00
|
|
|
c.password(password)
|
2011-11-06 19:25:30 +00:00
|
|
|
current = c.currentsong()
|
2012-07-07 02:39:01 +00:00
|
|
|
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
|
|
|
|
2012-07-07 02:39:01 +00:00
|
|
|
s = '%s - %s (%s)' % (artist, title, album)
|
2011-11-06 19:25:30 +00:00
|
|
|
if 'full' in args:
|
2012-07-07 02:39:01 +00:00
|
|
|
if 'elapsed' in current and 'time' in current:
|
|
|
|
current_time = float(c.status()['elapsed'])
|
2013-03-11 01:09:04 +00:00
|
|
|
percents = int(current_time / float(current['time']) * 10)
|
|
|
|
s += ' \x192}[\x191}' + '-'*(percents-1) + '\x193}+' + '\x191}' + '-' * (10-percents-1) + '\x192}]\x19o'
|
2013-03-08 21:53:35 +00:00
|
|
|
if not self.api.send_message('%s' % (s,)):
|
|
|
|
self.api.information('Cannot send result (%s)' % s, 'Error')
|
2011-11-06 19:25:30 +00:00
|
|
|
|
|
|
|
def completion_mpd(self, the_input):
|
2012-07-07 02:39:01 +00:00
|
|
|
return the_input.auto_completion(['full'], quotify=False)
|