Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@Mauro2
Created April 3, 2017 12:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mauro2/681c6d48856d8532a19adeb340a3098b to your computer and use it in GitHub Desktop.
Save Mauro2/681c6d48856d8532a19adeb340a3098b to your computer and use it in GitHub Desktop.
Azure Functions - Meetup Example
var request = require('request-promise');
var azure = require('azure-storage')
module.exports = function (context, data) {
context.log('Foto a procesar: ' + data.url);
analizarImagen(data.url)
.then(notificarIFTTT)
.then(subirBlobStorage);
context.done();
function analizarImagen(url) {
return request({
"uri": "https://westus.api.cognitive.microsoft.com/vision/v1.0/analyze?visualFeatures=Adult",
"method": "POST",
"body": {
"url": url
},
"headers": {
"Content-Type": "application/json",
"Ocp-Apim-Subscription-Key": process.env.SubscriptionKey
},
json: true
})
.then((response) => {
context.log("Is Adult: ", response.adult.isAdultContent);
context.log("Adult Score: ", response.adult.adultScore);
return {
url: url,
isAdult: response.adult.isAdultContent,
adultScore: response.adult.adultScore
}
});
}
function notificarIFTTT(valor) {
return request({
uri: "https://maker.ifttt.com/trigger/picture_processed/with/key/cxTjfZNK4JY6VOTADxongm",
method: 'POST',
body: {
"value1": valor.isAdult ? "OJO AL PIOJO EH" : "Quedate tranquila que no pasa nada"
},
json: true
})
.then(function() {
return valor;
});
}
function subirBlobStorage(data) {
if(!data.isAdult)
return;
var split = data.url.split('/');
var fileName = split[split.length - 1];
context.log(fileName + " subiendo a BlobStorage.");
return azure.createBlobService(process.env.AzureWebJobsStorage).startCopyBlob(data.url, 'uploaded', fileName, function (error, s, r) {
if(error) context.log(error);
context.log(fileName + " subido a BlobStorage.");
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment