2017-05-24 20:07:43 +02:00
|
|
|
var path = require('path');
|
|
|
|
var axios = require('axios');
|
|
|
|
|
|
|
|
function filterForFreePublicClaims(claimsListArray){
|
|
|
|
if (!claimsListArray) {
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
var freePublicClaims = claimsListArray.filter(function(claim){
|
|
|
|
return (((claim.value.stream.metadata.license.indexOf('Public Domain') != -1) || (claim.value.stream.metadata.license.indexOf('Creative Commons') != -1)) &&
|
|
|
|
(!claim.value.stream.metadata.fee || claim.value.stream.metadata.fee === 0));
|
|
|
|
});
|
|
|
|
return freePublicClaims;
|
|
|
|
}
|
2017-06-03 09:41:02 +02:00
|
|
|
|
2017-05-24 20:07:43 +02:00
|
|
|
function isFreePublicClaim(claim){
|
|
|
|
console.log(">> isFreePublicClaim, claim:", claim);
|
|
|
|
if ((claim.value.stream.metadata.license === 'Public Domain' || claim.value.stream.metadata.license === 'Creative Commons') &&
|
|
|
|
(!claim.value.stream.metadata.fee || claim.value.stream.metadata.fee.amount === 0)) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2017-06-03 09:41:02 +02:00
|
|
|
|
2017-05-24 20:07:43 +02:00
|
|
|
function orderTopClaims(claimsListArray){
|
|
|
|
console.log(">> orderTopClaims, claimsListArray:");
|
|
|
|
claimsListArray.sort(function(claimA, claimB){
|
|
|
|
if (claimA.amount === claimB.amount){
|
|
|
|
return (claimA.height > claimB.height);
|
|
|
|
} else {
|
|
|
|
return (claimA.amount < claimB.amount);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return claimsListArray;
|
|
|
|
}
|
|
|
|
|
2017-05-26 09:34:25 +02:00
|
|
|
function getClaimWithUri(uri, resolve, reject){
|
2017-06-01 01:44:45 +02:00
|
|
|
console.log(">> making get request to lbry daemon");
|
2017-05-26 09:34:25 +02:00
|
|
|
axios.post('http://localhost:5279/lbryapi', {
|
2017-06-01 05:54:56 +02:00
|
|
|
"method": "get",
|
|
|
|
"params": { "uri": uri }
|
2017-05-26 09:34:25 +02:00
|
|
|
}
|
|
|
|
).then(function (getUriResponse) {
|
|
|
|
console.log(">> 'get claim' success...");
|
2017-05-26 10:25:07 +02:00
|
|
|
//check to make sure the daemon didn't just time out
|
2017-06-03 07:19:47 +02:00
|
|
|
if (getUriResponse.data.result.error){
|
|
|
|
reject(getUriResponse.data.result.error);
|
2017-05-26 10:25:07 +02:00
|
|
|
}
|
2017-05-26 09:34:25 +02:00
|
|
|
console.log(">> response data:", getUriResponse.data);
|
|
|
|
console.log(">> dl path =", getUriResponse.data.result.download_path)
|
|
|
|
// resolve the promise with the download path for the claim we got
|
2017-05-26 10:25:07 +02:00
|
|
|
/*
|
|
|
|
note: put in a check to make sure we do not resolve until the download is actually complete
|
|
|
|
*/
|
2017-05-26 09:34:25 +02:00
|
|
|
resolve(getUriResponse.data.result.download_path);
|
|
|
|
}).catch(function(getUriError){
|
2017-06-03 07:19:47 +02:00
|
|
|
console.log(">> 'get' error.");
|
2017-05-26 09:34:25 +02:00
|
|
|
// reject the promise with an error message
|
2017-06-03 05:56:33 +02:00
|
|
|
reject(getUriError);
|
2017-05-26 10:25:07 +02:00
|
|
|
return;
|
2017-05-26 09:34:25 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-06-01 01:44:45 +02:00
|
|
|
function findAllClaims(name, resolve, reject){
|
2017-06-01 05:54:56 +02:00
|
|
|
// to do: abstract claim_list function to here
|
2017-06-01 01:44:45 +02:00
|
|
|
}
|
|
|
|
|
2017-05-24 20:07:43 +02:00
|
|
|
module.exports = {
|
2017-05-26 09:34:25 +02:00
|
|
|
|
2017-06-01 04:48:09 +02:00
|
|
|
publishClaim: function(publishParams){
|
2017-06-12 21:07:01 +02:00
|
|
|
console.log("publish params:>", publishParams);
|
2017-06-01 04:48:09 +02:00
|
|
|
var deferred = new Promise(function(resolve, reject){
|
|
|
|
axios.post('http://localhost:5279/lbryapi', {
|
|
|
|
"method": "publish",
|
|
|
|
"params": publishParams
|
|
|
|
})
|
|
|
|
.then(function (response) {
|
|
|
|
// receive resonse from LBRY
|
|
|
|
console.log(">> 'publish' success");
|
|
|
|
// return the claim we got
|
|
|
|
resolve(response.data);
|
|
|
|
}).catch(function(error){
|
|
|
|
// receive response from LBRY
|
|
|
|
console.log(">> 'publish' error");
|
2017-06-03 05:56:33 +02:00
|
|
|
reject(error);
|
2017-06-01 04:48:09 +02:00
|
|
|
})
|
2017-05-24 20:07:43 +02:00
|
|
|
})
|
2017-06-01 04:48:09 +02:00
|
|
|
return deferred;
|
2017-05-24 20:07:43 +02:00
|
|
|
},
|
2017-05-26 09:34:25 +02:00
|
|
|
|
2017-05-25 09:59:22 +02:00
|
|
|
getClaimBasedOnNameOnly: function(claimName){
|
|
|
|
var deferred = new Promise(function (resolve, reject){
|
|
|
|
// make a call to the daemon to get the claims list
|
2017-06-01 05:54:56 +02:00
|
|
|
axios.post('http://localhost:5279/lbryapi', {
|
|
|
|
"method": "claim_list",
|
|
|
|
"params": { "name": claimName }
|
2017-05-25 09:59:22 +02:00
|
|
|
})
|
|
|
|
.then(function (response) {
|
2017-06-01 01:44:45 +02:00
|
|
|
console.log(">> 'claim_list' success");
|
2017-05-26 09:34:25 +02:00
|
|
|
var claimsList = response.data.result.claims;
|
|
|
|
console.log(">> Number of claims:", claimsList.length)
|
2017-05-25 09:59:22 +02:00
|
|
|
// return early if no claims were found
|
2017-05-26 09:34:25 +02:00
|
|
|
if (claimsList.length === 0){
|
2017-06-01 01:44:45 +02:00
|
|
|
reject("NO_CLAIMS");
|
2017-05-26 09:34:25 +02:00
|
|
|
console.log("exiting due to lack of claims");
|
2017-05-25 09:59:22 +02:00
|
|
|
return;
|
2017-05-24 20:07:43 +02:00
|
|
|
}
|
2017-05-26 09:34:25 +02:00
|
|
|
// filter the claims to return only free, public claims
|
|
|
|
var freePublicClaims = filterForFreePublicClaims(claimsList);
|
2017-05-25 09:59:22 +02:00
|
|
|
// return early if no free, public claims were found
|
|
|
|
if (!freePublicClaims || (freePublicClaims.length === 0)){
|
2017-06-01 01:44:45 +02:00
|
|
|
reject("NO_FREE_PUBLIC_CLAIMS");
|
2017-05-26 09:34:25 +02:00
|
|
|
console.log("exiting due to lack of free or public claims");
|
2017-05-25 09:59:22 +02:00
|
|
|
return;
|
2017-05-24 20:07:43 +02:00
|
|
|
}
|
2017-05-25 09:59:22 +02:00
|
|
|
// order the claims
|
2017-06-01 04:48:09 +02:00
|
|
|
var orderedPublicClaims = orderTopClaims(freePublicClaims);
|
2017-05-25 09:59:22 +02:00
|
|
|
// create the uri for the first (selected) claim
|
2017-06-01 01:44:45 +02:00
|
|
|
console.log(">> ordered free public claims");
|
2017-06-01 05:54:56 +02:00
|
|
|
var freePublicClaimUri = orderedPublicClaims[0].name + "#" + orderedPublicClaims[0].claim_id;
|
2017-06-01 01:44:45 +02:00
|
|
|
console.log(">> your free public claim URI:", freePublicClaimUri);
|
2017-05-25 09:59:22 +02:00
|
|
|
// fetch the image to display
|
2017-05-26 09:34:25 +02:00
|
|
|
getClaimWithUri(freePublicClaimUri, resolve, reject);
|
2017-05-24 20:07:43 +02:00
|
|
|
})
|
2017-05-25 09:59:22 +02:00
|
|
|
.catch(function(error){
|
2017-06-03 05:56:33 +02:00
|
|
|
console.log(">> 'claim_list' error.");
|
|
|
|
reject(error);
|
2017-05-25 09:59:22 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
return deferred;
|
2017-05-24 20:07:43 +02:00
|
|
|
},
|
2017-05-26 09:34:25 +02:00
|
|
|
|
|
|
|
getClaimBasedOnUri: function(uri){
|
2017-05-24 20:07:43 +02:00
|
|
|
/*
|
|
|
|
to do: need to pass the URI through a test (use 'resolve') to see if it is free and public. Right now it is jumping straight to 'get'ing and serving the asset.
|
|
|
|
*/
|
2017-05-26 09:34:25 +02:00
|
|
|
var deferred = new Promise(function (resolve, reject){
|
2017-06-01 01:44:45 +02:00
|
|
|
console.log(">> get claim based on URI:", uri);
|
2017-05-26 09:34:25 +02:00
|
|
|
// fetch the image to display
|
|
|
|
getClaimWithUri(uri, resolve, reject);
|
|
|
|
});
|
|
|
|
return deferred;
|
2017-05-24 20:07:43 +02:00
|
|
|
|
|
|
|
},
|
2017-05-26 09:34:25 +02:00
|
|
|
|
2017-06-01 01:44:45 +02:00
|
|
|
getAllClaims: function(claimName, res){ // note: work in progress
|
|
|
|
var deferred = new Promise(function(resolve, reject){
|
|
|
|
console.log(">> get all claims data for", claimName)
|
|
|
|
// make a call to the daemon to get the claims list
|
|
|
|
axios.post('http://localhost:5279/lbryapi', {
|
|
|
|
method: "claim_list",
|
|
|
|
params: { name: claimName }
|
|
|
|
}
|
|
|
|
).then(function (response) {
|
|
|
|
console.log(">> 'claim_list' success");
|
|
|
|
console.log(">> Number of claims:", response.data.result.claims.length)
|
2017-06-01 02:00:13 +02:00
|
|
|
console.log(">> 'claim_list' success");
|
|
|
|
var claimsList = response.data.result.claims;
|
|
|
|
console.log(">> Number of claims:", claimsList.length)
|
2017-06-01 01:44:45 +02:00
|
|
|
// return early if no claims were found
|
2017-06-01 02:00:13 +02:00
|
|
|
if (claimsList.length === 0){
|
|
|
|
reject("NO_CLAIMS");
|
|
|
|
console.log("exiting due to lack of claims");
|
2017-06-01 01:44:45 +02:00
|
|
|
return;
|
|
|
|
}
|
2017-06-01 02:00:13 +02:00
|
|
|
// filter the claims to return only free, public claims
|
|
|
|
var freePublicClaims = filterForFreePublicClaims(claimsList);
|
2017-06-01 01:44:45 +02:00
|
|
|
// return early if no free, public claims were found
|
|
|
|
if (!freePublicClaims || (freePublicClaims.length === 0)){
|
2017-06-01 02:00:13 +02:00
|
|
|
reject("NO_FREE_PUBLIC_CLAIMS");
|
|
|
|
console.log("exiting due to lack of free or public claims");
|
2017-06-01 01:44:45 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// order the claims
|
2017-06-01 04:48:09 +02:00
|
|
|
var orderedPublicClaims = orderTopClaims(freePublicClaims);
|
2017-06-01 01:44:45 +02:00
|
|
|
// serve the response
|
|
|
|
/*
|
|
|
|
to do: rather than returning json, serve a page of all these claims
|
|
|
|
*/
|
2017-06-01 02:00:13 +02:00
|
|
|
resolve(orderedPublicClaims);
|
2017-06-01 01:44:45 +02:00
|
|
|
}).catch(function(error){
|
2017-06-03 05:56:33 +02:00
|
|
|
console.log(">> 'claim_list' error");
|
|
|
|
reject(error);
|
2017-06-01 01:44:45 +02:00
|
|
|
})
|
|
|
|
});
|
2017-06-01 02:00:13 +02:00
|
|
|
return deferred;
|
2017-05-24 20:07:43 +02:00
|
|
|
}
|
|
|
|
}
|