Created
October 21, 2019 18:21
-
-
Save EsteveSegura/074ba960a757ff31224706d2f7a35434 to your computer and use it in GitHub Desktop.
bot
This file contains 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
const download = require('image-downloader') | |
const detectFood = require('./detectFood'); | |
//download instagram post | |
async function downloadPost(ig,media_id){ | |
try { | |
let postToDownload = await getMediaIdInfo(ig,media_id); | |
let downloadedData = await downloadImageFromUrl(postToDownload.items[0].image_versions2.candidates[0].url,media_id); | |
return downloadedData; | |
} catch (error) { | |
return 'cant_read'; | |
} | |
} | |
//use package image-downloader for downloading images from the internet | |
async function downloadImageFromUrl (url,id){ | |
const path = `./imgs/${id}.jpg`; | |
await download.image({url: url,dest: path}); | |
return id; | |
} | |
//this way can call python vggModel.py | |
async function getPrediction(path){ | |
let pred = await detectFood.getPrediction(path); | |
return pred; | |
} | |
//instagram workflow | |
(async () => { | |
let ig = await login(); //login | |
await setAntiBanMode(ig); //anti-ban Mode | |
//repeat this every hour | |
setInterval(async () => { | |
let posts = await recentHashtagList(ig, "foodporn"); //get all hashtags in #foodporn hashtag | |
for(let i = 0 ; i < posts.length ; i++){ | |
let actualPost = await downloadPost(ig,posts[i].pk) //download picture | |
if(actualPost != "no_preds"){ | |
try { | |
let prediction = await detectFood.getPrediction(`./imgs/${actualPost}.jpg`) //try to predict | |
console.log(`the media id: ${actualPost} is ${prediction.label} in ${prediction.percentage}.00%`) | |
if(prediction.label == "cheeseburger" && prediction.percentage >= 85){ | |
console.log("Comment that picture!") | |
} | |
} catch (error) { | |
console.log("this is not a picture") | |
} | |
} | |
} | |
}, 60000); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment