Last active
October 20, 2020 15:31
-
-
Save LukyVj/59c2ac08b0285f88526fea56301ddc36 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var cloudinary = require("cloudinary"); | |
var fetch = require("node-fetch"); | |
var fs = require("fs"); | |
require("dotenv").config(); | |
cloudinary.config({ | |
cloud_name: process.env.CLOUD_NAME, | |
api_key: process.env.API_KEY, | |
api_secret: process.env.API_SECRET, | |
}); | |
// Prepare folders Array | |
const allFolders = []; | |
cloudinary.v2.api | |
.sub_folders("Algolia_com_Website_assets/images") | |
.then(async (folder) => { | |
// For each folder, push it to the folders Array | |
folder.folders.map((folderItem, folderIndex) => { | |
allFolders.push(folderItem.path); | |
}); | |
}) | |
.then(() => { | |
// Then empty the data.json file | |
fs.writeFile("./data/data_1x1.json", "", function (err) { | |
if (err) throw err; | |
console.log("Data file created successfully."); | |
}); | |
// For each folder in the Array | |
allFolders.map((folder, folderIndex) => { | |
console.log(folder, folderIndex); | |
// Prepare this folder data | |
const thisFolder = []; | |
const thisFolderData = []; | |
// Perform a search with the cloudinary API | |
cloudinary.v2.search | |
.expression(`folder:${folder} AND resource_type:image`) | |
.max_results(1000) | |
.execute() | |
.then(async (result) => { | |
// Images are inside result.resources ( Array of objects ) | |
result.resources.map(async (item, index) => { | |
fetch(`http://localhost:8080/?q=${item.url}`) | |
.then((res) => res.json()) | |
.then((data) => { | |
// For each item, prepare an object with wanted data | |
const obj = { | |
object_ID: `cloudinary_${item.folder}[${folderIndex}]_image[${index}]`, | |
blur_hash: data.hash, | |
path: item.folder, | |
filename: item.filename, | |
format: item.format, | |
fullPath: `${item.folder}/${item.filename}.${item.format}`, | |
url: item.secure_url, | |
}; | |
// Push the object to thisFolderData ( line 38 ) | |
thisFolderData.push(obj); | |
// Then push the thisFolderData to thisFolder with the name ( line 37 ) | |
thisFolder.push({ name: folder, data: thisFolderData }); | |
}) | |
.then(() => { | |
console.log(thisFolder); | |
// Then, append thisFolder to the data.json file | |
fs.appendFile( | |
"./data/data_1x1.json", // <==== I call this _1x1 because I append every new item to it. | |
// I wanted to write all the data at the end, by filling a big array of objects, and at the end | |
// Of the file, when the fetches are done, I wanted to write it into a json file | |
`${JSON.stringify(...thisFolderData)},`, | |
function (err) { | |
if (err) throw err; | |
console.log("line added successfully."); | |
} | |
); | |
}) | |
.catch((err) => { | |
console.log(err); | |
}); | |
}); | |
}) | |
.catch((err) => { | |
console.log(err); | |
}); | |
}); | |
}) | |
.catch((err) => console.log(err)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment