Skip to content

Instantly share code, notes, and snippets.

@joyrexus
Last active November 26, 2018 16:48
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save joyrexus/7552861 to your computer and use it in GitHub Desktop.
Save joyrexus/7552861 to your computer and use it in GitHub Desktop.
Map a stream of tweets from location

Map tweets from a place

This is a very simple node-based client demonstrating how you can use the Twitter streaming API with the locations parameter specified. It uses geojson.io to map any tweets it hears within a user-specified duration.

Setup

Before running the map.coffee script, place your app's OAuth keys in keys.json and run npm install to install dependencies (viz., ntwitter and opener).

Usage

You can optionally specify the place from which the tweets should be coming and the time to listen in seconds:

map.coffee [place] [time]

For example, to map tweets coming from Chicago within the next 5 seconds:

map.coffee chicago 5
{ "consumer_key": "",
"consumer_secret": "",
"access_token_key": "",
"access_token_secret": "" }
#!/usr/bin/env coffee
Twitter = require 'ntwitter'
open = require 'opener'
keys = require 'keys'
spot = process.argv[2] or 'chicago' # tweets from which spot?
time = process.argv[3] or 10 # listen for how many secs?
tweet = new Twitter(keys)
geojson =
type: "FeatureCollection"
features: []
# add your own spots of interest here
bbox =
chicago:
locations: '-87,41,-86,42'
nashville:
locations: '-87,35,-85,36.1'
nyc:
locations: '-74,40,-73,41'
bayarea:
locations: '-122.75,36.8,-121.75,37.8'
# callback to map tweets from the resulting stream
map = (stream) ->
setTimeout(stream.destroy, time * 1000) # disconnect after x seconds
stream.on 'data', (data) ->
if data.coordinates?
feature =
type: "Feature"
geometry: data.coordinates
properties:
text: data.text
geojson.features.push(feature)
stream.on 'end', ->
open 'http://geojson.io/' +
'#data=data:application/json,' +
encodeURIComponent JSON.stringify(geojson)
# map tweets within specified bounding box
tweet.stream('statuses/filter', bbox[spot], map)
{
"name": "tweet-map",
"version": "0.0.1",
"description": "Map tweets from a particular location",
"keywords": [
"twitter",
"stream",
"geojson"
],
"dependencies": {
"opener": "1.3.x",
"ntwitter": "0.5.x",
"coffee-script": "1.6.3"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment