-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
217 lines (185 loc) · 6.83 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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
var Twitter = require('twitter');
var mongo = require('mongodb');
var client = new Twitter({
//for Neel
/*consumer_key: 'Ktr2wcSmylc2YAl8aZddwXaxz',
consumer_secret: 'XFezblfiqxHkcbt2IwNV631e5zkqaGCckvt0tTimYSnghTNrau',
access_token_key: '95264379-0CVoAxjjbc3zYnut2MJoUl7k8WLysZmyCCUncVtBB',
access_token_secret: 'Y2DTSjuGvUTW5eU0JxO2Jf1ZVNHnqKvNfWj3341GA6lxX'*/
//for Flash
consumer_key: 'zXPRIkYkoFyWNZ3Lh6Q5FHo3r',
consumer_secret: 'So4XZBRxuulKSKpZF6XUe3CHubyd8qBMnoMP4VGkirl7puI3vj',
access_token_key: '749522238651629568-xdGPgcPUOBPOyyoW6KmXsXrJMltlWXD',
access_token_secret: 'KSc1gpRcbOpPFtaKrBQA2NnFhkXnImnN5ac9nwXIM4Cwh'
});
/*
var mongoUri = process.env.MONGOLAB_URI ||
process.env.MONGOHQ_URL ||
'mongodb://neelkadia:[email protected]:11765/feedmyflash';
var mongoUri = process.env.MONGOLAB_URI || 'mongodb://neelkadia:[email protected]:11765/feedmyflash'
*/
var databaseUrl = "mongodb://neel:[email protected]:11765/feedmyflash";
var mongojs = require('mongojs')
var db = mongojs(databaseUrl);
var feed = db.collection('feed')
var Particle = require('particle-api-js');
var particle = new Particle();
var token ;
var sparkData= "NO";
particle.login({username: '[email protected]', password: 'nokia7020'}).then(
function(data){
console.log('API call completed on promise resolve: ', data.body.access_token);
token = data.body.access_token;
},
function(err) {
console.log('API call completed on promise fail: ', err);
}
);
var yes_counter = 0;
var no_counter = 0;
var currentHour = 0;
function getDateTime() {
//Getting IST Time
var currentTime = new Date();
var currentOffset = currentTime.getTimezoneOffset();
var ISTOffset = 330; // IST offset UTC +5:30
var ISTTime = new Date(currentTime.getTime() + (ISTOffset + currentOffset)*60000);
// ISTTime now represents the time in IST coordinates
//var hoursIST = ISTTime.getHours()
//var minutesIST = ISTTime.getMinutes()
var year = ISTTime.getFullYear();
var month = ISTTime.getMonth()+1;
var day = ISTTime.getDate();
var hour = ISTTime.getHours();
var minute = ISTTime.getMinutes();
var second = ISTTime.getSeconds();
if(month.toString().length == 1) {
var month = '0'+month;
}
if(day.toString().length == 1) {
var day = '0'+day;
}
if(hour.toString().length == 1) {
var hour = '0'+hour;
}
if(minute.toString().length == 1) {
var minute = '0'+minute;
}
if(second.toString().length == 1) {
var second = '0'+second;
}
//var dateTime = year+'/'+month+'/'+day+' '+hour+':'+minute+':'+second;
var dateTime = year+'/'+month+'/'+day;
return dateTime;
}
function getFullDateTime() {
//Getting IST Time
var currentTime = new Date();
var currentOffset = currentTime.getTimezoneOffset();
var ISTOffset = 330; // IST offset UTC +5:30
var ISTTime = new Date(currentTime.getTime() + (ISTOffset + currentOffset)*60000);
// ISTTime now represents the time in IST coordinates
//var hoursIST = ISTTime.getHours()
//var minutesIST = ISTTime.getMinutes()
var year = ISTTime.getFullYear();
var month = ISTTime.getMonth()+1;
var day = ISTTime.getDate();
var hour = ISTTime.getHours();
var minute = ISTTime.getMinutes();
var second = ISTTime.getSeconds();
if(month.toString().length == 1) {
var month = '0'+month;
}
if(day.toString().length == 1) {
var day = '0'+day;
}
if(hour.toString().length == 1) {
var hour = '0'+hour;
}
if(minute.toString().length == 1) {
var minute = '0'+minute;
}
if(second.toString().length == 1) {
var second = '0'+second;
}
var dateTime = year+'/'+month+'/'+day+' '+hour+':'+minute+':'+second;
//var dateTime = year+'/'+month+'/'+day;
return dateTime;
}
function getDateTimeIST() {
//Getting IST Time
var currentTime = new Date();
var currentOffset = currentTime.getTimezoneOffset();
var ISTOffset = 330; // IST offset UTC +5:30
var ISTTime = new Date(currentTime.getTime() + (ISTOffset + currentOffset)*60000);
return ISTTime;
}
currentHour = getDateTimeIST().getHours();
//console.log(getDateTime());
//console.log(hoursIST + ":" + minutesIST + " : "+getDateTime());
//var db = require('mongojs').connect('mongodb://neelkadia:[email protected]:11765/feedmyflash');
/**
* Stream statuses filtered by keyword
* number of tweets per second depends on topic popularity
**/
client.stream('statuses/filter', {track: 'FeedMyFlash'}, function(stream) {
stream.on('data', function(tweet) {
if(tweet.text.search(/yes/i)>0){
yes_counter++;
}else if(tweet.text.search(/no/i)>0){
no_counter++;
}
//console.log("Yes:"+yes_counter + " No:"+no_counter);
});
stream.on('error', function(error) {
console.log(error);
});
});
setInterval(function(){
//code goes here that will be run every 5 seconds.
//Update database
///console.log("Update called!" + " " + "govt:"+yes_counter + " india:"+no_counter);
//console.log('setInterval yes_counter:'+yes_counter + ', no_counter:'+no_counter);
feed.findAndModify({
query: { date:getDateTime(), startHour: getDateTimeIST().getHours() },
update: { $set: { yes: yes_counter, no: no_counter} },
new: true
}, function (err, doc, lastErrorObject) {
// doc.tag === 'maintainer'
});
//END Update Database
//Check and update counter
if(currentHour != getDateTimeIST().getHours()){
//Check Feed or Not feed?
if(yes_counter>no_counter){
sparkData = "YES";
}else{
sparkData = "NO";
}
//Send a Feed/NoFeed data to Particle
var fnPr = particle.callFunction({ deviceId: '440034000e47343233323032', name: 'tweet', argument: sparkData, auth: token });
fnPr.then(
function(data) {
//console.log('Function called succesfully:', data);
//Post a Tweet about Feeding or not feeding
client.post('statuses/update', {status: getFullDateTime()+" #FeedMyFlashStatus: " + "Yes:"+yes_counter+", No:"+no_counter + " Feeding? = "+sparkData}, function(error, tweet, response){
if (!error) {
//console.log(tweet);
}else{
console.log(error);
//console.log(response);
}
//Clear counters and log that update happen
// console.log('last1 yes_counter:'+yes_counter + ', no_counter:'+no_counter);
yes_counter = 1;
no_counter = 0;
sparkData = "NO";
currentHour = getDateTimeIST().getHours();
});
//End Post a Tweet about Feeding or not feeding
}, function(err) {
console.log('An error occurred:', err);
});
//End Send a Feed/NoFeed data to Particle
}//END Check and update counter
}, 300000); // Make it 600000 mss