-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlambda.js
88 lines (73 loc) · 2.44 KB
/
lambda.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
const AWS = require('aws-sdk');
const axios = require("axios");
const uuid = require("uuid/v1");
const randomstring = require("randomstring");
const baseUrl = "https://api.bird.co/";
const loginPath = "user/login";
const birdsPath = "bird/nearby";
const appVersion = '3.0.5'
const tlvLatLng = { latitude: 32.079554, longitude: 34.781133 }
const sfLatLng = { latitude: 37.762053, longitude: -122.449028 }
const tlvTimeZone = "Asia/Jerusalem";
const sfTimeZone = "America/Los_Angeles";
// lambda function to collect the current birds locations and store them in s3.
// To change target city replace getBirds target location and dateString timezone
exports.handler = async (event) => {
const email = randomstring.generate(10) + '@test.com'
const device = uuid();
const token = await login(email, device);
const data = await getBirds(device, token, tlvLatLng);
data.date = Date.now();
const dateString = new Date().toLocaleString("en-GB", { timeZone: tlvTimeZone }).split(',')[0].replace(/\//g, '_')
writeToS3(dateString, data.date, data)
const response = {
statusCode: 200,
body: "Storing birds locations for " + data.date
};
return response;
};
const login = async (email, device) => {
const response = await axios({
method: 'post',
url: baseUrl + loginPath,
headers: {
'Device-id': device,
'Platform': 'ios'
},
data: {
email: email,
}
});
return response.data.token;
};
const getBirds = async (device, token, location) => {
const response = await axios({
method: 'get',
url: baseUrl + birdsPath,
headers: {
'Authorization': 'Bird ' + token,
'Device-id': device,
'App-Version': appVersion,
'Location': JSON.stringify(location)
},
params: {
latitude: location.latitude,
longitude: location.longitude,
radius: 10000
},
});
return response.data;
}
const writeToS3 = async (folder, filename, data) => {
var s3 = new AWS.S3();
var params = {
Bucket: 'birds-locations',
Key: folder + '/' + filename,
Body: JSON.stringify(data)
}
await s3.putObject(params, function (err, data) {
console.log("writeToS3");
if (err) console.log(err, err.stack); // an error occurred
else console.log(data); // successful response
});
}