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.use(cors());
|
||||||
|
|
||||||
//app.addHeader("Access-Control-Allow-Origin", "*");
|
//app.addHeader("Access-Control-Allow-Origin", "*");
|
||||||
module.exports = function (req, res) {
|
module.exports = async function (req, res) {
|
||||||
// res.header('Access-Control-Allow-Origin', '*');
|
// // res.header('Access-Control-Allow-Origin', '*');
|
||||||
|
|
||||||
//
|
// //
|
||||||
// Because I couldn't find a good source for colorpalette ids on colorhunt,
|
// // 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
|
// // 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.
|
// // a color palette code, and calls the function recursively if it doesn't.
|
||||||
//
|
// //
|
||||||
// This means its usually making several calls before returning data.
|
// // This means its usually making several calls before returning data.
|
||||||
//
|
// //
|
||||||
function getColorJson(url) {
|
// function getColorJson(url) {
|
||||||
|
|
||||||
// Loads in colorhunt url via request
|
// // Loads in colorhunt url via request
|
||||||
request(url, function(error, _, html) {
|
// 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 = {
|
// var json = {
|
||||||
id: "",
|
// id: "",
|
||||||
code: "",
|
// code: "",
|
||||||
date: "",
|
// date: "",
|
||||||
likes: "",
|
// likes: "",
|
||||||
c1: "",
|
// c1: "",
|
||||||
c2: "",
|
// c2: "",
|
||||||
c3: "",
|
// c3: "",
|
||||||
c4: ""
|
// c4: ""
|
||||||
};
|
// };
|
||||||
|
|
||||||
var script = $('#jscode').next().html();
|
// var script = $('#jscode').next().html();
|
||||||
|
|
||||||
// Finds our string of data on the page
|
// // Finds our string of data on the page
|
||||||
// -- or returns "-1" to itemerIndex if the random code guess doesn't work
|
// // -- or returns "-1" to itemerIndex if the random code guess doesn't work
|
||||||
var itemerIndex = script.search("itemer");
|
// var itemerIndex = script.search("itemer");
|
||||||
|
|
||||||
if (itemerIndex === -1) {
|
// if (itemerIndex === -1) {
|
||||||
console.log(url + " didn't work.");
|
// console.log(url + " didn't work.");
|
||||||
url = Math.floor(Math.random() * 100000).toString();
|
// url = Math.floor(Math.random() * 100000).toString();
|
||||||
getColorJson('http://colorhunt.co/c/' + url);
|
// getColorJson('http://colorhunt.co/c/' + url);
|
||||||
return;
|
// return;
|
||||||
}
|
// }
|
||||||
|
|
||||||
var itemerString = script.substr(itemerIndex, itemerIndex + 100);
|
// var itemerString = script.substr(itemerIndex, itemerIndex + 100);
|
||||||
|
|
||||||
var itemer = itemerString.split("'");
|
// var itemer = itemerString.split("'");
|
||||||
//console.log(itemer);
|
// //console.log(itemer);
|
||||||
|
|
||||||
json.id = itemer[1];
|
// json.id = itemer[1];
|
||||||
json.code = itemer[3];
|
// json.code = itemer[3];
|
||||||
json.date = itemer[5];
|
// json.date = itemer[5];
|
||||||
json.likes = itemer[7];
|
// json.likes = itemer[7];
|
||||||
json.c1 = "#" + json.code.substring(0,6);
|
// json.c1 = "#" + json.code.substring(0,6);
|
||||||
json.c2 = "#" + json.code.substring(6,12);
|
// json.c2 = "#" + json.code.substring(6,12);
|
||||||
json.c3 = "#" + json.code.substring(12,18);
|
// json.c3 = "#" + json.code.substring(12,18);
|
||||||
json.c4 = "#" + json.code.substring(18,24);
|
// json.c4 = "#" + json.code.substring(18,24);
|
||||||
|
|
||||||
console.log("The code " + json.id + " worked!");
|
// console.log("The code " + json.id + " worked!");
|
||||||
res.json(json);
|
// res.json(json);
|
||||||
return;
|
// return;
|
||||||
} else {
|
// } else {
|
||||||
console.log("Error on request: ")
|
// console.log("Error on request: ")
|
||||||
console.log(error);
|
// console.log(error);
|
||||||
}
|
// }
|
||||||
|
|
||||||
})
|
// })
|
||||||
}
|
// }
|
||||||
|
|
||||||
// Generates first color code and initializes recursive function.
|
// // Generates first color code and initializes recursive function.
|
||||||
var url = Math.floor(Math.random() * 100000).toString();
|
// var url = Math.floor(Math.random() * 100000).toString();
|
||||||
getColorJson('http://colorhunt.co/c/' + url);
|
// getColorJson('http://colorhunt.co/c/' + url);
|
||||||
|
|
||||||
|
res.end(await getRandom());
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function getRandom() {
|
function getRandom() {
|
||||||
return new Promise(res => {
|
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