-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.js
86 lines (69 loc) · 2.59 KB
/
handler.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
const capitalize = require("lodash/fp/capitalize");
const chunk = require("lodash/fp/chunk");
const compose = require("lodash/fp/compose");
const flatMap = require("lodash/fp/flatMap");
const groupBy = require("lodash/fp/groupBy");
const has = require("lodash/fp/has");
const join = require("lodash/fp/join");
const map = require("lodash/fp/map");
const mapValues = require("lodash/fp/mapValues");
const padCharsStart = require("lodash/fp/padCharsStart");
const reduce = require("lodash/fp/reduce");
const Twit = require('twit');
const { promisify } = require('util')
const request = require('request');
const request_promise = promisify(request);
const {
CONSUMER_KEY,
CONSUMER_SECRET,
ACCESS_TOKEN,
ACCESS_SECRET
} = process.env
const parkingBot = new Twit({
consumer_key: CONSUMER_KEY,
consumer_secret: CONSUMER_SECRET,
access_token: ACCESS_TOKEN,
access_token_secret: ACCESS_SECRET,
});
const chunkTextForTwitter = compose(map(join('')), chunk(280));
const to12HourTime = hours => hours % 12 || 12;
const formatHours = compose(to12HourTime, time => time.getHours());
const formatMinutes = compose(padCharsStart('0', 2), time => time.getMinutes());
const formatCurrentTime = (time) => formatHours(time) + ':' + formatMinutes(time);
const prependTime = text => formatCurrentTime(new Date) + text;
const groupToTweet = (tweet, lotCounts, parkingType) => `${tweet}
${parkingType}:
${lotCounts}`;
const zoneToTweet = ({ id, status, parks, total }) => (status === "open")
? `${id}: ${parks > 0 ? parks : 'Full'}`
: `${id}: Closed`;
const groupValToText = compose(join('\n'), map(zoneToTweet));
const uncappedReduce = reduce.convert({ 'cap': false });
const constructTweets = compose(
chunkTextForTwitter,
prependTime,
uncappedReduce(groupToTweet, ''),
mapValues(groupValToText),
);
const addIdToZone = id => zone => ({ ...zone, id });
const mapToZones = ({ zones, id }) => map(addIdToZone(id), zones);
const groupParkingLots = compose(
groupBy(zone => capitalize(zone.type)),
flatMap(mapToZones),
JSON.parse, // can fail
resp => resp.body
)
const getParkingData = () => request_promise('https://api.uow.edu.au/parking/data?array')
const successFn = result => (has('data.errors', result))
? Promise.reject(result.data.errors)
: console.log('Success!')
const sendTweet = (status) => parkingBot.post('statuses/update', { status })
.then(successFn)
.catch(console.log)
exports.bot = (event, context, cb) => {
getParkingData()
.then(groupParkingLots)
.then(constructTweets)
.then(map(sendTweet))
.then(x => cb(null, x))
}