-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
183 lines (149 loc) · 6.24 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
182
183
// setup and modules
if (process.env.NODE_ENV !== 'production') {
require('dotenv').config();
}
const port = 3000;
// twitter
const Twit = require("twit");
// http
const got = require("got");
// express
const express = require('express')
var app = express();
const createError = require('http-errors');
const path = require('path');
const cookieParser = require('cookie-parser');
const logger = require('morgan');
// socket
const server = require("http").Server(app);
const io = require("socket.io")(server);
// server
var indexRouter = require('./routes/index');
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', indexRouter);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
next(createError(404));
});
// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
});
module.exports = app;
server.listen(port);
// twitter streaming
const morning = ["Good morning", "Buenas dias", "صباح الخير", "সুপ্রভাত", "शुभ प्रभात", "Доброе утро", "Bom Dia", "おはようございます", "Guten Morgen", "Sugeng enjang", "좋은 아침", "Bon matin", "Günaydın", "Chào buổi sáng", "శుభోదయం", "शुभ प्रभात", "காலை வணக்கம்", "Buongiorno", "صبح بخیر", "સુપ્રભાત", "Dzień dobry", "Доброго ранку", "സുപ്രഭാതം", "ಶುಭೋದಯ"];
let morningre = new RegExp(morning.join("|"), "i")
const night = ["Good night", "Buenas noches", "تصبح على خير", "শুভ রাত্রি", "शुभ रात्रि", "Доброй ночи", "Boa noite", "おやすみ", "Gute Nacht", "Sugeng dalu", "안녕히 주무세요", "Bonne nuit", "İyi geceler", "Chúc ngủ ngon", "శుభ రాత్రి", "शुभ रात्री", "இனிய இரவு", "Buona notte", "شب بخیر", "શુભ રાત્રી", "Dobranoc", "Надобраніч", "ശുഭ രാത്രി", "ಶುಭ ರಾತ್ರಿ", "Noapte buna"];
let nightre = new RegExp(night.join("|"), "i")
const morningnight = morning.concat(night);
var T = new Twit({
consumer_key: process.env.TWITTER_CONSUMER_KEY,
consumer_secret: process.env.TWITTER_CONSUMER_SECRET,
access_token: process.env.TWITTER_ACCESS_TOKEN_KEY,
access_token_secret: process.env.TWITTER_ACCESS_TOKEN_SECRET,
timeout_ms: 10*1000
});
// socket.io communication
var stream = T.stream("statuses/filter", { track: morningnight });
var allClients = []; // https://stackoverflow.com/a/17311682
io.on("connect", (socket) => {
console.log('a user connected');
allClients.push(socket.id);
stream.on("tweet", function(tweet) {
// no need to act if no clients are there
if (allClients.length == 0) return;
// exclude retweets
if (tweet.retweeted || tweet.text.substring(0, 4) === "RT @") return;
// only look at geotagged tweets
if (!tweet.place) return;
// api finds phrases in more than just the tweet text; exclude those
if (!morningre.test(tweet.text) && !nightre.test(tweet.text)) return;
// console.log(tweet);
// console.log();
let text = (tweet.hasOwnProperty("extended_tweet")) ? tweet.extended_tweet.full_text : tweet.text;
let eventType = (morningre.test(tweet.text)) ? 0 : 1; // 0 for morning, 1 for night
let latlng = {};
let place = "";
if (tweet.place) {
let bbox = tweet.place.bounding_box.coordinates[0];
latlng["lng"] = (bbox[0][0] + bbox[2][0]) * 0.5;
latlng["lat"] = (bbox[0][1] + bbox[2][1]) * 0.5;
place = tweet.place.full_name;
console.log(tweet.text);
console.log("place", latlng);
console.log();
} else if (tweet.coordinates) {
latlng["lng"] = tweet.coordinates.coordinates[0];
latlng["lat"] = tweet.coordinates.coordinates[1];
console.log(tweet.text);
console.log("coords", latlng);
console.log();
}
let tweetObj = {
"text": text,
"eventType": eventType,
"latlng": latlng,
"place": place,
"url": "https://twitter.com/" + tweet.user.screen_name + "/status/" + tweet.id_str
};
socket.emit("tweet", tweetObj)
// } else {
// let userloc = tweet.user.location;
// // try to detect non-place locations
// if (userloc.includes(".com")) {
// console.log("No results found for:", userloc);
// return;
// }
// // try to get coords with mapquest api
// (async () => {
// try {
// let mapquestapi = "http://www.mapquestapi.com/geocoding/v1/address"
// const response = await got(mapquestapi, {
// searchParams: {
// key: process.env.MAPQUEST_KEY,
// location: userloc
// },
// responseType: "json"
// });
// let firstresult = response.body.results[0].locations[0];
// let gq = firstresult.geocodeQuality;
// if (gq === "CITY" || gq === "STATE" || gq === "COUNTRY") {
// latlng["lng"] = firstresult.latLng["lng"];
// latlng["lat"] = firstresult.latLng["lat"];
// console.log("location", userloc, latlng);
// console.log(tweet.text);
// console.log();
// io.emit("tweet", {"latlng": latlng, "text": tweet.text, "eventType": eventType });
// } else {
// console.log("No results found for:", userloc);
// }
// } catch (error) {
// console.log("No results found for:", userloc);
// throw error;
// }
// })();
// }
});
stream.on("error", function(error) {
if (allClients.length == 0) return;
throw error;
});
socket.on("disconnect", (reason) => {
console.log("a user disconnected");
var i = allClients.indexOf(socket.id);
allClients.splice(i, 1);
});
});