-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
110 lines (81 loc) · 2.41 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
require('dotenv').config()
const path = require('path');
const express = require('express');
const Request = require ('request');
const app = express();
app.use(express.static('./www'));
app.get('/',function (req,res){
res.sendFile(path.join(__dirname, 'index.html'));
});
app.get('/api/tweets', function(req, res){
let tags = process.env.Tags;
let qs = {
q : tags.replace(' ', ' OR '),
result_type: "recent",
include_entities: 1,
count: 100
};
let options = {
url: 'https://api.twitter.com/1.1/search/tweets.json?',
qs : qs,
method: 'GET',
headers: {
Authorization: `Bearer ${token}`
}
};
if(req.query.since_id !== undefined)
{
options.qs = req.query;
options.qs.count = 100;
options.url = 'https://api.twitter.com/1.1/search/tweets.json?';
}
Request(options,(err, response, payload)=> {
var jPayload = JSON.parse(payload);
var list = jPayload.statuses.map(mapToTweet);
var refresh = jPayload.search_metadata.refresh_url;
res.json({
tweets : list,
refreshUrl : refresh,
});
});
});
app.get('/api/tags', function(req,res){
let tags = process.env.Tags.replace(' ', ', ');
res.json(tags);
})
app.get('/api/authenticate', function(req,res){
var body = "grant_type=client_credentials";
let options = {
url: "https://api.twitter.com/oauth2/token",
method: "POST",
body: body,
headers:{
"Content-Type" : "application/x-www-form-urlencoded;charset-UTF-8",
Authorization: `Basic ${process.env.Authorization_Key}`
}
};
Request(options, (error, response, payload) => {
token = JSON.parse(payload).access_token;
res.sendStatus(200);
});
});
var port = process.env.PORT || 3000;
app.listen(port, function(){
console.log(`Listening on port ${port}`);
});
let token = '';
function mapToTweet(status) {
let imgUrl = '';
if(status.entities.media != null && status.entities.media.length > 0)
{
imgUrl = status.entities.media[0].media_url_https;
}
return {
createdAt : status.created_at,
name : status.user.name,
screenName : status.user.screen_name,
text: status.text,
profileImageUrl : status.user.profile_image_url,
imageUrl : imgUrl
};
};