Created
April 30, 2010 07:38
-
-
Save aquarla/384891 to your computer and use it in GitHub Desktop.
This file contains 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/ruby | |
# 自分のtweetがふぁぼられたら、即座にコンソールに表示 (with ChirpUserStreams) | |
# by あくあーら ( http://twitter.com/aquarla ) | |
# 参考にしたサイト : | |
# http://d.hatena.ne.jp/shibason/20090816/1250405491 | |
# http://apiwiki.twitter.com/ChirpUserStreams | |
# http://ssig33.com/blog/2010-04-25-1.html | |
require 'net/http' | |
require 'uri' | |
require 'rubygems' | |
require 'json' | |
USERNAME = 'xxxxxxxx' # ユーザ名を入れる | |
PASSWORD = 'xxxxxxxx' # パスワードを入れる | |
class Twitter | |
def json_api(api_url) | |
uri = URI.parse(api_url) | |
Net::HTTP.start(uri.host, uri.port) do |http| | |
request = Net::HTTP::Get.new(uri.request_uri) | |
request.basic_auth(USERNAME, PASSWORD) | |
http.request(request) do |response| | |
return JSON.parse(response.body) | |
end | |
end | |
{} | |
end | |
def user_id_of(user_name) | |
user = json_api("http://api.twitter.com/users/#{user_name}.json") | |
user['id'] || "0" | |
end | |
def user_screen_name_of(id) | |
user = json_api("http://api.twitter.com/users/#{id}.json") | |
user['screen_name'] || "unknown" | |
end | |
def status_of(id) | |
json_api("http://api.twitter.com/statuses/show/#{id}.json") | |
end | |
end | |
twitter = Twitter.new | |
my_id = twitter.user_id_of(USERNAME) | |
uri = URI.parse('http://chirpstream.twitter.com/2b/user.json') | |
Net::HTTP.start(uri.host, uri.port) do |http| | |
request = Net::HTTP::Get.new(uri.request_uri) | |
request.basic_auth(USERNAME, PASSWORD) | |
http.request(request) do |response| | |
raise 'Response is not chuncked' unless response.chunked? | |
response.read_body do |chunk| | |
status = JSON.parse(chunk) rescue next | |
if status['event'] == 'favorite' && status['target']['id'] == my_id | |
target_status = twitter.status_of(status['target_object']['id']) | |
puts "#{twitter.user_screen_name_of(status['source']['id'])} さんが、あなたの発言(#{target_status['text']})をふぁぼりました><" | |
end | |
end | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment