-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
117 lines (94 loc) · 2.37 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
var json = require('jsonfile');
var Twit = require('twit');
var express = require('express');
var BodyParser = require('body-parser');
var cors = require('cors');
// var file = './list.json';
var cred = require('./credentials');
var app = express();
var router = express.Router();
app.use(BodyParser.urlencoded({extended: false}));
app.use(cors());
var T = new Twit({
consumer_key: cred.consumer_key,
consumer_secret: cred.consumer_secret,
access_token: cred.access_token,
access_token_secret: cred.access_token_secret
})
var messages = [];
var defaultMessages = [
"Welcome to Digital Underground!",
"Digital Underground - the Digital Media Design grad show!",
"We're open Thurs 16th and Fri 17th, 11am - 6pm",
"Tweet us with the hashtag #BUdigital",
"Find us online at digitalunderground.co"
];
Array.prototype.move = function(from, to) {
this.splice(to, 0, this.splice(from, 1)[0]);
};
function pusher(message, keep){
messages.push({
message:message,
keep: keep,
tweet: false
});
// json.writeFile(file,messages,function(err){
// if (err){
// console.log(err)
// } else {
// console.log("Message Saved")
// };
// })
}
var tweetFire = 10;
var tweetIteration = tweetFire;
var twitterQuery = "#BUdigital"
function twitGet(){
if (tweetIteration == 0 ){
T.get('search/tweets', { q: twitterQuery, count: 1}, function(err, data, response){
messages.push({
message: data.statuses[0].text,
keep: 0,
tweet: true
})
});
tweetIteration = tweetFire;
} else {
tweetIteration--;
}
}
app.use(express.static(__dirname + '/public'));
app.get('/', function(req, res){
twitGet();
if (!messages.length == 0) {
res.send({
text: messages[0].message,
tweet: messages[0].tweet
});
console.log(messages);
if (messages[0].keep > 0){
messages[0].keep --;
messages.move(0,messages.length );
} else if (messages[0].keep == 0) {
messages.splice(0,1);
} else if (messages[0].keep < 0){
messages[0].keep = 1;
}
} else {
var rand = Math.floor(Math.random() * defaultMessages.length);
res.send({
text: defaultMessages[rand],
tweet: false
});
}
});
app.post('/', function(req, res){
var message = req.body.message;
var keep = req.body.keep;
console.log(message + " --Keep? " + keep);
pusher(message, keep);
res.send("Message Sent");
})
app.use('/', router);
app.listen(3120);
console.log("Running on port 3120");