2013-04-13 20:33:06 +00:00
|
|
|
"""
|
|
|
|
This plugin is here to send what you are listening to in a chat tab.
|
|
|
|
|
|
|
|
Installation
|
|
|
|
------------
|
|
|
|
|
|
|
|
You need `python-mpd`_, in its python3 version.
|
|
|
|
|
|
|
|
Then you can load the plugin.
|
|
|
|
|
|
|
|
.. code-block:: none
|
|
|
|
|
|
|
|
/load mpd_client
|
|
|
|
|
|
|
|
|
|
|
|
Configuration
|
|
|
|
-------------
|
|
|
|
|
|
|
|
You have to put the following into :file:`mpd_client.cfg`, as explained in
|
|
|
|
the :ref:`plugin-configuration` section.
|
|
|
|
|
|
|
|
.. note:: If you do not put anything, the plugin will try to connect to
|
|
|
|
:file:`localhost:6600` with no password.
|
|
|
|
|
|
|
|
.. code-block:: ini
|
|
|
|
|
|
|
|
[mpd_client]
|
|
|
|
host = the_mpd_host
|
|
|
|
port = 6600
|
|
|
|
password = password if necessary
|
|
|
|
|
|
|
|
|
|
|
|
Usage
|
|
|
|
-----
|
|
|
|
|
|
|
|
.. glossary::
|
|
|
|
|
|
|
|
/mpd
|
|
|
|
**Usage:** ``/mpd [full]``
|
|
|
|
|
|
|
|
The bare command will show the current song, artist, and album
|
|
|
|
|
|
|
|
``/mpd full`` will show the current song, artist, and album,
|
|
|
|
plus a nice progress bar in color.
|
|
|
|
|
|
|
|
.. _python-mpd: https://github.com/Mic92/python-mpd2
|
|
|
|
|
|
|
|
"""
|
2011-11-06 19:25:30 +00:00
|
|
|
|
2016-06-27 23:10:52 +00:00
|
|
|
from poezio.plugin import BasePlugin
|
|
|
|
from poezio.common import shell_split
|
2016-08-21 13:27:53 +00:00
|
|
|
from poezio.core.structs import Completion
|
2012-07-07 02:39:01 +00:00
|
|
|
from os.path import basename as base
|
2016-06-27 23:10:52 +00:00
|
|
|
from poezio import tabs
|
2011-11-06 19:25:30 +00:00
|
|
|
import mpd
|
|
|
|
|
2018-08-15 11:13:17 +00:00
|
|
|
|
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):
|
2018-08-15 11:13:17 +00:00
|
|
|
self.api.add_tab_command(
|
|
|
|
_class,
|
|
|
|
'mpd',
|
|
|
|
self.command_mpd,
|
|
|
|
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)
|
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()
|
2018-08-15 11:13:17 +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:
|
2018-08-15 11:24:11 +00:00
|
|
|
c.password(password) #pylint: disable=no-member
|
|
|
|
current = c.currentsong() #pylint: disable=no-member
|
2012-07-07 02:39:01 +00:00
|
|
|
artist = current.get('artist', 'Unknown artist')
|
|
|
|
album = current.get('album', 'Unknown album')
|
2018-08-15 11:13:17 +00:00
|
|
|
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:
|
2018-08-15 11:24:11 +00:00
|
|
|
current_time = float(c.status()['elapsed']) #pylint: disable=no-member
|
2013-03-11 01:09:04 +00:00
|
|
|
percents = int(current_time / float(current['time']) * 10)
|
2018-08-15 11:13:17 +00:00
|
|
|
s += ' \x192}[\x191}' + '-' * (
|
|
|
|
percents - 1) + '\x193}+' + '\x191}' + '-' * (
|
|
|
|
10 - percents - 1) + '\x192}]\x19o'
|
|
|
|
if not self.api.send_message('%s' % (s, )):
|
2013-03-08 21:53:35 +00:00
|
|
|
self.api.information('Cannot send result (%s)' % s, 'Error')
|
2011-11-06 19:25:30 +00:00
|
|
|
|
|
|
|
def completion_mpd(self, the_input):
|
2016-08-21 13:27:53 +00:00
|
|
|
return Completion(the_input.auto_completion, ['full'], quotify=False)
|