Skip to content

Instantly share code, notes, and snippets.

@mreinsch
Forked from harvesthq/cobot_twitter.rb
Created March 9, 2010 05:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mreinsch/326274 to your computer and use it in GitHub Desktop.
Save mreinsch/326274 to your computer and use it in GitHub Desktop.
require 'net/http'
require 'base64'
require 'cgi'
require 'json'
class CoBot
HEADERS = {
'Accept' => 'application/json',
'Content-Type' => 'application/json; charset=utf-8',
'User-Agent' => 'Harvest Twitter Script' }
def initialize(group_id, group_key)
@group_id = group_id
@group_key = group_key
@connection = Net::HTTP.new("coopapp.com", 80)
@connection.open_timeout = 10
end
def post(message, message_id)
message_id_pattern = /#{Regexp.escape(message_id)}$/
unless statuses.any?{|m| message_id_pattern =~ m}
status = "#{message} - #{key}"
@connection.post("/groups/#{@group_id}/notes", {:status => status, :key => @group_key}.to_json, HEADERS)
statuses << status
end
end
private
def statuses
@statuses ||= retrieve_statuses
end
def retrieve_statuses
response = @connection.get("/groups/#{@group_id}/users/cobot?key=#{@group_key}", HEADERS)
JSON.parse(response.body).map{|s| s['text']}
end
end
#!/usr/bin/env ruby
$KCODE = 'U'
require 'rubygems'
require 'twitter'
require 'cobot'
MAX_TWEETS = 3
cobot = CoBot.new(<group_id>, <group_secret>)
%w{mobalean kanjidamage keitai-dev}.each do |keyword|
Twitter::Search.new(keyword).per_page(MAX_TWEETS).each do |r|
cobot.post("@#{r['from_user']} mentioned #{keyword}: \"#{r['text']}\"",
"from: http://twitter.com/#{r['from_user']}/status/#{r['id']}")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment