-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
64 lines (53 loc) · 1.78 KB
/
index.js
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
'use strict';
const promise = require('bluebird');
const request = require('request-promise');
const aws = require('aws-sdk');
const s3 = new aws.S3({ apiVersion: '2006-03-01' });
const convert = require('./modules/convert');
module.exports.handler = (event, context, callback) => {
return promise.coroutine(processEvent)(event, context, callback);
};
function *processEvent(event, context, callback) {
console.log('lambda is started');
const options = {
uri: 'http://kasen.pref.ishikawa.jp/sp/data/timelineJson/4_10.json',
headers: {
'User-Agent': 'Request-Promise'
},
json: true // Automatically parses the JSON string in the response
};
const data = yield request(options).catch((err) => {
console.error(err.stack);
callback('end fail');
return;
});
const waterLevel = convert.toFormattedJson(data);
console.log(waterLevel);
putWaterLevel(waterLevel)
.then((res) => {
console.log(res);
callback(null, 'success');
return;
})
.catch((err) => {
console.error(err.stack);
callback(err);
return;
});
}
function putWaterLevel(body) {
console.log(body.timestamp);
const day = body.timestamp.split('T')[0];
const daySplit = day.split('-');
const dictory = `waterLevel/japan/ishikawa/asano/${daySplit[0]}/${daySplit[1]}/${daySplit[2]}/`;
const time = body.timestamp.substring(11, 19);
const fileName = `${time}.json`;
console.log(`${dictory}${fileName}`);
const params = {
Bucket: process.env.S3_BUCKET,
Key: `${dictory}${fileName}`,
Body: JSON.stringify(body),
ContentType: 'application/json'
};
return s3.putObject(params).promise();
}