forked from mgingras/fBomb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoffeeApp.coffee
167 lines (141 loc) · 4.72 KB
/
coffeeApp.coffee
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
express = require 'express'
routes = require './routes'
### newrelic = require 'newrelic' ###
http = require 'http'
path = require 'path'
util = require 'util'
compressor = require 'node-minify'
grunt = require 'grunt'
twit = require 'twit'
# Grunt task
grunt.loadNpmTasks 'grunt-contrib-coffee'
grunt.tasks [], {}, ->
grunt.log.ok "Grunt: Done running tasks!"
# Expres
app = express()
# Configs
app.set 'port', process.env.PORT || 3000
app.set 'views', path.join __dirname, 'views'
app.set 'view engine', 'jade'
app.use express.logger 'dev'
app.use express.bodyParser()
app.use express.methodOverride()
app.use app.router
app.use express.static(path.join(__dirname, "public"))
# Minify
new compressor.minify {
type: 'uglifyjs',
fileIn: 'assets/js/fBomb.js',
fileOut: 'public/js/fBomb.min.js',
callback: (err) -> if err
console.log 'minify: ' + err
}
new compressor.minify {
type: 'yui-css',
fileIn: 'public/css/add2home.css',
fileOut: 'public/css/add2home.min.css',
callback: (err) -> if err
console.log 'minify: ' + err
}
# Dev config
app.configure 'development', ->
app.use express.errorHandler {
dumpExceptions: true,
showStack: true
}
app.locals.pretty = true
# Prod config
app.configure 'production', ->
app.use express.errorHandler()
Twitter = new twit {
consumer_key: "Y7oXngp2Ef4nqfczM2vGaA",
consumer_secret: "SHzys12htIXq3f6NE72XBal2uEcZviDGfFK1SA3pSg",
access_token: "21124143-jagMCpGZ7507QroTSCGqmNb3MMEei9K0jQQfcKK1U",
access_token_secret: "0jF0UYnxb4FPnjOz2V4pqEUn2B5QZIX5NKpTJNHAEuv2h"
}
# Temporary storage of tweets
tweets = []
retweets = []
# Clears cache of tweets every 5 seconds
eraseTweets = -> tweets = []
setInterval eraseTweets, 5000
# Returns tweets to client
`getTweets = function() {return tweets;}`
stream = Twitter.stream 'statuses/filter', {track:'fuck,fucker,fucking,fucked,fucks'}
id = 0
# Logic to get tweets
stream.on 'tweet', (tweet) ->
if tweet.coordinates
tweets.push {text: "@" + tweet.user.screen_name + " : " + tweet.text, coordinates: tweet.coordinates.coordinates, id:id++}
### retweet tweet.user.screen_name, tweet.id_str, tweet.user.followers_count ###
else if tweet.place
if tweet.place.bounding_box
if tweet.place.bounding_box.type is 'Polygon'
centerPoint tweet.place.bounding_box.coordinates[0], (center) ->
tweets.push {text: "@" + tweet.user.screen_name + " : " + tweet.text, coordinates: center, id:id++}
### retweet tweet.user.screen_name, tweet.id_str, tweet.user.followers_count ###
else
console.log 'WTF_Place: ' + util.inspect tweet.place
else
console.log 'placeWithNoBoundingBox' + util.inspect tweet.place
stream.on 'limit', (limitMessage) ->
console.log "mgingras (limit): "
console.log limitMessage
stream.on 'warning', (warning) ->
console.log "mgingras (warning): "
console.log warning
stream.on 'disconnect', (disconnectMessage) ->
console.log "mgingras (disconnect): "
console.log disconnectMessage
stream.on 'reconnect', (req, res, connectInterval) ->
console.log "mgingras (reconnect): "
console.log "Reqeuest: "
console.log req
console.log "Response: "
console.log res
console.log "Connection Interval: "
console.log connectInterval
# Calculate the center of a bounding box for tweet
centerPoint = (coords, callback) ->
centerPointX = 0
centerPointY = 0
for coord in coords
centerPointX += coord[0]
centerPointY += coord[1]
callback [centerPointX / coords.length, centerPointY / coords.length]
# Array of re-tweeted screen_names to avoid spam
twitterUsernameArray = []
limit = 1
# Reset the limit of retweets every 5 minutes
resetLimit = -> limit = 1
# setInterval resetLimit, 20000
setInterval resetLimit, 300000
# Retweet logic
retweet = (screen_name, tweetID, followers) ->
if limit isnt 0 && retweets.length > 0
limit--
if twitterUsernameArray[screen_name]
console.log screen_name + " mgingras: Already Retweeted!"
else
mostPopular = 0
index = 0
for tweet in retweets
if tweet.followers >= mostPopular
mostPopular = tweet.followers
index = _i
Twitter.post 'statuses/retweet/:id', { id: retweets[index].tweetID }, (err) ->
if err
console.log "mgingras (Retweet Error): "
console.log err
else
twitterUsernameArray[screen_name] = retweets[index].screen_name
retweets = []
else
retweets.push {screen_name: screen_name, tweetID: tweetID, followers: followers}
# Routes
app.get '/', routes.index
app.get '/data', routes.data
http.createServer(app).listen app.get('port'), ->
console.log 'Express server listening on port ' + app.get 'port'
process.on 'uncaughtException', (err) ->
console.log 'Uncaught Error!!! : ' + err