Created
January 22, 2011 22:00
-
-
Save ashaw/791532 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# much of this copied from | |
# http://adam.heroku.com/past/2010/3/19/consuming_the_twitter_streaming_api/ | |
require 'eventmachine' | |
require 'em-http' | |
require 'json' | |
class AndyMZork | |
MATCH_PREFIX = /(^|\s+)/ | |
MATCH_SUFFIX = /([[:punct:]]|\s+|$)/ | |
MATCHERS = { | |
/(?:I|me)/ => 'you', | |
/my/ => 'your', | |
/mine/ => 'yours', | |
/am/ => 'are', | |
/we\'ll/ => 'you\'ll', | |
/I\'d/ => 'you\'d' | |
} | |
attr_reader :tweet | |
def initialize(tweet) | |
matched = false | |
MATCHERS.each do |k,v| | |
if tweet =~ /#{MATCH_PREFIX}#{k}#{MATCH_SUFFIX}/i | |
tweet.gsub!(/#{MATCH_PREFIX}#{k}#{MATCH_SUFFIX}/i, '\1' + v + '\2') | |
matched = true | |
end | |
end | |
@tweet = tweet if matched == true | |
end | |
def self.frobozz(tweet) | |
q = self.new(tweet) | |
q.tweet | |
end | |
end | |
usage = "#{$0} <user> <password>" | |
abort usage unless user = ARGV.shift | |
abort usage unless password = ARGV.shift | |
url = 'http://stream.twitter.com/1/statuses/filter.json?follow=17687524' | |
def handle_tweet(tweet) | |
return unless tweet['user']['screen_name'] == "andymboyle" | |
zorkd_tweet = AndyMZork.frobozz(tweet['text']) | |
unless zorkd_tweet.nil? | |
puts ">> #{zorkd_tweet}" | |
end | |
end | |
EventMachine.run do | |
http = EventMachine::HttpRequest.new(url).get :head => {'Authorization' => [ user, password ]} | |
buffer = "" | |
http.stream do |chunk| | |
buffer += chunk | |
while line = buffer.slice!(/.+\r?\n/) | |
handle_tweet JSON.parse(line) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment