colorhunt/api/palette.js

102 lines
2.6 KiB
JavaScript
Raw Normal View History

2020-11-22 18:01:10 -05:00
var request = require('request');
var cheerio = require('cheerio');
// var app = express();
// var cors = require('cors');
2020-11-22 15:10:33 -05:00
2020-11-22 18:01:10 -05:00
// app.use(cors());
//app.addHeader("Access-Control-Allow-Origin", "*");
2020-11-23 02:46:17 -05:00
module.exports = async function (req, res) {
// // res.header('Access-Control-Allow-Origin', '*');
2020-11-22 18:01:10 -05:00
2020-11-23 02:46:17 -05:00
// //
// // 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) {
2020-11-22 18:01:10 -05:00
2020-11-23 02:46:17 -05:00
// // Loads in colorhunt url via request
// request(url, function(error, _, html) {
2020-11-22 18:01:10 -05:00
2020-11-23 02:46:17 -05:00
// if(!error){
2020-11-22 18:01:10 -05:00
2020-11-23 02:46:17 -05:00
// // Parses html via cheerioJS
2020-11-22 18:01:10 -05:00
2020-11-23 02:46:17 -05:00
// var $ = cheerio.load(html);
2020-11-22 18:01:10 -05:00
2020-11-23 02:46:17 -05:00
// var json = {
// id: "",
// code: "",
// date: "",
// likes: "",
// c1: "",
// c2: "",
// c3: "",
// c4: ""
// };
2020-11-23 00:34:50 -05:00
2020-11-23 02:46:17 -05:00
// var script = $('#jscode').next().html();
2020-11-22 18:01:10 -05:00
2020-11-23 02:46:17 -05:00
// // 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");
2020-11-22 18:01:10 -05:00
2020-11-23 02:46:17 -05:00
// if (itemerIndex === -1) {
// console.log(url + " didn't work.");
// url = Math.floor(Math.random() * 100000).toString();
// getColorJson('http://colorhunt.co/c/' + url);
// return;
// }
2020-11-22 18:01:10 -05:00
2020-11-23 02:46:17 -05:00
// var itemerString = script.substr(itemerIndex, itemerIndex + 100);
2020-11-22 18:01:10 -05:00
2020-11-23 02:46:17 -05:00
// var itemer = itemerString.split("'");
// //console.log(itemer);
2020-11-22 18:01:10 -05:00
2020-11-23 02:46:17 -05:00
// 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);
2020-11-22 18:01:10 -05:00
2020-11-23 02:46:17 -05:00
// console.log("The code " + json.id + " worked!");
// res.json(json);
// return;
// } else {
// console.log("Error on request: ")
// console.log(error);
// }
2020-11-22 18:01:10 -05:00
2020-11-23 02:46:17 -05:00
// })
// }
// // Generates first color code and initializes recursive function.
// var url = Math.floor(Math.random() * 100000).toString();
// getColorJson('http://colorhunt.co/c/' + url);
2020-11-22 18:01:10 -05:00
2020-11-23 02:46:17 -05:00
res.end(await getRandom());
2020-11-23 00:34:50 -05:00
};
function getRandom() {
2020-11-23 02:48:30 -05:00
return new Promise(resolve => {
2020-11-23 02:46:17 -05:00
request({
url: 'http://colorhunt.co/hunt.php',
form: {
step: '1',
sort: 'random',
tags: ''
}
}, function(error, res, body) {
2020-11-23 02:48:30 -05:00
resolve(body);
2020-11-23 02:46:17 -05:00
})
2020-11-23 00:34:50 -05:00
});
}