This repository has been archived by the owner on Aug 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
186 lines (143 loc) · 4.83 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
184
185
186
// REQUIRED MODULES
const express = require( 'express' );
const https = require( 'https' );
const bodyParser = require( 'body-parser' );
const secrets = require( './secrets.json' );
const authSecrets = 'Basic ' + new Buffer( secrets.consumerKey + ':' + secrets.consumerSecret ).toString( 'base64' );
// EXPRESS
const app = express();
// MIDDLEWARE __________________________________________________________________
// body parser _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
app.use( bodyParser.urlencoded( {
extended: false
} ) );
// _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
// serve static assets _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
app.use( express.static( `${__dirname}/public` ) );
// _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
// error handling _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
app.use( ( err, req, res, next ) => {
console.error( err.stack );
res.status( 500 ).send( 'Something broke!' );
} );
// _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
// end point from where ticker should get the data
app.get( '/headlines.json', ( req, res ) => {
getToken( function ( err, data ) {
if ( err ) {
console.error( err.stack );
} else {
getTweets( data.access_token, function ( err, data ) {
if ( err ) {
console.error( err.stack );
} else {
res.json( filterData( data ) );
}
} );
}
} );
} );
// make the request to twitter through the server.
// use the node server
function getToken( callback ) {
const reqOptions = {
method: 'POST',
host: 'api.twitter.com',
path: '/oauth2/token',
headers: {
'Authorization': authSecrets,
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
}
};
const req = https.request( reqOptions, function ( res ) {
// console.log( `STATUS: ${res.statusCode}` );
// console.log( `HEADERS: ${JSON.stringify(res.headers)}` );
if ( res.statusCode != 200 ) {
callback( res.statusCode );
return;
}
let body = '';
res.on( 'data', ( chunck ) => {
body += chunck;
} );
res.on( 'end', () => {
try {
body = JSON.parse( body );
callback( null, body );
} catch ( err ) {
callback( err );
}
} );
} );
// check if the req generates an error and handle it in the async way by passing it to the callback.
req.on( 'error', ( err ) => {
callback( err );
} );
// write to the body of the post the required string from twitter.
req.write( 'grant_type=client_credentials' );
// close the request
req.end();
}
function getTweets( bearerToken, callback ) {
// console.log('getTweets', bearerToken );
const reqOptions = {
method: 'GET',
host: 'api.twitter.com',
path: '/1.1/statuses/user_timeline.json?screen_name=realDonaldTrump',
headers: {
'Authorization': 'Bearer ' + bearerToken
}
};
const req = https.request( reqOptions, ( res ) => {
if ( res.statusCode != 200 ) {
callback( res.statusCode );
return;
}
// console.log( `STATUS: ${res.statusCode}` );
let body = '';
res.on( 'data', ( chunck ) => {
body += chunck;
} );
res.on( 'end', () => {
try {
body = JSON.parse( body );
// console.log(body);
callback( null, body );
} catch ( err ) {
callback( err );
}
} );
} );
req.on( 'error', ( err ) => {
callback( err );
} );
req.end();
}
function filterData( data ) {
let filteredTweets = {
headlines: []
};
data = data.filter( function ( tweet ) {
return tweet.entities.urls.length == 1;
} );
data.forEach( function ( tweet ) {
// console.log(tweet);
let headline = {};
const text = tweet.text.substring( 0, ( tweet.text.indexOf( 'https://' ) - 1 ) );
headline.headline = text;
headline.url = tweet.entities.urls[ 0 ].url;
headline.time = tweet.created_at;
filteredTweets.headlines.push( headline );
} );
// data.forEach(function(item) {
// console.log('______________');
// console.log(item.text);
// console.log(item.created_at);
// console.log(item.entities.urls);
// });
console.log( filteredTweets );
return filteredTweets;
}
app.listen( 8080, () => {
console.log( 'listening on port: 8080.' );
} );