Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
92 lines (82 loc) · 2.99 KB
/
Copy pathserver.js
File metadata and controls
92 lines (82 loc) · 2.99 KB
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
var express = require('express'),
app = express(),
server = require('http').createServer(app),
twitterAPI = require('node-twitter-api'),
_ = require('underscore')
//---------- set up Twitter stuff
if (!process.env.TWITTER_CONSUMER_KEY ||
!process.env.TWITTER_CONSUMER_SECRET ||
!process.env.TWITTER_ACCESS_TOKEN ||
!process.env.TWITTER_ACCESS_TOKEN_SECRET) {
console.log('ERROR: TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET, TWITTER_ACCESS_TOKEN, and TWITTER_ACCESS_TOKEN_SECRET must be set in your environment.')
process.exit()
}
var twitter = new twitterAPI({
consumerKey: process.env.TWITTER_CONSUMER_KEY,
consumerSecret: process.env.TWITTER_CONSUMER_SECRET,
callback: 'http://localtest.me:8080/oauth'
})
var
token = process.env.TWITTER_ACCESS_TOKEN,
secret = process.env.TWITTER_ACCESS_TOKEN_SECRET
// twitter.verifyCredentials(token, secret, function(error, data, response) {
// if (error) {
// console.log('ERROR: Invalid Twitter access token and/or access token secret.')
// console.log(error)
// process.exit()
// } else {
// console.log('SUCCESS: Twitter credentials are valid.')
// }
// })
//---------- start listening
var port = process.env.PORT || 8080
console.log("Listening on port", port)
server.listen(port)
app.use(express.bodyParser())
app.use(express.logger())
app.use(express.static(__dirname + "/public"))
app.post('/tweets-for-meme', tweetsForMeme)
app.post('/mock-tweets-for-meme', mockTweetsForMeme)
function tweetsForMeme(req, res) {
console.log("This is called TweetsForMeme")
var meme = "#" + req.body.meme
twitter.search({q: meme, count: 100, result_type: 'mixed'}, token, secret, function(error, data, response) {
if (error) {
console.log("ERROR:", error, response)
var json = JSON.stringify({ message: response })
res.statusCode = 500
res.setHeader('Content-Type', 'application/json')
res.end(json)
} else {
var topTenTweets = _.sortBy(data.statuses, function(status) {
return status.retweet_count
}).reverse().slice(0, 10)
var json = JSON.stringify(topTenTweets)
res.setHeader('Content-Type', 'application/json')
res.end(json)
}
})
}
function mockTweetsForMeme(req, res) {
console.log("This is hit")
var topTweets = [
{ id: 410854284670955500,
id_str: "410854284670955520",
text: "The Dark Knight Light. #AddaWordRuinaMovie",
retweet_count: 26,
user: { screen_name: "jason_s_dolley" } },
{ id: 411342778921406460,
id_str: "411361322493042688",
text: "The Cat 'Tis Came Back #AddaWordRuinaMovie",
retweet_count: 0,
user: { screen_name: "AddAWordBot" } },
{ id: 411343492468580350,
id_str: "411361876015329280",
text: "#AddaWordRuinaMovie Gone with the Wind Farm http://t.co/xCPwRKkOss",
retweet_count: 0,
user: { screen_name: "garciavet" } }
]
var json = JSON.stringify(topTweets)
res.setHeader('Content-Type', 'application/json')
res.end(json)
}