yemot-recordings-backup
Version:
Automatic backup tool for Yemot HaMashiach call recordings to Amazon S3
97 lines (88 loc) • 2.68 kB
JavaScript
const axios = require('axios');
const logger = require('./logger');
class YemotApi {
constructor(username, password, baseUrl = 'https://www.call2all.co.il/ym/api') {
this.username = username;
this.password = password;
this.baseUrl = baseUrl;
this.token = null;
}
/**
* Get token for API requests
* @returns {string} token
*/
async getToken() {
// If we already have a token, return it
if (this.token) {
return this.token;
}
// Otherwise, create a simple token from username:password
this.token = `${this.username}:${this.password}`;
return this.token;
}
/**
* Get directory contents
* @param {string} path - Directory path
* @returns {object} Directory contents
*/
async getDirectoryContents(path) {
try {
const token = await this.getToken();
const url = `${this.baseUrl}/GetIVR2Dir`;
const response = await axios.get(url, {
params: {
token,
path,
orderBy: 'date',
orderDir: 'desc'
}
});
if(response.data.responseStatus == "OK"){
return response.data.responseData;
}else{
logger.error(`Failed to get directory contents for ${path}: ${JSON.stringify(response.data)}`);
throw new Error(JSON.stringify(response.data));
}
} catch (error) {
logger.error(`Failed to get directory contents for ${path}: ${error.message}`);
throw error;
}
}
/**
* Download a file
* @param {string} path - File path
* @returns {Buffer} File content
*/
async downloadFile(path) {
try {
const token = await this.getToken();
const url = `${this.baseUrl}/DownloadFile`;
const response = await axios.get(url, {
params: {
token,
path
},
responseType: 'arraybuffer'
});
if(response.data.responseStatus == "OK"){
return response.data;
}else{
logger.error(`Failed to download file ${path}: ${JSON.stringify(response.data)}`);
throw new Error(JSON.stringify(response.data));
}
} catch (error) {
logger.error(`Failed to download file ${path}: ${error.message}`);
throw error;
}
}
/**
* Check if path is a recording (audio file)
* @param {string} fileName - File name
* @returns {boolean}
*/
isRecording(fileName) {
const audioExtensions = ['.mp3', '.wav', '.ogg', '.aac', '.m4a'];
return audioExtensions.some(ext => fileName.toLowerCase().endsWith(ext));
}
}
module.exports = YemotApi;