HUNT.PHP
parent
99d75c871b
commit
21ccaa56df
129
api/palette.js
129
api/palette.js
|
|
@ -7,85 +7,96 @@ var cheerio = require('cheerio');
|
|||
// app.use(cors());
|
||||
|
||||
//app.addHeader("Access-Control-Allow-Origin", "*");
|
||||
module.exports = function (req, res) {
|
||||
// res.header('Access-Control-Allow-Origin', '*');
|
||||
module.exports = async function (req, res) {
|
||||
// // res.header('Access-Control-Allow-Origin', '*');
|
||||
|
||||
//
|
||||
// Because I couldn't find a good source for colorpalette ids on colorhunt,
|
||||
// the below function generates a random number from 0 - 10000 and sees if it matches
|
||||
// a color palette code, and calls the function recursively if it doesn't.
|
||||
//
|
||||
// This means its usually making several calls before returning data.
|
||||
//
|
||||
function getColorJson(url) {
|
||||
// //
|
||||
// // Because I couldn't find a good source for colorpalette ids on colorhunt,
|
||||
// // the below function generates a random number from 0 - 10000 and sees if it matches
|
||||
// // a color palette code, and calls the function recursively if it doesn't.
|
||||
// //
|
||||
// // This means its usually making several calls before returning data.
|
||||
// //
|
||||
// function getColorJson(url) {
|
||||
|
||||
// Loads in colorhunt url via request
|
||||
request(url, function(error, _, html) {
|
||||
// // Loads in colorhunt url via request
|
||||
// request(url, function(error, _, html) {
|
||||
|
||||
if(!error){
|
||||
// if(!error){
|
||||
|
||||
// Parses html via cheerioJS
|
||||
// // Parses html via cheerioJS
|
||||
|
||||
var $ = cheerio.load(html);
|
||||
// var $ = cheerio.load(html);
|
||||
|
||||
var json = {
|
||||
id: "",
|
||||
code: "",
|
||||
date: "",
|
||||
likes: "",
|
||||
c1: "",
|
||||
c2: "",
|
||||
c3: "",
|
||||
c4: ""
|
||||
};
|
||||
// var json = {
|
||||
// id: "",
|
||||
// code: "",
|
||||
// date: "",
|
||||
// likes: "",
|
||||
// c1: "",
|
||||
// c2: "",
|
||||
// c3: "",
|
||||
// c4: ""
|
||||
// };
|
||||
|
||||
var script = $('#jscode').next().html();
|
||||
// var script = $('#jscode').next().html();
|
||||
|
||||
// Finds our string of data on the page
|
||||
// -- or returns "-1" to itemerIndex if the random code guess doesn't work
|
||||
var itemerIndex = script.search("itemer");
|
||||
// // Finds our string of data on the page
|
||||
// // -- or returns "-1" to itemerIndex if the random code guess doesn't work
|
||||
// var itemerIndex = script.search("itemer");
|
||||
|
||||
if (itemerIndex === -1) {
|
||||
console.log(url + " didn't work.");
|
||||
url = Math.floor(Math.random() * 100000).toString();
|
||||
getColorJson('http://colorhunt.co/c/' + url);
|
||||
return;
|
||||
}
|
||||
// if (itemerIndex === -1) {
|
||||
// console.log(url + " didn't work.");
|
||||
// url = Math.floor(Math.random() * 100000).toString();
|
||||
// getColorJson('http://colorhunt.co/c/' + url);
|
||||
// return;
|
||||
// }
|
||||
|
||||
var itemerString = script.substr(itemerIndex, itemerIndex + 100);
|
||||
// var itemerString = script.substr(itemerIndex, itemerIndex + 100);
|
||||
|
||||
var itemer = itemerString.split("'");
|
||||
//console.log(itemer);
|
||||
// var itemer = itemerString.split("'");
|
||||
// //console.log(itemer);
|
||||
|
||||
json.id = itemer[1];
|
||||
json.code = itemer[3];
|
||||
json.date = itemer[5];
|
||||
json.likes = itemer[7];
|
||||
json.c1 = "#" + json.code.substring(0,6);
|
||||
json.c2 = "#" + json.code.substring(6,12);
|
||||
json.c3 = "#" + json.code.substring(12,18);
|
||||
json.c4 = "#" + json.code.substring(18,24);
|
||||
// json.id = itemer[1];
|
||||
// json.code = itemer[3];
|
||||
// json.date = itemer[5];
|
||||
// json.likes = itemer[7];
|
||||
// json.c1 = "#" + json.code.substring(0,6);
|
||||
// json.c2 = "#" + json.code.substring(6,12);
|
||||
// json.c3 = "#" + json.code.substring(12,18);
|
||||
// json.c4 = "#" + json.code.substring(18,24);
|
||||
|
||||
console.log("The code " + json.id + " worked!");
|
||||
res.json(json);
|
||||
return;
|
||||
} else {
|
||||
console.log("Error on request: ")
|
||||
console.log(error);
|
||||
}
|
||||
// console.log("The code " + json.id + " worked!");
|
||||
// res.json(json);
|
||||
// return;
|
||||
// } else {
|
||||
// console.log("Error on request: ")
|
||||
// console.log(error);
|
||||
// }
|
||||
|
||||
})
|
||||
}
|
||||
// })
|
||||
// }
|
||||
|
||||
// Generates first color code and initializes recursive function.
|
||||
var url = Math.floor(Math.random() * 100000).toString();
|
||||
getColorJson('http://colorhunt.co/c/' + url);
|
||||
// // Generates first color code and initializes recursive function.
|
||||
// var url = Math.floor(Math.random() * 100000).toString();
|
||||
// getColorJson('http://colorhunt.co/c/' + url);
|
||||
|
||||
res.end(await getRandom());
|
||||
};
|
||||
|
||||
|
||||
|
||||
function getRandom() {
|
||||
return new Promise(res => {
|
||||
request('http://colorhunt.co/c/random')
|
||||
request({
|
||||
url: 'http://colorhunt.co/hunt.php',
|
||||
form: {
|
||||
step: '1',
|
||||
sort: 'random',
|
||||
tags: ''
|
||||
}
|
||||
}, function(error, res, body) {
|
||||
res(body);
|
||||
})
|
||||
});
|
||||
}
|
||||
Loading…
Reference in New Issue