-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
executable file
·212 lines (187 loc) · 6.64 KB
/
server.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
var express = require('express'),
fs = require('fs'),
path = require('path'),
request = require('request'),
app = express(),
Twit = require('twit'),
request = require('request'),
https = require('https'),
randomFloat = require('random-float'),
config = {
consumer_key: process.env.CONSUMER_KEY,
consumer_secret: process.env.CONSUMER_SECRET,
access_token: process.env.ACCESS_TOKEN,
access_token_secret: process.env.ACCESS_TOKEN_SECRET
};
var T = new Twit(config);
// HELPER FUNCTIONS
function random_from_array(images) {
return images[Math.floor(Math.random() * images.length)];
}
function getCountry(body) {
var address_info = body.results[0].address_components;
for (var i = 0; i < address_info.length; i ++) {
var info = address_info[i];
if (info.types.includes("country")) {
return info.long_name;
}
}
}
function getLocationName(body) {
var address_info = body.results[0].address_components;
var locationName = "";
for (var i = 0; i < address_info.length; i ++) {
var info = address_info[i];
if (info.types.includes("administrative_area_level_2")) {
locationName += info.long_name + ", ";
}
if (info.types.includes("administrative_area_level_1")) {
locationName += info.long_name + ", ";
}
if (info.types.includes("country")) {
locationName += info.long_name;
}
}
return locationName;
}
// WEBSITE
app.get("/", function (request, response) {
response.writeHeader(200, {"Content-Type": "text/html"});
response.write('<h1>antipode-bot</h1><a href="https://glitch.com/edit/#!/antipode-bot">Check out the source code</a>');
response.end();
});
// TWEET FUNCTION
app.all("/tweet", function (request, response) {
console.log("Received a request...");
generateLatsLons();
});
function generateLatsLons() {
var lat1 = randomFloat(-90, 90).toFixed(3);
var lon1 = randomFloat(-180, 180).toFixed(3);
var lat2 = (-lat1).toFixed(3);
var lon2 = (180 - Math.abs(lon1)).toFixed(3);
if (lon1 > 0) {
lon2 = -lon2;
}
console.log(lat1 + ", " + lon1);
console.log(lat2 + ", " + lon2);
getLocation1(lat1, lon1, lat2, lon2);
}
function getLocation1(lat1, lon1, lat2, lon2) {
// make URL
var url = "https://maps.googleapis.com/maps/api/geocode/json?latlng=" + lat1 + "," + lon1 + "&key=" + process.env.GOOGLE_API;
var request = https.get(url, function (response) {
// data is streamed in chunks from the server
// so we have to handle the "data" event
var buffer = "",
data,
route;
response.on("data", function (chunk) {
buffer += chunk;
});
response.on("end", function (err) {
// finished transferring data
// dump the raw data
var body1 = JSON.parse(buffer);
var status1 = body1.status;
if (status1 == "OK" && getCountry(body1) != "Antarctica") {
console.log("Location 1: " + getLocationName(body1))
getLocation2(body1, lat1, lon1, lat2, lon2);
}
else {
generateLatsLons();
}
});
});
}
function getLocation2(body1, lat1, lon1, lat2, lon2) {
// make URL
var url = "https://maps.googleapis.com/maps/api/geocode/json?latlng=" + lat2 + "," + lon2 + "&key=" + process.env.GOOGLE_API;
var request = https.get(url, function (response) {
// data is streamed in chunks from the server
// so we have to handle the "data" event
var buffer = "",
data,
route;
response.on("data", function (chunk) {
buffer += chunk;
});
response.on("end", function (err) {
// finished transferring data
// dump the raw data
var body2 = JSON.parse(buffer);
var status2 = body2.status;
if (status2 == "OK" && getCountry(body2) != "Antarctica") {
console.log("Location 2: " + getLocationName(body2))
uploadImage1(body1, body2, lat1, lon1, lat2, lon2);
}
else {
generateLatsLons();
}
});
});
}
function uploadImage1(body1, body2, lat1, lon1, lat2, lon2) {
console.log('Image 1 loading ...');
var urls = ["https://maps.googleapis.com/maps/api/staticmap?format=jpg&scale=2&maptype=hybrid¢er=" + lat1 + "," + lon1 + "&zoom=8&size=600x300&key=" + process.env.GOOGLE_API];
request({url: random_from_array(urls), encoding: null}, function (err, res, body) {
if (!err && res.statusCode == 200) {
var b64content = 'data:' + res.headers['content-type'] + ';base64,',
image = body.toString('base64');
console.log('Image 1 loaded!');
T.post('media/upload', { media_data: image }, function (err, data, response) {
if (err){
console.log('ERROR:');
console.log(err);
// response.sendStatus(500);
}
else{
console.log('Image 1 uploaded!');
uploadImage2(body1, body2, lat1, lon1, lat2, lon2, data.media_id_string)
}
});
}
});
}
function uploadImage2(body1, body2, lat1, lon1, lat2, lon2, media_id_string1) {
console.log('Image 2 loading ...');
var urls = ["https://maps.googleapis.com/maps/api/staticmap?format=jpg&scale=2&maptype=hybrid¢er=" + lat2 + "," + lon2 + "&zoom=8&size=600x300&key=" + process.env.GOOGLE_API];
request({url: random_from_array(urls), encoding: null}, function (err, res, body) {
if (!err && res.statusCode == 200) {
var b64content = 'data:' + res.headers['content-type'] + ';base64,',
image = body.toString('base64');
console.log('Image 2 loaded!');
T.post('media/upload', { media_data: image }, function (err, data, response) {
if (err){
console.log('ERROR:');
console.log(err);
// response.sendStatus(500);
}
else{
console.log('Image 2 uploaded!');
postTweet(body1, body2, lat1, lon1, lat2, lon2, media_id_string1, data.media_id_string)
}
});
}
});
}
function postTweet(body1, body2, lat1, lon1, lat2, lon2, media_id_string1, media_id_string2) {
var message = "📍 " + getLocationName(body1) + "\nLatitude: " + lat1 + "\nLongitude: "+ lon1 + "\n" + "📍 " + getLocationName(body2) + "\nLatitude: " + lat2 + "\nLongitude: "+ lon2;
var params = {
status: message,
media_ids: [media_id_string1, media_id_string2]
}
T.post('statuses/update', params, function(err, data, response) {
if (err) {
/* TODO: Proper error handling? */
console.log('Error!');
console.log(err);
}
else {
console.log("Tweeted at " + new Date() + "!");
}
});
}
var listener = app.listen(process.env.PORT, function () {
console.log('Your app is listening on port ' + listener.address().port);
});