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