add mpd-notifications script

illustrative purposes, mostly
This commit is contained in:
Brian S. Stephan 2015-07-11 08:17:19 -05:00
parent 4abcef58c1
commit c8bedebe97
2 changed files with 79 additions and 0 deletions

View File

@ -0,0 +1,78 @@
"""Use dr.botzo's Dispatch to send mpd notifications."""
from __future__ import unicode_literals
import argparse
import getpass
import logging
from select import select
import sys
import mpd
import requests
from requests.auth import HTTPBasicAuth
# set up some basic logging
logger = logging.getLogger()
handler = logging.StreamHandler(sys.stdout)
logger.addHandler(handler)
logger.setLevel(logging.DEBUG)
# set up config flags
parser = argparse.ArgumentParser()
parser.add_argument('-l', '--location')
parser.add_argument('-u', '--user')
parser.add_argument('-m', '--mpd-host', default='localhost')
parser.add_argument('-p', '--mpd-port', default=6600)
args = parser.parse_args()
if args.user is None or args.location is None:
print("script needs "
"-l/--location (url to connect to) -u/--user (user to authenticate to XML-RPC as)")
sys.exit(2)
password = getpass.getpass("password for {0:s}: ".format(args.user))
auth = HTTPBasicAuth(args.user, password)
client = mpd.MPDClient(use_unicode=True)
client.connect(args.mpd_host, args.mpd_port)
last_np = ''
def notify_song():
global last_np
status = client.status()
logger.debug(status)
state = status.get('state', None)
if state == 'play':
current = client.currentsong()
logger.debug(current)
filename = current.get('file', None)
if filename:
artist = current.get('artist', "Unknown")
title = current.get('title', "Unknown")
album = current.get('album', "Unknown")
np = "{0:s} - [{1:s}] {2:s}".format(artist, album, title)
if np != last_np:
last_np = np
logger.debug("notifying: {0:s}".format(np))
payload = {'message': np}
r = requests.post(args.location, verify=False, auth=auth, data=payload)
print(r.json())
notify_song()
# loop forever on mpd events
while True:
client.send_idle()
select([client], [], [])
changed = client.fetch_idle()
for change in changed:
if change == 'player':
notify_song()

View File

@ -0,0 +1 @@
python-mpd2==0.5.4