Skip to content

Instantly share code, notes, and snippets.

@harvesthq
Created March 5, 2010 15:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save harvesthq/322815 to your computer and use it in GitHub Desktop.
Save harvesthq/322815 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'net/http'
require 'base64'
require 'cgi'
require 'time'
require 'json'
###### CO-OP CONNECTIONS AND METHODS ######
@co_op_key = 'you_need_your_own'
@co_op_headers = {
'Accept' => 'application/json',
'Content-Type' => 'application/json; charset=utf-8',
'User-Agent' => 'Harvest Twitter Script'
}
@co_op_connection = Net::HTTP.new("coopapp.com", 80)
@co_op_connection.open_timeout = 10
def post_to_co_op(message)
@co_op_connection.post('/groups/5/notes', {:status => message, :key => @co_op_key}.to_json, @co_op_headers)
end
def co_op_statuses
return @co_op_statuses unless @co_op_statuses.nil?
response = @co_op_connection.get("/groups/5/users/cobot?key=#{@co_op_key}", @co_op_headers)
@co_op_statuses = JSON.parse(response.body).map{|s| s['text']}
end
###### TWITTER PROCESSING ######
MAX_TWEETS = 3
TWITTER_ID = 'your_twitter_id'
def harvest_tweet(status)
"Harvest Tweet: #{status['text']}"
end
def fave_tweet(status)
"Fave Tweet: #{status['text']}"
end
begin
twitter_connection = Net::HTTP.new("api.twitter.com", 80)
twitter_connection.open_timeout = 10
# Gather our tweets excluding @replies and those already posted. Limit to 3 to avoid a Cobot Bomb (tm).
response = twitter_connection.get("/1/statuses/user_timeline/#{TWITTER_ID}.json?count=#{MAX_TWEETS}")
statuses = JSON.parse(response.body)[0,MAX_TWEETS].select{|s| !(s['text'] =~ /^@/) && !co_op_statuses.include?(harvest_tweet(s))}
# Gather our favorites excluding those already posted. Limit to 3 to avoid a Cobot Bomb (tm).
response = twitter_connection.get("/1/favorites/#{TWITTER_ID}.json?count=#{MAX_TWEETS}")
favorites = JSON.parse(response.body)[0,MAX_TWEETS].select{|s| !co_op_statuses.include?(fave_tweet(s))}
statuses.each do |status|
post_to_co_op(harvest_tweet(status))
end
favorites.each do |status|
post_to_co_op(fave_tweet(status))
end
rescue Timeout::Error => e
# Placeholder in case we want to log messages later
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment