spee.ch/server/lbrynet/index.js

145 lines
4.6 KiB
JavaScript
Raw Normal View History

const axios = require('axios');
2017-06-20 00:25:14 +02:00
const logger = require('winston');
2018-06-06 04:34:24 +02:00
const { apiHost, apiPort } = require('@config/lbryConfig');
const lbrynetUri = 'http://' + apiHost + ':' + apiPort;
const { chooseGaLbrynetPublishLabel, sendGATimingEvent } = require('../utils/googleAnalytics.js');
const handleLbrynetResponse = require('./utils/handleLbrynetResponse.js');
module.exports = {
publishClaim (publishParams) {
2017-08-03 20:46:56 +02:00
logger.debug(`lbryApi >> Publishing claim to "${publishParams.name}"`);
const gaStartTime = Date.now();
return new Promise((resolve, reject) => {
axios
.post(lbrynetUri, {
method: 'publish',
params: publishParams,
})
.then(response => {
2018-03-06 18:39:22 +01:00
sendGATimingEvent('lbrynet', 'publish', chooseGaLbrynetPublishLabel(publishParams), gaStartTime, Date.now());
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}"`);
const gaStartTime = Date.now();
return new Promise((resolve, reject) => {
axios
.post(lbrynetUri, {
method: 'get',
2017-07-10 18:29:26 +02:00
params: { uri, timeout: 20 },
})
.then(response => {
sendGATimingEvent('lbrynet', 'getClaim', 'GET', gaStartTime, Date.now());
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}"`);
const gaStartTime = Date.now();
return new Promise((resolve, reject) => {
axios
.post(lbrynetUri, {
method: 'claim_list',
params: { name: claimName },
})
.then(response => {
sendGATimingEvent('lbrynet', 'getClaimList', 'CLAIM_LIST', gaStartTime, Date.now());
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}"`);
const gaStartTime = Date.now();
return new Promise((resolve, reject) => {
axios
.post(lbrynetUri, {
method: 'resolve',
params: { uri },
})
.then(({ data }) => {
sendGATimingEvent('lbrynet', 'resolveUri', 'RESOLVE', gaStartTime, Date.now());
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...');
const gaStartTime = Date.now();
return new Promise((resolve, reject) => {
axios
.post(lbrynetUri, {
method: 'settings_get',
})
.then(({ data }) => {
sendGATimingEvent('lbrynet', 'getDownloadDirectory', 'SETTINGS_GET', gaStartTime, Date.now());
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) {
logger.debug(`lbryApi >> Creating channel for ${name}...`);
const gaStartTime = Date.now();
2017-09-17 02:50:22 +02:00
return new Promise((resolve, reject) => {
axios
.post(lbrynetUri, {
method: 'channel_new',
params: {
channel_name: name,
amount : 0.1,
},
})
.then(response => {
sendGATimingEvent('lbrynet', 'createChannel', 'CHANNEL_NEW', gaStartTime, Date.now());
handleLbrynetResponse(response, resolve, reject);
})
2017-09-26 07:49:27 +02:00
.catch(error => {
reject(error);
});
2017-09-17 02:50:22 +02:00
});
},
getWalletBalance () {
const gaStartTime = Date.now();
return new Promise((resolve, reject) => {
axios
.post(lbrynetUri, {
method: 'wallet_balance',
})
.then(response => {
sendGATimingEvent('lbrynet', 'getWalletBalance', 'SETTINGS_GET', gaStartTime, Date.now());
handleLbrynetResponse(response, resolve, reject);
})
.catch(error => {
reject(error);
});
});
},
};