Created
July 9, 2012 21:42
-
-
Save paulbjensen/3079191 to your computer and use it in GitHub Desktop.
A script used to transmit geo-located tweets about Nike to Dashku
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# The script that I use to broadcast tweets about | |
# Nike to Dashku's demo account, in CoffeeScript. | |
# require some npm libraries | |
Twit = require 'twit' | |
analyze = require('Sentimental').analyze | |
Placefinder = require 'placefinder' | |
dashku = require 'dashku' | |
# Set your API Key | |
dashku.setApiKey 'YOUR_DASHKU_API_KEY', -> | |
# Setup a Twit constructor | |
twit = new Twit | |
consumer_key: 'TWIITER_CONSUMER_KEY' | |
consumer_secret: 'TWIITER_CONSUMER_SECRET' | |
access_token: 'TWIITER_ACCESS_TOKEN' | |
access_token_secret: 'TWITTER_ACCESS_TOKEN_SECRET' | |
# Setup a Placefinder constructor, and set the Yahoo Api key | |
placefinder = new Placefinder | |
# You'll need an API key to use Yahoo's placefinder API. | |
# For more, see http://developer.yahoo.com/geo/placefinder/ | |
placefinder.appID = "YOUR_YAHOO_API_KEY" | |
# Setup the Twitter streaming API to filter for tweets about 'Nike' | |
stream = twit.stream 'statuses/filter', track: 'Nike' | |
# Execute on the tweet event being emitted | |
stream.on 'tweet', (tweet) -> | |
location = tweet.user.location | |
data = | |
_id: "YOUR_DASHKU_WIDGET_ID" | |
sentiment: analyze(tweet.text).score | |
text: tweet.text | |
tweet_id: tweet.id_str | |
screen_name: tweet.screen_name | |
# don't geocode the location if it is blank or vague. | |
if location is '' or location.match?('everywhere') or location.match('worldwide') | |
data.location = null | |
dashku.transmission data, (response) -> | |
# output the response. For more, see | |
# https://github.com/paulbjensen/dashku-node | |
console.log response | |
else | |
# Let's try and find the location's latitude and longitude, for use with a map. | |
placefinder.location location, (err, result) -> | |
if err? | |
data.location = null | |
dashku.transmission data, (response) -> | |
# output the response. For more, see | |
# https://github.com/paulbjensen/dashku-node | |
console.log response | |
else | |
# Sets the location to the result object from Yahoo's placefinder API | |
data.location = result.ResultSet?.Results?[0] | |
dashku.transmission data, (response) -> | |
# output the response. For more, see | |
# https://github.com/paulbjensen/dashku-node | |
console.log response |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment