Skip to content

Instantly share code, notes, and snippets.

@udzura
Created July 7, 2009 09:25
Show Gist options
  • Save udzura/141980 to your computer and use it in GitHub Desktop.
Save udzura/141980 to your computer and use it in GitHub Desktop.
Twitter Simple Client by Ruby, on IRB. The Manual is Just Source File.
#!/usr/bin/ruby
require 'irb'
require 'time'
require 'net/https'
require 'rubygems'
require 'twitter'
require 'rss'
require 'hpricot'
UNAME = "udzura"
PASSWD = Your::Passwd
DISP_FORMAT = "%-12.12s: %s"
$cli = Twitter::Client.new(
:login => UNAME,
:password => PASSWD
)
#def method_missing(name, *args)
# return [name.to_s, *args].join(" ")
#end
def post(str)
$cli.status(:post, str)
return str
end
def check_me(count=5)
$cli.timeline_for(:me, :count => count) do |s|
puts sprintf(DISP_FORMAT, s.user.screen_name, s.text)
end
return count
end
TWHOST = "twitter.com"
TWPORT = 80
TWPORT_SSL = 443
REP_URL = "/statuses/replies.json"
TWHEADER = {
'User-Agent' => 'Twitter4R',
'Accept' => 'text/json'
}
def replies(count=5)
@json
@i = 0
Net::HTTP.version_1_2
net_http = Net::HTTP.new(TWHOST, TWPORT_SSL)
net_http.use_ssl = true
net_http.verify_mode = OpenSSL::SSL::VERIFY_NONE
req = Net::HTTP::Get.new(REP_URL, TWHEADER)
req.basic_auth UNAME, PASSWD
net_http.start do |http|
@response = http.request(req)
@json = JSON.parse(@response.body)
end
@json.each do |item|
puts sprintf(DISP_FORMAT, item["user"]["screen_name"], item["text"])
@i += 1
break if @i >= count
end
return count
end
def timeline(count=10)
@i = 0
$cli.timeline_for(:friends, :count => count) do |s|
puts sprintf(DISP_FORMAT, s.user.screen_name, s.text)
@i += 1
break if @i >= count
end
return count
end
def timeline_for(uid, count=10)
@i = 0
$cli.timeline_for(:user, :id => uid, :count => count) do |s|
puts sprintf(DISP_FORMAT, s.user.screen_name, s.text)
@i += 1
break if @i >= count
end
return count
end
FAVOTTER_HOST = "favotter.matope.com"
FAV_FORMAT = " %2d fav by %s --"
DESC_CORE_RE = /^\w*\s(.*)\d+\sfav\sby\s*$/
class Favotter503Error < Net::ProtoServerError
end
def check_fav_for(uid, count=10)
@rss
@rss_url = "/userrss.php?user=#{uid}&mode=new"
@i = 0
Net::HTTP.version_1_2
Net::HTTP.start(FAVOTTER_HOST, TWPORT) do |http|
response = http.get(@rss_url)
raise Favotter503Error, "you can't access Favotter server. please try it later." unless response.code == "200"
@rss = RSS::Parser.parse(response.body, true)
end
@rss.channel.items.each do |item|
@user = item.author
@desc_raw = Hpricot.parse(item.description)
@text = @desc_raw.inner_text.gsub(DESC_CORE_RE, '\1')
puts sprintf(DISP_FORMAT, @user, @text)
@faved_by = []
@imgs = @desc_raw.search("img")
@cnt = @imgs.length - 1
@imgs[1..@cnt].each do |img|
@faved_by << img.attributes["title"]
end
puts sprintf(FAV_FORMAT, @cnt, @faved_by.join(", ")).rjust(96, "-")
@i += 1
break if @i >= count
end
return count
end
def check_fav(count=10)
check_fav_for(UNAME, count)
end
def boss # ボスが来た! 投げやり版
@os_commands = ["free", "vmstat -d", "df", "/sbin/ifconfig"]
@os_commands.each do |cmd|
puts `#{cmd}`
puts
end
gets
return nil
end
def b
boss
end
IRB.start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment