Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Rearrange files to use package.json and offer bin/twimap.js

  • Loading branch information...
commit 730ab0199c9e06904114e8bc369e95bafff5b5a4 1 parent 379cef7
Brandon Tilley authored
View
1  .gitignore
@@ -0,0 +1 @@
+node_modules/*
View
10 bin/twimap.js
@@ -0,0 +1,10 @@
+#!/usr/bin/env node
+
+var path = require('path');
+var fs = require('fs');
+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);
+map.run();
View
82 lib/live-twitter-map.coffee
@@ -0,0 +1,82 @@
+fs = require 'fs'
+sys = require 'sys'
+path = require 'path'
+express = require 'express'
+io = require 'socket.io'
+twitter = require 'twitter-node'
+
+exports.LiveTwitterMap = class LiveTwitterMap
+ constructor: (user, pass, port) ->
+ @create_web_server port
+ @create_twitter_streamer user, pass
+
+ # Number of tweets processed
+ @count = 0
+ # Time in seconds since the server started
+ @time = 0
+ # Backlog of tweets to send to the clients
+ @backlog = []
+
+ create_web_server: (port) =>
+ public = path.join path.dirname(fs.realpathSync(__filename)), './public'
+ # Set up the Express web server
+ app = express.createServer()
+ app.configure ->
+ app.use express.static public
+ # The client web page
+ app.get '/', (req, res) ->
+ res.sendfile path.join public, "client.htm"
+ app.listen port
+
+ # Attach Socket.IO to the web server
+ @socket = io.listen app
+
+ create_twitter_streamer: (user, pass) =>
+ # 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
+
+ run: =>
+ # Start the timer
+ setInterval (=> @time++), 1000
+ # Start reading from the Streaming API
+ @twit.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 = []
+
+ # Format the time in a M:SS format
+ format_time: (seconds) ->
+ minutes = Math.floor seconds / 60
+ seconds = seconds - (minutes * 60)
+ seconds = "0#{seconds}" if seconds < 10
+ "#{minutes}:#{seconds}"
View
0  client.coffee → lib/public/client.coffee
File renamed without changes
View
0  client.htm → lib/public/client.htm
File renamed without changes
View
0  coffee-script.js → lib/public/coffee-script.js
File renamed without changes
View
0  map_shadow.png → lib/public/map_shadow.png
File renamed without changes
View
28 package.json
@@ -0,0 +1,28 @@
+{
+ "name": "live-twitter-map",
+ "description": "Show tweets from the Twitter streaming API live on a map",
+ "keywords": ["twitter", "live", "map"],
+ "version": "1.0.0",
+ "homepage": "https://github.com/BinaryMuse/live-twitter-map",
+ "author": "Brandon Tilley <brandon@brandontilley.com> (http://brandontilley.com)",
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/BinaryMuse/live-twitter-map.git"
+ },
+ "engines": {
+ "node": ">= 0.4.0"
+ },
+ "directories": {
+ "lib": "./lib"
+ },
+ "main": "./lib/live-twitter-map",
+ "bin": {
+ "twimap": "./bin/twimap.js"
+ },
+ "dependencies": {
+ "twitter-node": "",
+ "coffee-script": "",
+ "express": "",
+ "socket.io": ""
+ }
+}
View
74 twitter.coffee
@@ -1,74 +0,0 @@
-#!/usr/bin/env coffee
-
-twitter = require 'twitter-node'
-sys = require 'sys'
-express = require 'express'
-io = require 'socket.io'
-fs = require 'fs'
-
-# Set up the Express web server
-app = express.createServer()
-app.configure ->
- app.use express.static(__dirname);
-# The client web page
-app.get '/', (req, res) ->
- res.sendfile "client.htm"
-app.listen 8080
-
-# Attach Socket.IO to the web server
-socket = io.listen app
-
-# Create the Twitter streaming connection
-twit = new twitter.TwitterNode
- user: process.env.TWITTER_USER
- password: process.env.TWITTER_PASS
- # Search for Geo-tagged tweets in the US
- locations: [-127, 25, -58, 49]
-
-# Number of tweets processed
-count = 0
-# Time in seconds since the server started
-time = 0
-# Backlog of tweets to send to the clients
-backlog = []
-# Start the timer
-setInterval (-> time++), 1000
-
-# Send the tweet backlog to the clients and reset the backlog
-broadcast_tweets = ->
- socket.broadcast tweets: backlog
- backlog = []
-
-# Format the time in a M:SS format
-format_time = (seconds) ->
- minutes = Math.floor seconds / 60
- seconds = seconds - (minutes * 60)
- seconds = "0#{seconds}" if seconds < 10
- "#{minutes}:#{seconds}"
-
-# 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
-
-# Start reading from the Streaming API
-twit.stream()
-# Send new tweets to the client every 1/4 second
-setInterval broadcast_tweets, 250
Please sign in to comment.
Something went wrong with that request. Please try again.