Its simple 20 lines of code
const instaTouch = require('instatouch');
const vision = require('@google-cloud/vision');
const client = new vision.ImageAnnotatorClient();
process.env['GOOGLE_APPLICATION_CREDENTIALS']
// this returns images from instagram based on tag I provide, it returns promise.
// so far so good, I am getting the image URLs
const getMediaUrls = async (tag, limit) => {
const options = { count: limit, mediaType: 'image' };
const response = await instaTouch.hashtag(tag, options);
let mediaUrls = response.collector.map((el) => el.display_url);
if (mediaUrls !== undefined || mediaUrls.length != 0) return mediaUrls;
};
Here is the problem
const startScrap = (tag, labels, limit) => {
let matchedMediaUrls = [];
getMediaUrls(tag, limit)
.then((mediaUrls) =>
mediaUrls.forEach(async (mediaUrl) => {
// send image url to google vision api and returns label
const [result] = await client.labelDetection(mediaUrl);
const mediaLabels = result.labelAnnotations;
// I check if returned labels from vision api matches with any of the labels I asked for
// if that the case I return the image, and populate matchedMediaUrls=[] array
let labelsArr = mediaLabels.map((el) => el.description.toLowerCase());
const matches = labelsArr.filter((el) => labels.includes(el));
if (matches.length > 0) {
// if I console.log() i find the array getting populated correctly but I don't know how to return it
matchedMediaUrls.push(mediaUrl);
console.log(matchedMediaUrls);
}
})
)
.catch((err) => console.log(err));
};
if I return it here, I get an empty array.
I picked nodejs yesterday. It might not be good question, but I really need help. I have no idea how to fix it, been trying for last 6 hours. Any help would be much appreciated.