forked from jeffnoehren/spotinst-function-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPictureRecognition.js
More file actions
70 lines (60 loc) · 1.58 KB
/
PictureRecognition.js
File metadata and controls
70 lines (60 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
const rp = require('request-promise')
module.exports.main = function main (event, context, callback) {
let params = event.query.imageInfo.split(",")
let user = params[0]
let url = params[1]
let visionKey = process.env['visionKey']
let account = process.env['spotAccount']
let token = process.env['spotToken']
let environment = process.env['spotEnvironment']
let body = {"requests":[
{
"image":{"source":{"imageUri":url}},
"features":{"type": "LABEL_DETECTION",}
}
]}
let options = {
uri:'https://vision.googleapis.com/v1/images:annotate',
qs:{key: visionKey},
method:'POST',
headers:{ 'Content-Type': 'application/json'},
body:body,
json:true
}
rp(options).then((res)=>{
let resList = res.responses[0].labelAnnotations
let tags = []
for(index in resList){
tags.push(resList[index].description)
}
let postValue = `{
"user":"${user}",
"url":"${url}",
"tags":"${tags.toString()}"
}`
let postOptions = {
uri:'https://api.spotinst.io/functions/environment/'+environment+'/userDocument',
method: "POST",
qs: {accountId: account},
headers: {
"Content-Type": "application/json",
"Authorization": "Bearer " + token},
body: {
"userDocument": {
"key": url.slice(28,-4),
"value": postValue}},
json:true
}
console.log(postOptions)
rp(postOptions).then((res)=>{
console.log(res)
callback(null,{statusCode:200,body:"Success"})
}).catch((err)=>{
console.log(err)
callback(null, {statusCode:400, body:err})
})
}).catch((err)=>{
console.log(err)
callback(null, {statusCode:400,body:err})
})
}