-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
142 lines (117 loc) · 3.06 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
138
139
140
141
142
'use strict';
// Load dev environment if not on Bluemix
if (!process.env.VCAP_SERVICES) {
require('dotenv').load();
}
// Required Libraries
var Path = require('path');
var Hapi = require('hapi');
var Inert = require('inert');
// Instantiate the server
var server = new Hapi.Server({
debug: {
request: ['error','good'],
},
connections: {
routes: {
files: {
relativeTo: Path.join(__dirname, 'public'),
},
},
},
});
// Set Hapi Connections
server.connection({
host: process.env.VCAP_APP_HOST || 'localhost',
port: process.env.VCAP_APP_PORT || 3000,
});
// Hapi Log
server.log(['error', 'database', 'read']);
// Register Hapi Plugins
server.register(Inert, function() {});
// Static site
server.route({
method: 'GET',
path: '/{param*}',
handler: {
directory: {
path: '.',
redirectToSlash: true,
index: true,
},
},
});
// Start Hapi
server.start(function(err) {
if (err) {
throw err;
}
console.log('Server running at:', server.info.uri);
});
// Scan Twitter
function getTweets(search, id) {
console.log(Date.now() + " -- " + id);
var params = {
q: search,
include_entities: false
};
if (typeof id !== 'undefined') {
params.since_id = id;
}
twitterClient.get('search/tweets', params, function(error, tweets, response){
if (!error) {
tweets.statuses.forEach(function(index){
if (((typeof id == 'undefined')? 0 : id) != index.id) {
twitterClient.get('statuses/oembed', {id: index.id_str}, function(embederror, embed, response){
if (!embederror) {
// console.log(embed);
pusher.trigger('stream', 'twitter', {
id: index.id_str,
tweet: embed.html,
});
} else {
console.warn(embederror);
}
});
}
});
setTimeout(function () {
if (tweets.statuses.length > 0) {
var since_id = (typeof id == 'undefined')? 0 : id;
for(var i = 0; i < tweets.statuses.length; i++) {
if (tweets.statuses[i].id > since_id) {
var since_id = tweets.statuses[i].id;
console.log(tweets.statuses[i].id);
}
}
// var since_id = tweets.statuses[tweets.statuses.length - 1].id_str;
getTweets(search, since_id);
} else {
getTweets(search, id);
}
}, 5000);
} else {
getTweets(search, id);
}
});
}
if (process.env.TWITTER && process.env.PUSHER) {
var TWITTER = JSON.parse(process.env.TWITTER);
var PUSHER = JSON.parse(process.env.PUSHER);
var Twitter = require('twitter');
var Pusher = require('pusher');
var twitterClient = new Twitter({
consumer_key: TWITTER.cKey,
consumer_secret: TWITTER.cSecret,
access_token_key: TWITTER.aToken,
access_token_secret: TWITTER.aSecret,
});
var pusher = new Pusher({
appId: PUSHER.appId,
key: PUSHER.key,
secret: PUSHER.secret,
encrypted: PUSHER.encrypted
});
pusher.port = PUSHER.port;
getTweets('#hackference');
}