#!/usr/bin/python
#-*- encoding: utf-8 -*-
import os
import sys
import subprocess
import time
from mpd import MPDClient # sudo pip install python-mpd2
import logging
import logging.handlers
if __name__ == '__main__':
_DEBUG_ = 0
argv =
sys.argv #
コマンドライン引数を格納したリストの取得
argc =
len(argv) #
引数の個数
# ユーザ判定
curr_user =
os.geteuid()
if _DEBUG_ ==
1: print "curr_user=",curr_user
if curr_user !=
0:
print("Must be root")
sys.exit()
#
LDC表示プログラムの絶対パス
lcdPath =
os.path.join(os.path.dirname(os.path.abspath(__file__)),'lcd-gpio3')
#lcdPath =
os.path.join(os.path.dirname(os.path.abspath(__file__)),'lcd-dummy')
if _DEBUG_ ==
1: print "lcdPath=",lcdPath
# start syslog
my_logger =
logging.getLogger('MyLogger')
my_logger.setLevel(logging.DEBUG)
handler =
logging.handlers.SysLogHandler(address = '/dev/log')
my_logger.addHandler(handler)
# connect mpd
server
client =
MPDClient()
# create client object
client.connect("localhost",
6600) # connect to
localhost:6600
if _DEBUG_ ==
1: print "connect"
while True:
client.idle()
if _DEBUG_ == 1: print(client.status()["state"])
if client.status()["state"] == "play":
if _DEBUG_ == 1: print(client.currentsong())
name="name" in client.currentsong()
file="file" in client.currentsong()
title="title" in client.currentsong()
if name:
if _DEBUG_ == 1: print('name:[%s]' %
client.currentsong()["name"])
if file:
if _DEBUG_ == 1: print('file:[%s]' %
client.currentsong()["file"])
if title:
#if _DEBUG_ == 1: print('title:%s' %
client.currentsong()["title"])
titleString = client.currentsong()["title"]
artist, song = titleString.split(' - ')
artist = artist.strip()
song = song.strip()
if _DEBUG_ == 1: print('artist:[%s]' % artist)
if _DEBUG_ == 1: print('song:[%s]' % song)
# Display on LCD
try:
subprocess.call([lcdPath, artist, song])
except:
print('radio:subprocess.check_call() failed')
my_logger.critical('radio:subprocess.check_call() failed')
if client.status()["state"] == "stop":
# Clear on LCD & turn off Backlight
try:
subprocess.call([lcdPath])
except:
print('radio:subprocess.check_call() failed')
my_logger.critical('radio:subprocess.check_call() failed')
# never reach
here
client.close()
# send the close command
client.disconnect()
# disconnect from the server
|