-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
137 lines (111 loc) · 3.25 KB
/
index.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
const express = require('express');
const Twit = require('twit');
const request = require('request');
const promise = require('request-promise');
const bodyParser = require('body-parser');
const favicon = require('serve-favicon');
const path = require("path");
// const TW-TRENDS_URL = "http://1.1/trends/place.json"
// const TW-LIVE_URL = "http://1.1/statuses/filter.json"
var TW_KEYS = {
consumer_key: process.env.TW_CONSUMER_KEY,
consumer_secret: process.env.TW_CONSUMER_SECRET
}
var Twitter = new Twit({
consumer_key: process.env.TW_CONSUMER_KEY,
consumer_secret: process.env.TW_CONSUMER_SECRET,
access_token: process.env.TW_TOKEN_KEY,
access_token_secret: process.env.TW_TOKEN_SECRET,
// timeout_ms: 60*1000, // optional HTTP request timeout to apply to all requests.
})
const app = express();
const port = process.env.PORT || 5000;
if (process.env.NODE_ENV === "production"){
app.use(express.static(path.join(__dirname, 'client/build')));
}
app.use(favicon(path.join(__dirname, 'client/src/media', 'favicon.ico')))
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
// Priority serve any static files.
var allowCrossDomain = function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With');
// intercept OPTIONS method
if ('OPTIONS' == req.method) {
res.sendStatus(200);
} else {
next();
}
};
app.use(allowCrossDomain);
app.get('/api/twitter/reverse_geocode', (req, res) => {
var parameters = {
lat: req.query.lat,
long: req.query.long,
}
Twitter.get('geo/reverse_geocode', parameters)
.then(response => {
res.send(response);
})
.catch(e => {
res.sendStatus(res.statusCode).send(e);
})
});
app.get('/api/twitter/search', (req, res) => {
let q = '';
if(req.query.q){
q = req.query.q;
}
let geocode = '';
if(req.query.geocode){
geocode = req.query.geocode;
}
let until = '';
if(req.query.until){
until = req.query.until;
}
var parameters = {
q : q,
geocode: geocode,
until: until,
lang: "en",
// result_type: "mixed",
count: 100,
include_entities: true,
}
// console.log(q);
console.log("Searching, q="+q+", geocode="+geocode+", until="+until)
Twitter.get('search/tweets', parameters)
.then(response => {
res.send(response);
})
.catch(e => res.status(500).send('Something broke!')
)
});
app.post('/api/sentiment', (req, res) => {
var parameters = {
method: 'POST',
uri: "http://www.sentiment140.com/api/bulkClassifyJson",
body : req.body,
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
json: true
}
promise(parameters)
.then(response => {
res.send(response)
})
.catch(e => {
console.log(e)
res.status(500).send('Something broke!')
});
});
if (process.env.NODE_ENV === "production"){
app.get('/*', (req, res) => {
res.sendFile(path.join(__dirname+'/client/build/index.html'));
});
}
app.listen(port, () => console.log(`Listening on port ${port}`))