-
-
Save mreinsch/326274 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
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_src) | |
message_src_pattern = /#{Regexp.escape(message_src)}$/ | |
unless statuses.any?{|m| message_src_pattern =~ m} | |
status = "#{message}<br />Source: #{message_src}" | |
@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 | |
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
#!/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']}\"", | |
"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