-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
98 lines (78 loc) · 1.72 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
var express = require('express')
var app = express()
var redis = require("redis"),
r = redis.createClient();
var twit = require('twit')
var t = new twit({
consumer_key: process.env.twitter_search_consumer_key,
consumer_secret: process.env.twitter_search_consumer_secret,
access_token: process.env.twitter_search_access_token,
access_token_secret: process.env.twitter_search_access_token_secret,
timeout_ms: 60*1000, // optional HTTP request timeout to apply to all requests.
})
app.get('/', function (req, res) {
res.send('Hello World')
})
app.get('/tweets', function (req, res) {
var q = req.query['s'];
var searchTerm = "*" + q + "*";
r.keys(searchTerm, function (err, keys)
{
var s = "";
r.mget(keys, function(err, v)
{
if (v)
{
v.forEach(function(item, i)
{
var json = JSON.parse(item);
if (json.entities.urls.length > 0)
{
s += json.text + "| <a href='" + json.entities.urls[0].url + "'>" + "tweet</a>" + "<br><br>";
}
else
{
s += json.text + "<br>";
}
});
res.send(s);
}
else
{
res.send("")
}
});
})
})
function iterateTimeline(maxId)
{
var q;
if (maxId == undefined)
{
q = { user_id:57425501, count:100 };
}
else
{
q = { user_id:57425501, max_id:maxId, count:100 };
}
t.get('statuses/user_timeline', q, function(err, data, response)
{
if (data == undefined) { return; }
for (i in data)
{
var item = data[i];
r.set(item.text, JSON.stringify(item));
}
if (data.length > 0)
{
var nextMaxId = data[data.length - 1].id - 1;
iterateTimeline(nextMaxId);
}
else
{
console.log("done.");
}
});
}
iterateTimeline();
app.listen(3000)