public
Last active

  • Download Gist
cobot_twitter.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.