2019-06-24 Fred Gleason <fredg@paravelsystems.com>

* Added a 'pypad.rivendellConfig()' method.
	* Added a 'pypad.syslog()' method.
This commit is contained in:
Fred Gleason 2019-06-24 18:22:36 -04:00
parent 52dfde374e
commit 338fd54796
10 changed files with 47 additions and 74 deletions

View File

@ -18827,3 +18827,6 @@
script.
2019-06-24 Fred Gleason <fredg@paravelsystems.com>
* Added 'RDApplication::syslog()' methods.
2019-06-24 Fred Gleason <fredg@paravelsystems.com>
* Added a 'pypad.rivendellConfig()' method.
* Added a 'pypad.syslog()' method.

View File

@ -82,10 +82,11 @@ FIELD_EXTERNAL_ANNC_TYPE='externalAnncType'
PAD_TCP_PORT=34289
class Update(object):
def __init__(self,pad_data,config):
def __init__(self,pad_data,config,rd_config):
self.__fields=pad_data
#print('PAD: '+str(self.__fields))
self.__config=config
self.__rd_config=rd_config
def __fromIso8601(self,string):
try:
@ -326,6 +327,13 @@ class Update(object):
except TypeError:
return False;
def rivendellConfig(self):
"""
Returns a parserconfig object containing the contents of the
current rd.conf(5) file.
"""
return self.__rd_config
def serviceName(self):
"""
Returns the name of the service associated with this update (string).
@ -734,6 +742,13 @@ class Update(object):
if self.__config.get(section,log_dict[self.machine()]).lower()=='onair':
return self.onairFlag()
def syslog(self,priority,msg):
"""
Send a message to the syslog.
"""
if((priority&248)==0):
priority=priority|(int(self.__rd_config.get('Identity','SyslogFacility',fallback=syslog.LOG_USER))<<3)
syslog.syslog(priority,msg)
@ -833,13 +848,14 @@ class Receiver(object):
# So we exit cleanly when shutdown by rdpadengined(8)
signal.signal(signal.SIGTERM,SigHandler)
# Open the configuration file
config=configparser.ConfigParser(interpolation=None)
config.readfp(open('/etc/rd.conf'))
# Open rd.conf(5)
rd_config=configparser.ConfigParser(interpolation=None)
rd_config.readfp(open('/etc/rd.conf'))
# Open the syslog
pypad_name=sys.argv[0].split('/')[-1]
syslog.openlog(pypad_name,logoption=syslog.LOG_PID,facility=config.get('Identity','SyslogFacility',fallback=syslog.LOG_USER))
print('SyslogFacility: '+str(rd_config.get('Identity','SyslogFacility')))
syslog.openlog(pypad_name,logoption=syslog.LOG_PID,facility=int(rd_config.get('Identity','SyslogFacility',fallback=syslog.LOG_USER)))
# Connect to the PAD feed
sock=socket.socket(socket.AF_INET)
@ -869,7 +885,7 @@ class Receiver(object):
if c[0]==10:
msg+=line.decode('utf-8')
if line.decode('utf-8')=="\r\n":
self.__pypad_Process(Update(json.loads(msg),self.__config_parser))
self.__pypad_Process(Update(json.loads(msg),self.__config_parser,rd_config))
msg=""
line=bytes()
if self.__timer_interval!=None:

View File

@ -30,20 +30,6 @@ import syslog
import pypad
import configparser
def eprint(*args,**kwargs):
print(pypad_name+': ',file=sys.stderr,end='')
print(*args,file=sys.stderr)
syslog.syslog(syslog.LOG_ERR,*args)
def iprint(*args,**kwargs):
print(pypad_name+': ',file=sys.stdout,end='')
print(*args,file=sys.stdout)
syslog.syslog(syslog.LOG_INFO,*args)
def isTrue(string):
l=['Yes','On','True','1']
return string.lower() in map(str.lower,l)
def ProcessPad(update):
if update.hasPadType(pypad.TYPE_NOW):
n=1
@ -54,17 +40,17 @@ def ProcessPad(update):
values['mount']=update.config().get(section,'Mountpoint')
values['song']=update.resolvePadFields(update.config().get(section,'FormatString'),pypad.ESCAPE_NONE)
values['mode']='updinfo'
iprint('Updating '+update.config().get(section,'Hostname')+': song='+values['song'])
update.syslog(syslog.LOG_INFO,'Updating '+update.config().get(section,'Hostname')+': song='+values['song'])
url="http://%s:%s/admin/metadata" % (update.config().get(section,'Hostname'),update.config().get(section,'Tcpport'))
try:
response=requests.get(url,auth=HTTPBasicAuth(update.config().get(section,'Username'),update.config().get(section,'Password')),params=values)
response.raise_for_status()
except requests.exceptions.RequestException as e:
eprint(str(e))
update.syslog(syslog.LOG_WARNING,str(e))
n=n+1
except configparser.NoSectionError:
if(n==1):
eprint('No icecast config found')
update.syslog(syslog.LOG_WARNING,'No icecast config found')
return
#
@ -87,6 +73,4 @@ except IndexError:
eprint('pypad_icecast2: USAGE: cmd <hostname> <port> <config>')
sys.exit(1)
rcvr.setPadCallback(ProcessPad)
iprint('Started')
rcvr.start(sys.argv[1],int(sys.argv[2]))
iprint('Stopped')

View File

@ -4,7 +4,7 @@
#
# Write PAD updates to Live365 stations
#
# (C) Copyright 2018 Fred Gleason <fredg@paravelsystems.com>
# (C) Copyright 2018-2019 Fred Gleason <fredg@paravelsystems.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
@ -52,9 +52,9 @@ def ProcessPad(update):
curl.perform()
code=curl.getinfo(pycurl.RESPONSE_CODE)
if (code<200) or (code>=300):
syslog.syslog(syslog.LOG_WARNING,'['+section+'] returned response code '+str(code))
update.syslog(syslog.LOG_WARNING,'['+section+'] returned response code '+str(code))
except pycurl.error:
syslog.syslog(syslog.LOG_WARNING,'['+section+'] failed: '+curl.errstr())
update.syslog(syslog.LOG_WARNING,'['+section+'] failed: '+curl.errstr())
curl.close()
n=n+1

View File

@ -59,9 +59,9 @@ def ProcessPad(update):
curl.perform()
code=curl.getinfo(pycurl.RESPONSE_CODE)
if (code<200) or (code>=300):
syslog.syslog(syslog.LOG_WARNING,'['+section+'] returned response code '+str(code))
update.syslog(syslog.LOG_WARNING,'['+section+'] returned response code '+str(code))
except pycurl.error:
syslog.syslog(syslog.LOG_WARNING,'['+section+'] failed: '+curl.errstr())
update.syslog(syslog.LOG_WARNING,'['+section+'] failed: '+curl.errstr())
curl.close()
n=n+1

View File

@ -104,9 +104,9 @@ def ProcessPad(update):
curl.perform()
code=curl.getinfo(pycurl.RESPONSE_CODE)
if (code<200) or (code>=300):
syslog.syslog(syslog.LOG_WARNING,'['+section+'] returned response code '+str(code))
update.syslog(syslog.LOG_WARNING,'['+section+'] returned response code '+str(code))
except pycurl.error:
syslog.syslog(syslog.LOG_WARNING,'['+section+'] failed: '+curl.errstr())
update.syslog(syslog.LOG_WARNING,'['+section+'] failed: '+curl.errstr())
curl.close()
n=n+1

View File

@ -29,20 +29,6 @@ import syslog
import pypad
import configparser
def eprint(*args,**kwargs):
print(pypad_name+': ',file=sys.stderr,end='')
print(*args,file=sys.stderr)
syslog.syslog(syslog.LOG_ERR,*args)
def iprint(*args,**kwargs):
print(pypad_name+': ',file=sys.stdout,end='')
print(*args,file=sys.stdout)
syslog.syslog(syslog.LOG_INFO,*args)
def isTrue(string):
l=['Yes','On','True','1']
return string.lower() in map(str.lower,l)
def ProcessPad(update):
if update.hasPadType(pypad.TYPE_NOW):
n=1
@ -56,21 +42,21 @@ def ProcessPad(update):
values['title']=update.resolvePadFields(update.config().get(section,'TitleString'),pypad.ESCAPE_NONE)
values['artist']=update.resolvePadFields(update.config().get(section,'ArtistString'),pypad.ESCAPE_NONE)
values['album']=update.resolvePadFields(update.config().get(section,'AlbumString'),pypad.ESCAPE_NONE)
iprint('Updating TuneIn: artist='+values['artist']+' title='+values['title']+' album='+values['album'])
update.syslog(syslog.LOG_INFO,'Updating TuneIn: artist='+values['artist']+' title='+values['title']+' album='+values['album'])
try:
response=requests.get('http://air.radiotime.com/Playing.ashx',params=values)
response.raise_for_status()
except requests.exceptions.RequestException as e:
eprint(str(e))
update.syslog(syslog.LOG_WARNING,str(e))
else:
xml=ET.fromstring(response.text)
status=xml.find('./head/status')
if(status.text!='200'):
eprint('Update Failed: '+xml.find('./head/fault').text)
update.syslog(syslog.LOG_WARNING,'Update Failed: '+xml.find('./head/fault').text)
n=n+1
except configparser.NoSectionError:
if(n==1):
eprint('No station config found')
update.syslog(syslog.LOG_WARNING,'No station config found')
return
#
@ -93,6 +79,4 @@ except IndexError:
eprint('pypad_tunein.py: USAGE: cmd <hostname> <port> <config>')
sys.exit(1)
rcvr.setPadCallback(ProcessPad)
iprint('Started')
rcvr.start(sys.argv[1],int(sys.argv[2]))
iprint('Stopped')

View File

@ -47,7 +47,7 @@ def ProcessPad(update):
try:
curl.perform()
except pycurl.error:
syslog.syslog(syslog.LOG_WARNING,'['+section+'] failed: '+curl.errstr())
update.syslog(syslog.LOG_WARNING,'['+section+'] failed: '+curl.errstr())
curl.close()
n=n+1

View File

@ -47,7 +47,7 @@ def ProcessPad(update):
try:
curl.perform()
except pycurl.error:
syslog.syslog(syslog.LOG_WARNING,'['+section+'] failed: '+curl.errstr())
update.syslog(syslog.LOG_WARNING,'['+section+'] failed: '+curl.errstr())
curl.close()
n=n+1

View File

@ -30,21 +30,9 @@ import xml.etree.ElementTree as ET
import time
import pypad
def eprint(*args,**kwargs):
print(pypad_name+': ',file=sys.stderr,end='')
print(*args,file=sys.stderr)
syslog.syslog(syslog.LOG_ERR,*args)
def iprint(*args,**kwargs):
print(pypad_name+': ',file=sys.stdout,end='')
print(*args,file=sys.stdout)
syslog.syslog(syslog.LOG_INFO,*args)
def dprint(*args,**kwargs):
print(pypad_name+': ',file=sys.stdout,end='')
print(*args,file=sys.stdout)
syslog.syslog(syslog.LOG_DEBUG,*args)
def XcmdResponse():
resp_code={b'+':'Command processed successfully',b'!':'Unknown command',b'-':'Invalid argument',b'/':'Command processed partially'}
@ -157,24 +145,24 @@ def ProcessPad(update):
send_sock.settimeout(5)
encoder=(update.config().get(section,'IpAddress'),int(update.config().get(section,'TcpPort')))
iprint('Connecting to {}:{}'.format(*encoder))
iprint(xcmd.decode('utf-8'))
update.syslog(syslog.LOG_INFO,'Connecting to {}:{}'.format(*encoder))
update.syslog(syslog.LOG_INFO,xcmd.decode('utf-8'))
try:
send_sock.connect(encoder)
send_sock.sendall(xcmd)
ack,response,respstr=XcmdResponse()
if response:
iprint(respstr)
update.syslog(syslog.LOG_INFO,respstr)
except OSError as e:
eprint("Socket error: {0}".format(e))
update.syslog(syslog.LOG_WARNING,"Socket error: {0}".format(e))
except IOError as e:
errno,strerror=e.args
eprint("I/O error({0}): {1}".format(errno,strerror))
update.syslog(syslog.LOG_WARNING,"I/O error({0}): {1}".format(errno,strerror))
iprint('Closing connection')
update.syslog(syslog.LOG_INFO,'Closing connection')
send_sock.close()
# Give the device time to process and close before sending another command
@ -200,6 +188,4 @@ except IndexError:
eprint('pypad_xcmd.py: USAGE: cmd <hostname> <port> <config>')
sys.exit(1)
rcvr.setPadCallback(ProcessPad)
iprint('Started')
rcvr.start(sys.argv[1],int(sys.argv[2]))
iprint('Stopped')