Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Update application to use Twitter OAuth and ntwitter

  • Loading branch information...
commit 9648fb859e54f7c0f406528045fff764452b0639 1 parent feec9af
@BinaryMuse authored
View
2  bin/twimap.js
@@ -6,5 +6,5 @@ var coffee = require('coffee-script');
var lib = path.join(path.dirname(fs.realpathSync(__filename)), '../lib');
var app = require(lib + '/live-twitter-map');
-var map = new app.LiveTwitterMap(process.env.TWITTER_USER, process.env.TWITTER_PASS, 8080);
+var map = new app.LiveTwitterMap(8080);
map.run();
View
71 lib/live-twitter-map.coffee
@@ -3,12 +3,11 @@ sys = require 'sys'
path = require 'path'
express = require 'express'
io = require 'socket.io'
-twitter = require 'twitter-node'
+twitter = require 'ntwitter'
exports.LiveTwitterMap = class LiveTwitterMap
- constructor: (user, pass, port) ->
+ constructor: (port) ->
@create_web_server port
- @create_twitter_streamer user, pass
# Number of tweets processed
@count = 0
@@ -18,60 +17,60 @@ exports.LiveTwitterMap = class LiveTwitterMap
@backlog = []
create_web_server: (port) =>
- public = path.join path.dirname(fs.realpathSync(__filename)), './public'
+ pubDir = path.join path.dirname(fs.realpathSync(__filename)), './public'
# Set up the Express web server
app = express.createServer()
app.configure ->
- app.use express.static public
+ app.use express.static pubDir
# The client web page
app.get '/', (req, res) ->
- res.sendfile path.join public, "client.htm"
+ res.sendfile path.join pubDir, "client.htm"
app.listen port
# Attach Socket.IO to the web server
@socket = io.listen app
- create_twitter_streamer: (user, pass) =>
+ stream: =>
# Create the Twitter streaming connection
- @twit = new twitter.TwitterNode
- user: user
- password: pass
- # Search for Geo-tagged tweets in the US
- locations: [-127, 25, -58, 49]
-
- # Listen to our Twitter stream for tweets
- # Process them only if tweet.coordinates exists
- @twit.addListener 'tweet', (tweet) =>
- if tweet.coordinates?
- @backlog.push
- lat: tweet.coordinates.coordinates[1]
- lon: tweet.coordinates.coordinates[0]
- text: tweet.text
- user: tweet.user.screen_name
- image: tweet.user.profile_image_url
- sys.puts "#{++@count} #{@format_time @time} -- @#{tweet.user.screen_name}: #{tweet.text}"
- @twit.addListener 'limit', (limit) ->
- sys.puts "LIMIT: #{sys.inspect limit}"
- @twit.addListener 'delete', (del) ->
- sys.puts "DELETE: #{sys.inspect del}"
- @twit.addListener 'end', (resp) =>
- sys.puts "END: #{resp.statusCode}"
- if resp.statusCode == 200
- @twit.stream()
- else
- setTimeout (=> @twit.stream()), 5 * 60 * 1000
+ api = new twitter
+ consumer_key: process.env.TWITTER_KEY
+ consumer_secret: process.env.TWITTER_SECRET
+ access_token_key: process.env.TWITTER_TOKEN
+ access_token_secret: process.env.TWITTER_TOKEN_SECRET
+ api.verifyCredentials (err, data) ->
+ throw err if err?
+ console.log "Authenticated with Twitter"
+ api.stream 'statuses/filter', locations: [-127, 25, -58, 49], (stream) =>
+ stream.on 'error', (err) =>
+ throw err
+ stream.on 'data', (tweet) =>
+ if tweet.coordinates?
+ @backlog.push
+ lat: tweet.coordinates.coordinates[1]
+ lon: tweet.coordinates.coordinates[0]
+ text: tweet.text
+ user: tweet.user.screen_name
+ image: tweet.user.profile_image_url
+ sys.puts "#{++@count} #{@format_time @time} -- @#{tweet.user.screen_name}: #{tweet.text}"
+ stream.on 'end', (resp) =>
+ sys.puts "END: #{resp.statusCode}"
+ if resp.statusCode == 200
+ @stream()
+ else
+ setTimeout (=> @stream()), 5 * 60 * 1000
run: =>
# Start the timer
setInterval (=> @time++), 1000
# Start reading from the Streaming API
- @twit.stream()
+ @stream()
# Send new tweets to the client every 1/4 second
setInterval @broadcast_tweets, 250
# Send the tweet backlog to the clients and reset the backlog
broadcast_tweets: =>
- @socket.broadcast tweets: @backlog
+ backlog = unescape(encodeURIComponent(JSON.stringify(@backlog)))
+ @socket.sockets.emit 'tweets', backlog
@backlog = []
# Format the time in a M:SS format
View
8 lib/public/client.coffee
@@ -14,11 +14,11 @@ $ ->
@setup_socket()
setup_socket: ->
- socket = new io.Socket
- socket.connect()
- socket.on 'message', (data) =>
+ socket = io.connect()
+ socket.on 'tweets', (tweets) =>
+ data = JSON.parse(decodeURIComponent(escape(tweets)))
# Iterate over the array of tweets stored in data.tweets and add them to the map
- for tweet in data.tweets
+ for tweet in data
auto_show = @random_percent() < @auto_show_chance
lat = tweet.lat
lng = tweet.lon
View
4 lib/public/coffee-script.js
2 additions, 2 deletions not shown
Please sign in to comment.
Something went wrong with that request. Please try again.