sparsejs
Version:
Package that allows you to easily use the sparse api within js.
172 lines (136 loc) • 4.84 kB
JavaScript
let sparse = {}
let reqPerMin = 3;
let currentMinReq = 0;
let data;
const fetch = require('node-fetch')
sparse.song = async function() {
if (!validKey) {return "Invalid key!"}
watchTower()
if (limitUser) {return "Slow Down! Please do not create more then " + reqPerMin + " request per seccond!"}
await fetch('https://sparse.pw/api/song?key=' + apiKey)
.then(res => res.json()).then(body => {
if (!body) {return "No output received from API!"}
data = body.data
})
return data;
}
sparse.cat = async function() {
if (!validKey) {return "Invalid key!"}
watchTower()
if (limitUser) {return "Slow Down! Please do not create more then " + reqPerMin + " request per seccond!"}
await fetch('https://sparse.pw/api/cat?key=' + apiKey)
.then(res => res.json()).then(body => {
if (!body) {return "No output received from API!"}
data = body.data
})
return data;
}
sparse.insult = async function() {
if (!validKey) {return "Invalid key!"}
watchTower()
if (limitUser) {return "Slow Down! Please do not create more then " + reqPerMin + " request per seccond!"}
await fetch('https://sparse.pw/api/insult?key=' + apiKey)
.then(res => res.json()).then(body => {
if (!body) {return "No output received from API!"}
data = body.output
})
return data;
}
sparse.dog = async function() {
if (!validKey) {return "Invalid key!"}
watchTower()
if (limitUser) {return "Slow Down! Please do not create more then " + reqPerMin + " request per seccond!"}
await fetch('https://sparse.pw/api/dog?key=' + apiKey)
.then(res => res.json()).then(body => {
if (!body) {return "No output received from API!"}
data = body.output
})
return data;
}
sparse.fact = async function() {
if (!validKey) {return "Invalid key!"}
watchTower()
if (limitUser) {return "Slow Down! Please do not create more then " + reqPerMin + " request per seccond!"}
await fetch('https://sparse.pw/api/fact?key=' + apiKey)
.then(res => res.json()).then(body => {
if (!body) {return "No output received from API!"}
data = body.output
})
return data;
}
sparse.password = async function(maxChars) {
if (!validKey) {return "Invalid key!"}
watchTower()
if (limitUser) {return "Slow Down! Please do not create more then " + reqPerMin + " request per seccond!"}
if (!maxChars) {maxChars = 6;}
if (maxChars < 6) {maxChars = 6;}
if (maxChars > 200) {maxChars = 200}
if (!parseInt(maxChars)) {return "Please use a valid integer!"}
maxChars = parseInt(Math.round(maxChars));
await fetch('https://sparse.pw/api/password?length=' + maxChars + '&key=' + apiKey)
.then(res => res.json()).then(body => {
if (!body) {return "No output recieved from API"}
data = body.output
})
return data;
}
sparse.nslookup = async function(url) {
if (!validKey) {return "Invalid key!"}
watchTower()
if (limitUser) {return "Slow Down! Please do not create more then " + reqPerMin + " request per seccond"}
if (!url) {return "please add a valid url!"}
if (url < 6 || url > 500) {return "URL either too short or too long!"}
await fetch(`https://sparse.pw/api/nslookup?url=${url}&key=${apiKey}`)
.then(res => res.json()).then(body => {
if (!body) return "No data received from the API!"
data = body.output.replace("\"", "").replace("\"", "").replace(",", ", ")
})
return data
}
sparse.host2ip = async function(url) {
if (!validKey) {return "Invalid key!"}
watchTower()
if (limitUser) {return `Slow Down! Please do not create more then ${reqPerMin} request per seccond!`}
if (!url) {return "Please add a valid url!"}
if (url < 6 || url > 500) {return "URL too short or too long!"}
await fetch(`https://sparse.pw/api/host2ip?url=${url}&key=${apiKey}`)
.then(res => res.json()).then(body => {
if (!body) {return "invalid response from API"}
data = body.output
})
return data
}
function watchTower() {
currentMinReq += 1
if (currentMinReq > reqPerMin) {
limitUser = true
}
}
let limitUser = false
setTimeout(function(){
limitUser = false
currentMinReq = 0
}, 1000);
sparse.auth = async function(apiAuth) {
watchTower()
if (limitUser) {return "You have made to many request to auth your key, please wait!"}
let key = apiAuth.apiKey
await fetch(`https://sparse.pw/api/validate?key=${key}`)
.then(res => res.json()).then(body => {
if (!body) {return "Nothing recieved from API"}
data = body.data
})
await allowAPIAccess(data, key)
}
function allowAPIAccess(data, key) {
if (data != true) {
console.log("Invalid Key!")
return
}
validKey = true
apiKey = key
console.log("Welcome to the Sparse API, we have successfully validated your key!")
}
let validKey = false
let apiKey = ""
module.exports = sparse;