forked from manoharganta256/MyTwitterHandler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
108 lines (81 loc) · 2.56 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
var express = require('express');
const app = express();
const port = process.env.PORT || 3000;
const bodyParser = require('body-parser');
var Twitter = require('twitter');
const hbs = require('hbs');
/*
hbs.registerHelper('for', function(from, to, block){
var accum = '';
for(var i = from; i < to; i++)
accum += block.fn(i);
return accum;
});
*/
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
app.use(express.static(__dirname + '/public'));
var tweetRequestRoutes = require('./api/routes/tweetRequestRoutes');
tweetRequestRoutes(app);
var postedTweetRoutes = require('./api/routes/postedTweetRoutes');
postedTweetRoutes(app);
app.listen(port);
app.get('/', function (req, res) {
res.render('index.hbs')
});
var client = new Twitter({
consumer_key: process.env.TWITTER_CONSUMER_KEY,
consumer_secret: process.env.TWITTER_CONSUMER_SECRET,
access_token_key: process.env.TWITTER_ACCESS,
access_token_secret: process.env.TWITTER_ACCESS_SECRET
});
app.post('/', function(req, res){
console.log(req.body.search);
var twats = [];
client.get('search/tweets', {q: req.body.search}, function(error, tweets, response) {
tweets.statuses.forEach(function(tweet) {
//console.log("tweet: ", JSON.stringify(tweet))
var twat = {
'uname': tweet.user.screen_name,
'id': tweet.id_str,
'text': tweet.text
};
twats.push(twat);
});
console.log(twats);
res.render('index.hbs', {data: twats});
});
});
app.post('/fav', function(req, res){
var id = req.body.twitid.trim();
var uname = req.body.uname;
client.post('favorites/create', {id}, function(err, response){
// If the favorite fails, log the error message
if(err){
console.log(err[0].message);
}
// If the favorite is successful, log the url of the tweet
else{
var send = 'Favourited: '+'https://twitter.com/'+uname+'/status/'+id;
res.render('index.hbs', {fav: send});
}
});
});
app.post('/retweet', function(req, res){
var id = req.body.twitid.trim();
var uname = req.body.uname;
client.post('statuses/retweet', {id}, function(err, response){
// If the favorite fails, log the error message
if(err){
console.log(err[0].message);
}
// If the favorite is successful, log the url of the tweet
else{
var send = 'Retweeted: '+'https://twitter.com/'+uname+'/status/'+id;
res.render('index.hbs', {retweet: send});
}
});
});
console.log('SimpleRTAppAPI server started on: ' + port);