Let's say I have 4 tasks
- Take a screenshot of a website (using some API service)
- Download the screenshot to your local hardDrive
- Upload the screenshot from the local hardDrive to a Google Cloud Bucket
- Use GC-Vision API to detect text on that screenshot
If the result of task #4 is equal to -1, that means I didn't find what I'm looking for, so I need to do this all over again until the result doesn't equal -1 (which probably means I found what I need).
Now some code:
const screenshot = require("./thum/takeScreenshot");
const googleVisionAPI = require("./gc-vision-api/findTextInButton");
const matchURL = /^((http[s]?|ftp):\/)?\/?([^:\/\s]+)((\/\w+)*\/)([\w\-\.]+[^#?\s]+)(.*)?(#[\w\-]+)?$/;
const UploadToGcBucket = require("./gc-storage/saveScreenshot");
const downloadImage = require("./downloadFile");
// let's wait for the bucket to replicate before searching text
function timeout(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
const workHard = async () => {
// 1. Take a screenshot
const takeAscreenShot = async () => {
let scShotTaken = await `https:${screenshot.thumURL}`;
console.log("screenshot captured!!! ", scShotTaken);
return scShotTaken;
};
// 2. Download it to local hard-drive
const downloadScreenshot = async url => {
let download = await downloadImage.downloadImage(url);
return download;
};
// 3. Upload it to Google Cloud Bucket
const uploadToGCBucket = async () => {
let upload = await UploadToGcBucket.UploadToGcBucket();
return upload;
};
// 4. Check if the appointment's button is present
const checkMagicButton = async () => {
// wait a little bit for replication
await timeout(5000);
let searchAppointment = await googleVisionAPI.findMagicButton(
`some_picture_in_a_cloud_bucket.jpg`
);
return searchAppointment;
};
takeAscreenShot()
.then(pic => {
// Verify if it looks like a URL
let verifyURL = matchURL.test(pic);
if (!verifyURL) {
return "The screenshot doesn't look like a valid URL, check the Thumb API";
}
downloadScreenshot(pic)
.then(() => {
uploadToGCBucket()
.then(() => {
checkMagicButton()
.then(button => {
if (button === -1) {
throw new Error();
}
})
.catch(makeCatch("AI finding button ERROR"));
})
.catch(makeCatch("Uploading to GC Bucket ERROR"));
})
.catch(makeCatch("Downloading ERROR"));
})
.catch(makeCatch("ScreenShot ERROR"));
};
const makeCatch = msg => err => {
console.log("Problem with " + msg, err);
throw new Error(); // this is important- this rejects the Promise, so it stops the hardWork
};
const tryWork = () => {
workHard().catch(error => {
setInterval(tryWork, 5000);
});
};
tryWork();
so...how do I call HardWork() all over again let's say in 5 minutes if I don't find what I need.
checkMagicButton. That looks like a mistake. Is that your actual code, or psuedo-code? (can you post the actual code?)takeAscreenShot() .then(pic => {some more code}