-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_pexels.js
54 lines (48 loc) · 1.46 KB
/
create_pexels.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/env node
const fs = require("fs");
const download = require("image-downloader");
const { pexelsKey } = require("./.env");
const axios = require("axios");
const deepDream = require("./functions/deepDream");
const generate = require("./functions/generate");
const dateSuffix = require("./functions/dateSuffix");
const randomItem = require("./functions/randomItem");
const randomWord = require("./functions/randomWord");
const generateImage = async iterations => {
const randomFrom1to1000 = Math.floor(Math.random() * 999) + 1;
const cleanName = "pexels_" + randomFrom1to1000.toString() + dateSuffix();
fs.mkdirSync(`images/${cleanName}`);
try {
const response = await axios.get(
`https://api.pexels.com/v1/curated?per_page=1&page=${randomFrom1to1000}`,
{ headers: { Authorization: pexelsKey } }
);
await download.image({
url: response.data.photos[0].src.large,
dest: "./temp_pexels.jpg",
followRedirect: true
});
} catch (error) {
console.log(error);
console.log("Image with ID " + randomFrom1to1000 + " did not work");
}
try {
await deepDream(
"./temp_pexels.jpg",
`images/${cleanName}/${cleanName}.jpg`,
iterations
);
} catch (e) {
console.log(e);
}
return {
filePath: `images/${cleanName}/${cleanName}.jpg`,
fileName: `${cleanName}.jpg`
};
};
module.exports = generateImage;
if (require.main === module) {
(async () => {
await generate(generateImage);
})();
}