// Example script for downloading recording files
var plivo = require('plivo');
var axios = require('axios');
var fs = require('fs');
var path = require('path');
const AUTH_ID = "<auth_id>";
const AUTH_TOKEN = "<auth_token>";
(function main() {
'use strict';
var client = new plivo.Client(AUTH_ID,AUTH_TOKEN);
// Directory where the recordings will be saved
var recordingsDir = path.join(__dirname, "recordings");
if (!fs.existsSync(recordingsDir)) {
fs.mkdirSync(recordingsDir, { recursive: true });
}
client.recordings.list(
{
add_time__gt: "2023-04-01 00:00:00",
add_time__lt: "2023-04-30 00:00:00",
offset: 0,
limit: 5,
},
).then(function (response) {
console.log("Found " + response.length + " recordings.");
response.forEach(recording => {
var recording_url = recording.recordingUrl;
var recording_id = recording.recordingId;
var format = recording.recordingFormat;
console.log("Downloading recording: " + recording_url);
var output_file = path.join(recordingsDir, recording_id + "." + format);
// Download the file
axios({
url: recording_url,
method: 'GET',
responseType: 'stream',
}).then(function (response) {
var fileStream = fs.createWriteStream(output_file);
response.data.pipe(fileStream);
fileStream.on('finish', function () {
console.log("Downloaded file to: " + output_file);
});
}).catch(function (error) {
console.log("Error downloading file: " + error.message);
});
});
}, function (err) {
console.error(err);
});
})();