diff --git a/index.js b/index.js index d7e3ce1..994f165 100644 --- a/index.js +++ b/index.js @@ -6,9 +6,14 @@ const port = 3000; var app = express(); app.use(bodyParser.json()); -app.post('/random', (req, res) => { + +app.post('/random', async(req, res) => { res.header('Content-Type', 'application/json'); - res.send(JSON.stringify(random.dataGenerator(req.body.n))); + random.generateJson(req.body.n).then( + (data) => { + res.send(JSON.stringify(data)); + } + ); }); // var Scraper = require('images-scraper'); diff --git a/random.js b/random.js index c920959..bba0e53 100644 --- a/random.js +++ b/random.js @@ -41,6 +41,43 @@ function getRandomString(requiredLength = 5) { return "NP" + getRandomInt(+min_, +max_); } + +async function getRandomImages(count = 2, query = 'nose pin', width = 200, height = 300) { + + var Scraper = require('images-scraper'); + const google = new Scraper({ + puppeteer: { + headless: true, + } + }); + let imageUrls = []; + return google.scrape(query, count).then((results) => { + for (let i = 0; i < results.length; i++) { + imageUrls.push(results[i].url); + } + }).then(() => { + return imageUrls; + }); +} + +exports.generateJson = async(x) => { + let returnArr = []; + template = { + 'StyleNumber': getRandomString(), + 'Images': await getRandomImages(), + 'DiamondWeight': getRandomNumber(min = 0, max = 2), + 'GoldWeight': getRandomNumber(min = 0, max = 4, precision = 3), + 'DiamondCount': getRandomInt(min = 0, max = 100), + 'DesignParameters': { + 'featuredDesign': getRandomBool(), + 'highestSelling': getRandomBool(), + 'hancyDiamond': getRandomBool(), + 'newDesign': getRandomBool(), + } + }; + for (var i = 0; i < x; i++) returnArr.push(template); + return returnArr; +======= function getRandomImage(count = 2, width = 200, height = 300) { let randomImages = []; for (var i = 0; i < count; i++) { @@ -80,4 +117,5 @@ exports.dataGenerator = async (x) => { return arr; + } \ No newline at end of file