-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
181 lines (157 loc) · 4.82 KB
/
app.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
"use strict";
const axios = require("axios").default;
const admin = require("firebase-admin");
const Buffer = require("safe-buffer").Buffer;
const https = require("https");
const { serializeError } = require("serialize-error");
const Twit = require("twit");
const {
adsbx: { url, lat, lon, radius, key },
cooldownMinutes,
maximumAlt,
dbUrl,
firebase,
photoApi,
storageBucket,
twitter
} = require("./config");
const {
createStatus,
isNewState,
filterStates,
formatIdentifier,
formatType,
randomItem
} = require("./utils");
admin.initializeApp({
credential: admin.credential.cert(firebase),
databaseURL: dbUrl,
storageBucket
});
const db = admin.database();
const statesRef = db.ref("states");
const opsRef = db.ref("operators");
const ignoredRef = db.ref("ignored");
const bucket = admin.storage().bucket();
const T = new Twit(twitter);
// Download and convert image to base64
const downloadMedia = url =>
axios
.get(url, { responseType: "arraybuffer" })
.then(({ data }) => Buffer.from(data, "binary").toString("base64"));
const fetchMedia = async (call = "", icao, reg) => {
// Check for photos in storage before calling API
const [files] = await bucket.getFiles({
delimiter: "/",
prefix: `photos/${icao.toUpperCase()}/`
});
return files.length > 0
? await fetchLocalMedia(
files.filter(file => !file.name.endsWith("/")).map(file => file.name)
)
: await fetchRemoteMediaUrl(reg || call.trim());
};
const fetchLocalMedia = async files => {
const url = `https://storage.googleapis.com/${storageBucket}/${randomItem(
files
)}`;
return {
photo: await downloadMedia(url),
link: false
};
};
// Get image url from photo API
const fetchRemoteMediaUrl = reg => {
const { url, username, password } = photoApi;
return axios({
method: "get",
url,
auth: { username, password },
params: { reg }
}).then(async ({ data: { photos = [], links = [] } }) => ({
photo: photos.length > 0 && (await downloadMedia(photos[0])),
link: links.length > 0 && links[0]
}));
};
// Main function to retrieve ADS-B data from ADSBx
const fetchStates = () =>
axios
.get(`${url}/lat/${lat}/lon/${lon}/dist/${radius}/`, {
headers: { "api-auth": key, "Accept-Encoding": "gzip" }
})
.then(({ data }) => data);
// Send a media tweet if there is a photo, otherwise normal tweet
const postTweet = async (snap, state, ops) => {
const { dbFlags, flight, hex, r: reg, t: frame } = state;
const media = await fetchMedia(flight, hex, reg);
return media.photo
? // Send media tweet
T.post("media/upload", { media_data: media.photo })
.then(({ data }) =>
T.post("media/metadata/create", {
media_id: data.media_id_string,
alt_text: {
text: `${formatType(frame)} (${formatIdentifier(
flight,
reg,
dbFlags
)})`
}
}).then(res =>
T.post("statuses/update", {
status: createStatus(snap, state, ops),
media_ids: [data.media_id_string]
})
)
)
.catch(error => {
console.warn(
"Media upload failed",
JSON.stringify(serializeError(error))
);
// Atempt to send tweet without media on error
return T.post("statuses/update", {
status: createStatus(snap, state, ops, media.link)
});
})
: // Send tweet without media
T.post("statuses/update", {
status: createStatus(snap, state, ops, media.link)
});
};
// Record timestamp of spotted aircraft to database
const saveTimestamp = (hex, time) =>
statesRef.child(`${hex.toUpperCase()}/timestamps`).push(time);
const app = async () => {
try {
const { ac: states, ctime: time } = await fetchStates();
// Return if no states exist
if (!states) return false;
const ignored = await ignoredRef.child("operators").once("value");
return await Promise.all(
filterStates(states, ignored).map(async state => {
const { hex } = state;
const snap = await statesRef.child(hex.toUpperCase()).once("value");
const ops = await opsRef.once("value");
// Check if this is a new aircraft or if it's past the cooldown time
if (isNewState(snap, cooldownMinutes)) {
console.info(JSON.stringify(state));
return await Promise.all([
saveTimestamp(hex, time),
postTweet(snap, state, ops)
]);
}
return false;
})
);
} catch (error) {
return console.error(JSON.stringify(serializeError(error)));
} finally {
https
.get("https://hc-ping.com/696f14b8-21f9-4cbf-8dad-7b847a8ab295")
.on("error", err => {
console.log("Ping failed: " + err);
});
}
};
module.exports = app;