spee.ch/helpers/libraries/lbryApi.js

102 lines
3.1 KiB
JavaScript
Raw Normal View History

var axios = require('axios');
var db = require("../../models");
module.exports = {
2017-06-17 04:31:31 +02:00
publishClaim: function(publishParams, fileName, fileType){
2017-06-01 04:48:09 +02:00
var deferred = new Promise(function(resolve, reject){
console.log(">> lbryApi >> publishClaim:", publishParams);
2017-06-01 04:48:09 +02:00
axios.post('http://localhost:5279/lbryapi', {
"method": "publish",
"params": publishParams
}).then(function (response) {
2017-06-17 04:31:31 +02:00
console.log(">> 'publish' success", response);
var result = response.data.result;
2017-06-16 19:07:31 +02:00
db.File.create({
name: publishParams.name,
claim_id: result.claim_id,
outpoint: result.txid + ":" + result.nout,
2017-06-17 04:31:31 +02:00
file_name: fileName,
file_path: publishParams.file_path,
2017-06-16 19:07:31 +02:00
file_type: fileType,
nsfw: publishParams.metadata.nsfw,
2017-06-16 19:07:31 +02:00
}).catch(function(error){
console.log('An error occurred when writing to the MySQL database:', error);
});
resolve(result);
2017-06-01 04:48:09 +02:00
}).catch(function(error){
console.log(">> 'publish' error");
2017-06-03 05:56:33 +02:00
reject(error);
});
});
return deferred;
},
2017-06-14 02:52:29 +02:00
getClaim: function(uri){
var deferred = new Promise(function(resolve, reject){
console.log(">> lbryApi >> getClaim:", uri);
axios.post('http://localhost:5279/lbryapi', {
"method": "get",
"params": { "uri": uri, "timeout": 20}
2017-06-16 19:07:31 +02:00
}).then(function (response) {
console.log(">> 'get' success");
2017-06-16 00:56:40 +02:00
//check to make sure the daemon didn't just time out
2017-06-16 19:07:31 +02:00
if (response.data.result.error){
reject(response.data.result.error);
2017-06-14 02:52:29 +02:00
}
/*
2017-06-14 02:52:29 +02:00
note: put in a check to make sure we do not resolve until the download is actually complete (response.data.completed === true)
*/
// save a record of the file to the Files table
var result = response.data.result
db.File.create({
name: result.name,
claim_id: result.claim_id,
outpoint: result.outpoint,
file_name: result.file_name,
file_path: result.download_path,
file_type: result.mime_type,
nsfw: result.metadata.stream.metadata.nsfw,
}).catch(function(error){
2017-06-16 19:07:31 +02:00
console.log('An error occurred when writing to the MySQL database:', error);
});
resolve(result);
2017-06-16 19:07:31 +02:00
}).catch(function(error){
console.log(">> 'get' error");
2017-06-16 19:07:31 +02:00
reject(error);
});
});
2017-06-01 04:48:09 +02:00
return deferred;
},
2017-06-14 00:39:38 +02:00
getClaimsList: function(claimName){
var deferred = new Promise(function(resolve, reject){
console.log(">> lbryApi >> getClaimList:", claimName);
2017-06-01 05:54:56 +02:00
axios.post('http://localhost:5279/lbryapi', {
2017-06-14 00:39:38 +02:00
method: "claim_list",
params: { name: claimName }
}).then(function (response) {
console.log(">> 'claim_list' success");
resolve(response.data.result);
2017-06-14 00:39:38 +02:00
}).catch(function(error){
console.log(">> 'claim_list' error");
2017-06-03 05:56:33 +02:00
reject(error);
});
});
return deferred;
},
2017-06-14 00:39:38 +02:00
resolveUri: function(uri){
2017-06-01 01:44:45 +02:00
var deferred = new Promise(function(resolve, reject){
2017-06-14 22:07:30 +02:00
console.log(">> lbryApi >> resolveUri:", uri);
2017-06-01 01:44:45 +02:00
axios.post('http://localhost:5279/lbryapi', {
2017-06-14 00:39:38 +02:00
"method": "resolve",
"params": { "uri": uri}
}).then(function(response){
console.log(">> 'resolve' success");
2017-06-16 21:29:02 +02:00
resolve(response.data.result);
2017-06-01 01:44:45 +02:00
}).catch(function(error){
2017-06-14 00:39:38 +02:00
console.log(">> 'resolve' error");
2017-06-03 05:56:33 +02:00
reject(error);
2017-06-14 00:39:38 +02:00
});
2017-06-01 01:44:45 +02:00
});
2017-06-01 02:00:13 +02:00
return deferred;
}
}