From b09ec691034f58686d8df83ddb2d1e7382e72eff Mon Sep 17 00:00:00 2001 From: Fred Gleason Date: Wed, 10 Apr 2024 17:12:21 -0400 Subject: [PATCH] 2024-04-10 Fred Gleason * Stubbed out a 'pypad_sftpfilewrite.py'. PyPAD script. Signed-off-by: Fred Gleason --- ChangeLog | 2 + INSTALL | 2 +- apis/pypad/scripts/Makefile.am | 8 +- apis/pypad/scripts/pypad_sftpwrite.exemplar | 97 +++++++++++++++++++++ apis/pypad/scripts/pypad_sftpwrite.py | 77 ++++++++++++++++ 5 files changed, 184 insertions(+), 2 deletions(-) create mode 100644 apis/pypad/scripts/pypad_sftpwrite.exemplar create mode 100644 apis/pypad/scripts/pypad_sftpwrite.py diff --git a/ChangeLog b/ChangeLog index 7e2b9c55..454c08ab 100644 --- a/ChangeLog +++ b/ChangeLog @@ -24702,3 +24702,5 @@ 2024-04-06 Fred Gleason * Incremented the package version to 4.2.0 * Incremented the Python API version to 4.2.0. +2024-04-10 Fred Gleason + * Stubbed out a 'pypad_sftpfilewrite.py'. PyPAD script. diff --git a/INSTALL b/INSTALL index 4882a683..99a8d95f 100644 --- a/INSTALL +++ b/INSTALL @@ -275,7 +275,7 @@ DISTRO-SPECIFIC NOTES --------------------- 1) RedHat Enterprise Linux 7 -Required build packages: git gcc-c++ automake autoconf libtool qt5-qtbase-devel qt5-qtbase-mysql qt5-linguist qt5-qtwebkit-devel libcurl-devel cdparanoia-devel hpklinux-devel alsa-lib-devel jack-audio-connection-kit-devel libsamplerate-devel libsndfile-devel id3lib-devel libvorbis-devel flac-devel pam-devel soundtouch-devel twolame-devel libmad-devel lame-devel rpm-build createrepo fop docbook5-style-xsl libxslt kernel-devel rpm-sign man-pages openssl-devel taglib-devel libmusicbrainz5-devel libdiscid-devel libcoverart libcoverart-devel ImageMagick-c++-devel +Required build packages: git gcc-c++ automake autoconf libtool qt5-qtbase-devel qt5-qtbase-mysql qt5-linguist qt5-qtwebkit-devel libcurl-devel cdparanoia-devel hpklinux-devel alsa-lib-devel jack-audio-connection-kit-devel libsamplerate-devel libsndfile-devel id3lib-devel libvorbis-devel flac-devel pam-devel soundtouch-devel twolame-devel libmad-devel lame-devel rpm-build createrepo fop docbook5-style-xsl libxslt kernel-devel rpm-sign man-pages openssl-devel taglib-devel libmusicbrainz5-devel libdiscid-devel libcoverart libcoverart-devel ImageMagick-c++-devel python36-paramiko Configure script invocation: ./configure --prefix=/usr --libdir=/usr/lib64 --libexecdir=/var/www/rd-bin --sysconfdir=/etc/httpd/conf.d diff --git a/apis/pypad/scripts/Makefile.am b/apis/pypad/scripts/Makefile.am index 36da0b22..2a8eacd8 100644 --- a/apis/pypad/scripts/Makefile.am +++ b/apis/pypad/scripts/Makefile.am @@ -2,7 +2,7 @@ ## ## Makefile.am for Rivendell pypad/examples ## -## (C) Copyright 2018-2023 Fred Gleason +## (C) Copyright 2018-2024 Fred Gleason ## ## This program is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as @@ -42,6 +42,8 @@ install-exec-am: cp pypad_nautel.exemplar $(DESTDIR)/usr/lib/rivendell/pypad/pypad_nautel.exemplar ../../../helpers/install_python.sh pypad_serial.py $(DESTDIR)/usr/lib/rivendell/pypad/pypad_serial.py cp pypad_serial.exemplar $(DESTDIR)/usr/lib/rivendell/pypad/pypad_serial.exemplar + ../../../helpers/install_python.sh pypad_sftpwrite.py $(DESTDIR)/usr/lib/rivendell/pypad/pypad_sftpwrite.py + cp pypad_sftpwrite.exemplar $(DESTDIR)/usr/lib/rivendell/pypad/pypad_sftpwrite.exemplar ../../../helpers/install_python.sh pypad_shoutcast1.py $(DESTDIR)/usr/lib/rivendell/pypad/pypad_shoutcast1.py cp pypad_shoutcast1.exemplar $(DESTDIR)/usr/lib/rivendell/pypad/pypad_shoutcast1.exemplar ../../../helpers/install_python.sh pypad_spinitron.py $(DESTDIR)/usr/lib/rivendell/pypad/pypad_spinitron.py @@ -84,6 +86,8 @@ uninstall-local: rm -f $(DESTDIR)/usr/lib/rivendell/pypad/pypad_nautel.py rm -f $(DESTDIR)/usr/lib/rivendell/pypad/pypad_serial.exemplar rm -f $(DESTDIR)/usr/lib/rivendell/pypad/pypad_serial.py + rm -f $(DESTDIR)/usr/lib/rivendell/pypad/pypad_sftpwrite.exemplar + rm -f $(DESTDIR)/usr/lib/rivendell/pypad/pypad_sftpwrite.py rm -f $(DESTDIR)/usr/lib/rivendell/pypad/pypad_shoutcast1.exemplar rm -f $(DESTDIR)/usr/lib/rivendell/pypad/pypad_shoutcast1.py rm -f $(DESTDIR)/usr/lib/rivendell/pypad/pypad_spinitron.exemplar @@ -125,6 +129,8 @@ EXTRA_DIST = pypad_ando.exemplar\ pypad_nautel.py\ pypad_serial.exemplar\ pypad_serial.py\ + pypad_sftpwrite.exemplar\ + pypad_sftpwrite.py\ pypad_shoutcast1.exemplar\ pypad_shoutcast1.py\ pypad_spinitron.exemplar\ diff --git a/apis/pypad/scripts/pypad_sftpwrite.exemplar b/apis/pypad/scripts/pypad_sftpwrite.exemplar new file mode 100644 index 00000000..4bc8a1f8 --- /dev/null +++ b/apis/pypad/scripts/pypad_sftpwrite.exemplar @@ -0,0 +1,97 @@ +; This is the configuration for the 'pypad_sftpwrite' script for +; Rivendell, which can be used to a remote system via the SFTP protocol +; using Now & Next data. + +; Section Header +; +; One section per file to be written should be configured, starting with +; 'Url1' and working up consecutively +[Url1] + +; Url +; +; The URL to which to send the file, which may contain filepath +; wildcards as defined in Appendix C of the Rivendell Operations and +; Administration Guide. +; +Url=sftp://server.example.com/myfile.txt + +; Username +; +; The username to be used when authenticating to the remote server. +; +Username=someuser + +; Password +; +; The password to be used when authenticating to the remote server. +Password=hackme + +; Format String. The string to be sent each time RDAirPlay changes +; play state, including any wildcards as placeholders for metadata values. +; +; The list of available wildcards can be found in the 'Metadata Wildcards' +; appendix of the Rivendell Operations Guide. +; +FormatString=NOW: %d(ddd MMM d hh:mm:ss yyyy): %t - %a\nNEXT: %D(ddd MMM d hh:mm:ss yyyy): %T - %A\n + +; Encoding. Defines the set of escapes to be applied to the PAD fields. +; The following options are available: +; +; 0 - Perform no character escaping. +; 1 - "XML" escaping: Escape reserved characters as per XML-v1.0 +; 2 - "URL" escaping: Escape reserved characters as per RFC 2396 Section 2.4 +; 3 - "JSON" escaping: Escape reserved characters as per ECMA-404 +Encoding=0 + +; Log Selection +; +; Set the status for each log to 'Yes', 'No' or 'Onair' to indicate whether +; state changes on that log should be output. If set to 'Onair', then +; output will be generated only if RDAirPlays OnAir flag is active. +MasterLog=Yes +Aux1Log=Yes +Aux2Log=Yes +VLog101=No +VLog102=No +VLog103=No +VLog104=No +VLog105=No +VLog106=No +VLog107=No +VLog108=No +VLog109=No +VLog110=No +VLog111=No +VLog112=No +VLog113=No +VLog114=No +VLog115=No +VLog116=No +VLog117=No +VLog118=No +VLog119=No +VLog120=No + + +[NowGroups] +; Group Selection +; +; Filter updates according to the Group membership of the 'now' playing +; event. If no groups are listed here and in the [NextGroups] section, +; then ALL updates will be forwarded +; without regard to Group. +; Group1=MUSIC +; Group2=LEGAL +; [...] ; Additional groups can be added... + +[NextGroups] +; Group Selection +; +; Filter updates according to the Group membership of the 'next' playing +; event. If no groups are listed here, If no groups are listed here and in +; the [NowGroups] section,then ALL updates will be forwarded +; without regard to Group. +; Group1=MUSIC +; Group2=LEGAL +; [...] ; Additional groups can be added... diff --git a/apis/pypad/scripts/pypad_sftpwrite.py b/apis/pypad/scripts/pypad_sftpwrite.py new file mode 100644 index 00000000..195f18a6 --- /dev/null +++ b/apis/pypad/scripts/pypad_sftpwrite.py @@ -0,0 +1,77 @@ +#!%PYTHON_BANGPATH% + +# pypad_sftpwrite.py +# +# Write PAD updates to arbitrary URLs +# +# (C) Copyright 2018-2022 Fred Gleason +# +# 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 sys +import syslog +import configparser +import paramiko +import socket +import urllib +try: + from rivendellaudio import pypad +except ModuleNotFoundError: + import pypad # Rivendell v3.x style +from io import BytesIO + +def eprint(*args,**kwargs): + print(*args,file=sys.stderr,**kwargs) + +def ProcessPad(update): + n=1 + section='Url'+str(n) + while(update.config().has_section(section)): + if update.shouldBeProcessed(section): + fmtstr=update.config().get(section,'FormatString') + url=urllib.parse.urlparse(update.resolveFilepath(update.config().get(section,'Url'),update.dateTime())) + username=update.config().get(section,'Username') + password=update.config().get(section,'Password') + ssh=paramiko.SSHClient() + #ssh.load_host_keys('/usr/lib/rivendell/ssh/known_hosts') + ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) + try: + ssh.connect(url.netloc,username=username,password=password) + sftp=ssh.open_sftp() + path=url.path + if path[0]=='/': # Single leading '/' means relative path + path=path[1::] + fh=sftp.file(filename=path,mode='w') + fh.write(update.resolvePadFields(fmtstr,int(update.config().get(section,'Encoding'))).encode('utf-8')) + fh.close() + sftp.close() + except paramiko.ssh_exception.NoValidConnectionsError as errors: + update.syslog(syslog.LOG_WARNING,'connection failed: '+str(errors)) + n=n+1 + section='Url'+str(n) + + +# +# 'Main' function +# +rcvr=pypad.Receiver() +try: + paramiko.util.log_to_file('/home/fredg/temp/paramiko.txt') + rcvr.setConfigFile(sys.argv[3]) +except IndexError: + eprint('pypad_sftpwrite.py: USAGE: cmd ') + sys.exit(1) +rcvr.setPadCallback(ProcessPad) +rcvr.start(sys.argv[1],int(sys.argv[2]))