revised lbry api routes

This commit is contained in:
bill bittner 2017-06-13 15:39:38 -07:00
parent 25500a592e
commit 7b4345aefb
5 changed files with 255 additions and 172 deletions

View file

@ -1,72 +1,7 @@
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;
}
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;
}
}
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;
}
function getClaimWithUri(uri, resolve, reject){
console.log(">> making get request to lbry daemon");
axios.post('http://localhost:5279/lbryapi', {
"method": "get",
"params": { "uri": uri }
}
).then(function (getUriResponse) {
console.log(">> 'get claim' success...");
//check to make sure the daemon didn't just time out
if (getUriResponse.data.result.error){
reject(getUriResponse.data.result.error);
}
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
/*
note: put in a check to make sure we do not resolve until the download is actually complete
*/
resolve(getUriResponse.data.result.download_path);
}).catch(function(getUriError){
console.log(">> 'get' error.");
// reject the promise with an error message
reject(getUriError);
return;
});
}
function findAllClaims(name, resolve, reject){
// to do: abstract claim_list function to here
}
module.exports = {
publishClaim: function(publishParams){
console.log("publish params:>", publishParams);
var deferred = new Promise(function(resolve, reject){
@ -75,113 +10,44 @@ module.exports = {
"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");
reject(error);
})
})
return deferred;
},
getClaimBasedOnNameOnly: function(claimName){
var deferred = new Promise(function (resolve, reject){
// make a call to the daemon to get the claims list
getClaimsList: function(claimName){
var deferred = new Promise(function(resolve, reject){
console.log(">> claims_list for", claimName)
axios.post('http://localhost:5279/lbryapi', {
"method": "claim_list",
"params": { "name": claimName }
})
.then(function (response) {
console.log(">> 'claim_list' success");
var claimsList = response.data.result.claims;
console.log(">> Number of claims:", claimsList.length)
// return early if no claims were found
if (claimsList.length === 0){
reject("NO_CLAIMS");
console.log("exiting due to lack of claims");
return;
}
// filter the claims to return only free, public claims
var freePublicClaims = filterForFreePublicClaims(claimsList);
// return early if no free, public claims were found
if (!freePublicClaims || (freePublicClaims.length === 0)){
reject("NO_FREE_PUBLIC_CLAIMS");
console.log("exiting due to lack of free or public claims");
return;
}
// order the claims
var orderedPublicClaims = orderTopClaims(freePublicClaims);
// create the uri for the first (selected) claim
console.log(">> ordered free public claims");
var freePublicClaimUri = orderedPublicClaims[0].name + "#" + orderedPublicClaims[0].claim_id;
console.log(">> your free public claim URI:", freePublicClaimUri);
// fetch the image to display
getClaimWithUri(freePublicClaimUri, resolve, reject);
})
.catch(function(error){
console.log(">> 'claim_list' error.");
method: "claim_list",
params: { name: claimName }
}).then(function (response) {
console.log(">> claim_list success");
resolve(response.data);
}).catch(function(error){
console.log(">> claim_list error");
reject(error);
});
});
return deferred;
},
getClaimBasedOnUri: function(uri){
/*
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.
*/
var deferred = new Promise(function (resolve, reject){
console.log(">> get claim based on URI:", uri);
// fetch the image to display
getClaimWithUri(uri, resolve, reject);
});
return deferred;
},
getAllClaims: function(claimName, res){ // note: work in progress
resolveUri: function(uri){
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
console.log(">> resolve uri for", uri)
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)
console.log(">> 'claim_list' success");
var claimsList = response.data.result.claims;
console.log(">> Number of claims:", claimsList.length)
// return early if no claims were found
if (claimsList.length === 0){
reject("NO_CLAIMS");
console.log("exiting due to lack of claims");
return;
}
// filter the claims to return only free, public claims
var freePublicClaims = filterForFreePublicClaims(claimsList);
// return early if no free, public claims were found
if (!freePublicClaims || (freePublicClaims.length === 0)){
reject("NO_FREE_PUBLIC_CLAIMS");
console.log("exiting due to lack of free or public claims");
return;
}
// order the claims
var orderedPublicClaims = orderTopClaims(freePublicClaims);
// serve the response
/*
to do: rather than returning json, serve a page of all these claims
*/
resolve(orderedPublicClaims);
"method": "resolve",
"params": { "uri": uri}
}).then(function(response){
console.log(response.data);
resolve(response.data);
}).catch(function(error){
console.log(">> 'claim_list' error");
console.log(">> 'resolve' error");
reject(error);
})
});
});
return deferred;
}

213
helpers/lbryHelpers.js Normal file
View file

@ -0,0 +1,213 @@
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;
}
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;
}
}
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;
}
function getClaimWithUri(uri, resolve, reject){
console.log(">> making get request to lbry daemon");
axios.post('http://localhost:5279/lbryapi', {
"method": "get",
"params": { "uri": uri }
}
).then(function (getUriResponse) {
console.log(">> 'get claim' success...");
//check to make sure the daemon didn't just time out
if (getUriResponse.data.result.error){
reject(getUriResponse.data.result.error);
}
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
/*
note: put in a check to make sure we do not resolve until the download is actually complete
*/
resolve(getUriResponse.data.result.download_path);
}).catch(function(getUriError){
console.log(">> 'get' error.");
// reject the promise with an error message
reject(getUriError);
return;
});
}
function findAllClaims(name, resolve, reject){
// to do: abstract claim_list function to here
}
module.exports = {
publishClaim: function(publishParams){
console.log("publish params:>", publishParams);
var deferred = new Promise(function(resolve, reject){
axios.post('http://localhost:5279/lbryapi', {
"method": "publish",
"params": publishParams
})
.then(function (response) {
console.log(">> 'publish' success");
resolve(response.data);
}).catch(function(error){
console.log(">> 'publish' error");
reject(error);
})
})
return deferred;
},
getClaimBasedOnNameOnly: function(claimName){
var deferred = new Promise(function (resolve, reject){
// 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");
var claimsList = response.data.result.claims;
console.log(">> Number of claims:", claimsList.length)
// return early if no claims were found
if (claimsList.length === 0){
reject("NO_CLAIMS");
console.log("exiting due to lack of claims");
return;
}
// filter the claims to return only free, public claims
var freePublicClaims = filterForFreePublicClaims(claimsList);
// return early if no free, public claims were found
if (!freePublicClaims || (freePublicClaims.length === 0)){
reject("NO_FREE_PUBLIC_CLAIMS");
console.log("exiting due to lack of free or public claims");
return;
}
// order the claims
var orderedPublicClaims = orderTopClaims(freePublicClaims);
// create the uri for the first (selected) claim
console.log(">> ordered free public claims");
var freePublicClaimUri = orderedPublicClaims[0].name + "#" + orderedPublicClaims[0].claim_id;
console.log(">> your free public claim URI:", freePublicClaimUri);
// fetch the image to display
getClaimWithUri(freePublicClaimUri, resolve, reject);
})
.catch(function(error){
console.log(">> 'claim_list' error.");
reject(error);
});
});
return deferred;
},
getClaimBasedOnUri: function(uri){
/*
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.
*/
var deferred = new Promise(function (resolve, reject){
console.log(">> get claim based on URI:", uri);
// fetch the image to display
getClaimWithUri(uri, resolve, reject);
});
return deferred;
},
getAllFreePublicClaims: 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");
var claimsList = response.data.result.claims;
console.log(">> Number of claims:", claimsList.length)
// return early if no claims were found
if (claimsList.length === 0){
reject("NO_CLAIMS");
console.log("exiting due to lack of claims");
return;
}
// filter the claims to return only free, public claims
var freePublicClaims = filterForFreePublicClaims(claimsList);
// return early if no free, public claims were found
if (!freePublicClaims || (freePublicClaims.length === 0)){
reject("NO_FREE_PUBLIC_CLAIMS");
console.log("exiting due to lack of free or public claims");
return;
}
// order the claims
var orderedPublicClaims = orderTopClaims(freePublicClaims);
// resolve the promise
resolve(orderedPublicClaims);
}).catch(function(error){
console.log(">> 'claim_list' error");
reject(error);
});
});
return deferred;
},
getClaimsList: function(claimName){
var deferred = new Promise(function(resolve, reject){
console.log(">> claims_list for", claimName)
axios.post('http://localhost:5279/lbryapi', {
method: "claim_list",
params: { name: claimName }
}).then(function (response) {
console.log(">> claim_list success");
resolve(response.data);
}).catch(function(error){
console.log(">> claim_list error");
reject(error);
});
});
return deferred;
},
resolveUri: function(uri){
var deferred = new Promise(function(resolve, reject){
console.log(">> resolve uri for", uri)
axios.post('http://localhost:5279/lbryapi', {
"method": "resolve",
"params": { "uri": uri}
}).then(function(response){
console.log(response.data);
resolve(response.data);
}).catch(function(error){
console.log(">> 'resolve' error");
reject(error);
});
});
return deferred;
}
}

View file

@ -1,21 +1,25 @@
module.exports = function(app, axios){
// route to return claim list in json
app.get("/api/claim_list/:claim", function(req, res){
var claim = req.params.claim;
// make a call to the daemon
axios.post('http://localhost:5279/lbryapi', { // to do: extrapolate into lbryApi 'claim list' method and call that
method: "claim_list",
params: {
name: claim
}
}
).then(function (response) {
console.log("success");
res.send(response.data);
}).catch(function(error){
console.log(error.data);
res.send(error.data);
module.exports = function(app, routeHelpers, lbryApi){
// route to run a claim_list request on the daemon
app.get("/api/claim_list/:claim", function(req, res){
lbryApi.getClaimsList(req.params.claim)
.then(function(orderedFreePublicImages){
console.log("/api/claim_list/:claim success.");
res.status(200).json(orderedFreePublicImages);
})
.catch(function(error){
console.log("/api/claim_list/:name error:", error);
routeHelpers.handleRequestError(error, res);
});
});
// route to run a resolve request on the daemon
app.get("/api/resolve/:uri", function(req, res){
lbryApi.resolveUri(req.params.uri)
.then(function(resolvedUri){
console.log("/api/resolve/:claim success.");
res.status(200).json(resolvedUri);
}).catch(function(error){
routeHelpers.handleRequestError(error, res);
});
});
}

View file

@ -5,7 +5,7 @@ module.exports = function(app, routeHelpers, lbryApi, ua, googleAnalyticsId){
console.log(">> GET request on /" + req.params.name + " (all)");
ua(googleAnalyticsId, {https: true}).event("Show Routes", "/name/all", req.params.name + "/all").send();
// create promise
lbryApi.getAllClaims(req.params.name)
lbryApi.getAllFreePublicClaims(req.params.name)
.then(function(orderedFreePublicClaims){
console.log("/:name/all success.");
res.status(200).render('allClaims', { claims: orderedFreePublicClaims });

View file

@ -51,7 +51,7 @@ app.engine('handlebars', hbs.engine);
app.set('view engine', 'handlebars');
// require express routes
require("./routes/api-routes.js")(app, axios);
require("./routes/api-routes.js")(app, routeHelpers, lbryApi);
require("./routes/show-routes.js")(app, routeHelpers, lbryApi, ua, googleAnalyticsId);
require("./routes/serve-routes.js")(app, routeHelpers, lbryApi, ua, googleAnalyticsId);
require("./routes/home-routes.js")(app);