1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# IMPORTS
import sys
import pprint
from mpd import (MPDClient, CommandError)
from socket import error as SocketError
HOST = 'localhost'
PORT = '6600'
PASSWORD = False
##
CON_ID = {'host':HOST, 'port':PORT}
##
## Some functions
def mpdConnect(client, con_id):
"""
Simple wrapper to connect MPD.
"""
try:
client.connect(**con_id)
except SocketError:
return False
return True
def mpdAuth(client, secret):
"""
Authenticate
"""
try:
client.password(secret)
except CommandError:
return False
return True
##
def main():
## MPD object instance
client = MPDClient()
if mpdConnect(client, CON_ID):
print('Got connected!')
else:
print('fail to connect MPD server.')
sys.exit(1)
# Auth if password is set non False
if PASSWORD:
if mpdAuth(client, PASSWORD):
print('Pass auth!')
else:
print('Error trying to pass auth.')
client.disconnect()
sys.exit(2)
## Fancy output
pp = pprint.PrettyPrinter(indent=4)
## Print out MPD stats & disconnect
print('\nCurrent MPD state:')
pp.pprint(client.status())
print('\nMusic Library stats:')
pp.pprint(client.stats())
client.disconnect()
sys.exit(0)
# Script starts here
if __name__ == "__main__":
main()
|