From fa28a37c47cd8f4f0ef8882f4f53c490252918d4 Mon Sep 17 00:00:00 2001 From: Fred Gleason Date: Tue, 7 Dec 2021 11:35:54 -0500 Subject: [PATCH] 2021-12-07 Fred Gleason * Added an 'Export()' method to the 'rivwebpyapi' API. Signed-off-by: Fred Gleason --- ChangeLog | 2 + apis/rivwebpyapi/api/rivwebpyapi.py | 89 ++++++++++++++++++++++++++ apis/rivwebpyapi/tests/Makefile.am | 1 + apis/rivwebpyapi/tests/export.py | 99 +++++++++++++++++++++++++++++ 4 files changed, 191 insertions(+) create mode 100755 apis/rivwebpyapi/tests/export.py diff --git a/ChangeLog b/ChangeLog index 87d4fe74..12bfc204 100644 --- a/ChangeLog +++ b/ChangeLog @@ -22682,3 +22682,5 @@ * Added an 'AudioStore()' method to the 'rivwebpyapi' API. 2021-12-06 Fred Gleason * Added an 'DeleteAudio()' method to the 'rivwebpyapi' API. +2021-12-07 Fred Gleason + * Added an 'Export()' method to the 'rivwebpyapi' API. diff --git a/apis/rivwebpyapi/api/rivwebpyapi.py b/apis/rivwebpyapi/api/rivwebpyapi.py index 569bf51c..97a019b6 100755 --- a/apis/rivwebpyapi/api/rivwebpyapi.py +++ b/apis/rivwebpyapi/api/rivwebpyapi.py @@ -27,6 +27,17 @@ from urllib.parse import urlparse from xml.sax.handler import ContentHandler import xml.sax +# +# Audio Formats +# +AUDIO_FORMAT_PCM16_WAV=0 +AUDIO_FORMAT_PCM24_WAV=7 +AUDIO_FORMAT_MPEG_L2_WAV=6 +AUDIO_FORMAT_MPEG_L2_NATIVE=2 +AUDIO_FORMAT_MPEG_L3_NATIVE=3 +AUDIO_FORMAT_FLAC=4 +AUDIO_FORMAT_OGG_VORBIS=5 + class RivWebPyApi_ListHandler(ContentHandler): def __init__(self,base_tag,fields): self.__output=[] @@ -618,6 +629,84 @@ class rivwebpyapi(object): if(r.status_code!=requests.codes.ok): r.raise_for_status() + def Export(self,filename,cart_number,cut_number,audio_format,channels, + sample_rate,bit_rate,quality,start_point,end_point, + normalization_level,enable_metadata): + """ + Export audio data from the audio store (file). + + Takes the following arguments: + + filename - Location to save the exported audio file (string). + + cart_number - The number of the desired cart, in the range + 1 - 999999 (integer) + + cut_number - The number of the desired cut, in the range + 1 - 999 (integer) + + audio_format - Format of the exported audio (AUDIO_FORMAT) + + channels - Channel count of the exported audio (integer) + + sample_rate - Sample rate of the exported audio (integer, + samples/sec) + + bit_rate - Bit rate of the export audio [MPEG formats only] + (integer, bits/sec) + + quality - Quality [OggVorbis only] (integer, -1 - 10) + + start_point - Start point of audio export (integer, mS from + absolute start of stored audio) + + end_point - End point of audio export (integer, mS from + absolute start of stored audio) + + normalization_level - Normalization level, 0 = no normalization + (integer, dBFS) + + enable_metadata - Encode metadata into exported file (boolean) + """ + + if((cart_number<1)or(cart_number>999999)): + raise ValueError('invalid cart number') + if((cut_number<1)or(cut_number>999)): + raise ValueError('invalid cut number') + + # + # Build the WebAPI arguments + # + postdata={ + 'COMMAND': '1', + 'LOGIN_NAME': self.__connection_username, + 'PASSWORD': self.__connection_password, + 'CART_NUMBER': str(cart_number), + 'CUT_NUMBER': str(cut_number), + 'FORMAT': str(audio_format), + 'CHANNELS': str(channels), + 'SAMPLE_RATE': str(sample_rate), + 'BIT_RATE': str(bit_rate), + 'QUALITY': str(quality), + 'START_POINT': str(start_point), + 'END_POINT': str(end_point), + 'NORMALIZATION_LEVEL': str(normalization_level) + } + if(enable_metadata): + postdata['ENABLE_METADATA']='1' + else: + postdata['ENABLE_METADATA']='0' + + # + # Fetch the audio data + # + r=requests.post(self.__connection_url,data=postdata,stream=True) + if(r.status_code!=requests.codes.ok): + r.raise_for_status() + with open(filename,'wb') as handle: + for block in r.iter_content(1024): + handle.write(block) + def ListCart(self,cart_number,include_cuts=False): """ Returns the metadata associated with a Rivendell cart diff --git a/apis/rivwebpyapi/tests/Makefile.am b/apis/rivwebpyapi/tests/Makefile.am index 4f24e141..7028c62d 100644 --- a/apis/rivwebpyapi/tests/Makefile.am +++ b/apis/rivwebpyapi/tests/Makefile.am @@ -28,6 +28,7 @@ EXTRA_DIST = add_cart.py\ audio_store.py\ delete_audio.py\ delete_log.py\ + export.py\ list_cart.py\ list_cart_sched_codes.py\ list_carts.py\ diff --git a/apis/rivwebpyapi/tests/export.py b/apis/rivwebpyapi/tests/export.py new file mode 100755 index 00000000..2b7f8ebc --- /dev/null +++ b/apis/rivwebpyapi/tests/export.py @@ -0,0 +1,99 @@ +#!%PYTHON_BANGPATH% + +# export.py +# +# RivWebPyApi test script for Rivendell +# +# Test the Export Web API call +# +# (C) Copyright 2021 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 getpass +import rivwebpyapi +import sys + +url=''; +username='' +password='' +filename='' +cart_number=0 +cut_number=0 +audio_format=0 +channels=2 +sample_rate=48000 +bit_rate=0 +quality=0 +start_point=-1 +end_point=-1 +normalization_level=0 +enable_metadata=False + +# +# Get login parameters +# +usage='export --url= --username= --filename= --audio-format= --channels=1|2 --sample-rate= --bit-rate= --quality= --start-point= --end-point= --normalization-level= [--enable-metadata] --cart-number= --cut-number= [--password=]' +for arg in sys.argv: + f0=arg.split('=') + if(len(f0)==2): + if(f0[0]=='--url'): + url=f0[1] + if(f0[0]=='--username'): + username=f0[1] + if(f0[0]=='--password'): + password=f0[1] + if(f0[0]=='--filename'): + filename=f0[1] + if(f0[0]=='--cart-number'): + cart_number=int(f0[1]) + if(f0[0]=='--cut-number'): + cut_number=int(f0[1]) + if(f0[0]=='--audio-format'): + audio_format=int(f0[1]) + if(f0[0]=='--channels'): + channels=int(f0[1]) + if(f0[0]=='--sample-rate'): + sample_rate=int(f0[1]) + if(f0[0]=='--bit-rate'): + bit_rate=int(f0[1]) + if(f0[0]=='--quality'): + quality=int(f0[1]) + if(f0[0]=='--start-point'): + start_point=int(f0[1]) + if(f0[0]=='--end-point'): + end_point=int(f0[1]) + if(f0[0]=='--normalization-level'): + normalization_level=int(f0[1]) + if(len(f0)==1): + if(f0[0]=='--enable-metadata'): + enable_metadata=True + +if(not password): + password=getpass.getpass() +if((not url)or(not username)): + print(usage) + sys.exit(1) + +# +# Get the code list +# +webapi=rivwebpyapi.rivwebpyapi(url=url,username=username,password=password) +webapi.Export(filename=filename,cart_number=cart_number,cut_number=cut_number, + audio_format=audio_format,channels=channels, + sample_rate=sample_rate,bit_rate=bit_rate,quality=quality, + start_point=start_point,end_point=end_point, + normalization_level=normalization_level, + enable_metadata=enable_metadata)