Rivendellaudio/apis/pypad/scripts/pypad_icecast2.py
Fred Gleason 0ef5a88a02 2024-05-29 Fred Gleason <fredg@paravelsystems.com>
* Fixed a bug in the 'pypad_icecast2.py PyPAD script that caused
	mojibake when sending ISO-8859-1 characters with code points >= 0x80.

Signed-off-by: Fred Gleason <fredg@paravelsystems.com>
2024-05-29 15:20:20 -04:00

105 lines
3.4 KiB
Python
Executable File

#!%PYTHON_BANGPATH%
# pypad_icecast2.py
#
# Send PAD updates to Icecast2 mountpoint
#
# (C) Copyright 2018-2024 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
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public
# License along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import os.path
import sys
import socket
import requests
from requests.auth import HTTPBasicAuth
from requests import Session, Request
import xml.etree.ElementTree as ET
import syslog
from urllib.parse import quote
try:
from rivendellaudio import pypad
except ModuleNotFoundError:
import pypad # Rivendell v3.x style
import configparser
def eprint(*args,**kwargs):
print(*args,file=sys.stderr,**kwargs)
def IcecastQuote(string):
string=quote(string,safe=' ',encoding='ISO-8859-1',errors='replace')
return string
def ProcessPad(update):
if update.hasPadType(pypad.TYPE_NOW):
n=1
while(True):
#
# First, get all of our configuration values
#
section='Icecast'+str(n)
try:
values={}
values['mount']=update.config().get(section,'Mountpoint')
values['mode']='updinfo'
hostname=update.config().get(section,'Hostname')
tcpport=update.config().get(section,'Tcpport')
username=update.config().get(section,'Username')
password=update.config().get(section,'Password')
url="http://%s:%s/admin/metadata" % (hostname,tcpport)
except configparser.NoSectionError:
if(n==1):
update.syslog(syslog.LOG_WARNING,'No icecast config found')
return
#
# Now, send the update
#
if update.shouldBeProcessed(section):
try:
req=Request('GET',url,auth=HTTPBasicAuth(username,password),params=values)
prep=req.prepare()
song=update.resolvePadFields(update.config().get(section,'FormatString'),pypad.ESCAPE_NONE)
prep.url=prep.url+'&song='+IcecastQuote(song)
response=sess.send(prep)
response.raise_for_status()
update.syslog(syslog.LOG_INFO,'Updating http://'+hostname+values['mount']+': song='+song)
except requests.exceptions.RequestException as e:
update.syslog(syslog.LOG_WARNING,str(e))
n=n+1
#
# Program Name
#
pypad_name=sys.argv[0].split('/')[-1]
#
# Create Send Socket
#
send_sock=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
#
# Start Receiver
#
rcvr=pypad.Receiver()
try:
rcvr.setConfigFile(sys.argv[3])
except IndexError:
eprint('pypad_icecast2: USAGE: cmd <hostname> <port> <config>')
sys.exit(1)
rcvr.setPadCallback(ProcessPad)
sess=Session()
rcvr.start(sys.argv[1],int(sys.argv[2]))