spee.ch/helpers/lbryApi.js

133 lines
3.6 KiB
JavaScript
Raw Normal View History

const axios = require('axios');
2017-06-20 00:25:14 +02:00
const logger = require('winston');
2018-01-18 20:12:05 +01:00
const config = require('../config/speechConfig.js');
2018-01-23 21:41:39 +01:00
const { apiHost, apiPort } = config.api;
const lbryApiUri = 'http://' + apiHost + ':' + apiPort;
2017-06-20 00:25:14 +02:00
function handleLbrynetResponse ({ data }, resolve, reject) {
logger.debug('lbry api data:', data);
if (data.result) {
// check for an error
if (data.result.error) {
logger.debug('Lbrynet api error:', data.result.error);
reject(new Error(data.result.error));
return;
};
resolve(data.result);
return;
}
// fallback in case it just timed out
reject(JSON.stringify(data));
}
module.exports = {
publishClaim (publishParams) {
2017-08-03 20:46:56 +02:00
logger.debug(`lbryApi >> Publishing claim to "${publishParams.name}"`);
return new Promise((resolve, reject) => {
axios
2018-01-23 21:41:39 +01:00
.post(lbryApiUri, {
method: 'publish',
params: publishParams,
})
.then(response => {
handleLbrynetResponse(response, resolve, reject);
})
.catch(error => {
reject(error);
});
});
},
getClaim (uri) {
2017-08-03 20:46:56 +02:00
logger.debug(`lbryApi >> Getting Claim for "${uri}"`);
return new Promise((resolve, reject) => {
axios
2018-01-23 21:41:39 +01:00
.post(lbryApiUri, {
method: 'get',
2017-07-10 18:29:26 +02:00
params: { uri, timeout: 20 },
})
.then(response => {
handleLbrynetResponse(response, resolve, reject);
})
.catch(error => {
reject(error);
});
});
},
2017-08-15 22:48:42 +02:00
getClaimList (claimName) {
2017-08-04 20:32:21 +02:00
logger.debug(`lbryApi >> Getting claim_list for "${claimName}"`);
return new Promise((resolve, reject) => {
axios
2018-01-23 21:41:39 +01:00
.post(lbryApiUri, {
method: 'claim_list',
params: { name: claimName },
})
.then(response => {
handleLbrynetResponse(response, resolve, reject);
})
.catch(error => {
reject(error);
});
});
},
resolveUri (uri) {
2017-08-03 20:46:56 +02:00
logger.debug(`lbryApi >> Resolving URI for "${uri}"`);
return new Promise((resolve, reject) => {
axios
2018-01-23 21:41:39 +01:00
.post(lbryApiUri, {
method: 'resolve',
params: { uri },
})
.then(({ data }) => {
if (data.result[uri].error) { // check for errors
reject(data.result[uri].error);
} else { // if no errors, resolve
resolve(data.result[uri]);
}
})
.catch(error => {
reject(error);
});
});
},
getDownloadDirectory () {
2017-08-03 20:46:56 +02:00
logger.debug('lbryApi >> Retrieving the download directory path from lbry daemon...');
return new Promise((resolve, reject) => {
axios
2018-01-23 21:41:39 +01:00
.post(lbryApiUri, {
method: 'settings_get',
})
.then(({ data }) => {
if (data.result) {
resolve(data.result.download_directory);
} else {
2017-09-14 18:35:58 +02:00
return new Error('Successfully connected to lbry daemon, but unable to retrieve the download directory.');
}
})
2017-09-14 18:35:58 +02:00
.catch(error => {
logger.error('Lbrynet Error:', error);
resolve('/home/lbry/Downloads/');
});
});
},
2017-09-26 07:49:27 +02:00
createChannel (name) {
2017-09-17 02:50:22 +02:00
return new Promise((resolve, reject) => {
axios
2018-01-23 21:41:39 +01:00
.post(lbryApiUri, {
method: 'channel_new',
params: {
channel_name: name,
amount : 0.1,
},
})
.then(response => {
2017-10-17 21:12:39 +02:00
logger.verbose('createChannel response:', response);
handleLbrynetResponse(response, resolve, reject);
})
2017-09-26 07:49:27 +02:00
.catch(error => {
2017-10-17 21:12:39 +02:00
logger.error('createChannel error:', error);
2017-09-26 07:49:27 +02:00
reject(error);
});
2017-09-17 02:50:22 +02:00
});
},
};