Created
August 9, 2008 07:07
-
-
Save koduki/4658 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 "rubygems" | |
require "mechanize" | |
require "readline" | |
require "Kconv" | |
URL = 'http://www.twitter.com/' | |
KCODE = 'u' | |
class Twitter | |
def login name, password, proxy | |
@agent = WWW::Mechanize.new | |
if proxy != nil | |
uri = proxy.split(/:/)[0] | |
port = proxy.split(/:/)[1].to_i | |
@agent.set_proxy(uri, port) | |
end | |
page = @agent.get URL + 'login' | |
login_form = page.forms[1] | |
login_form["session[username_or_email]"] = name | |
login_form["session[password]"] = password | |
@agent.submit(login_form) | |
end | |
def change_icon pic | |
page = @agent.get URL + 'account/picture' | |
pic_form = page.forms[0] | |
pic_form.file_uploads[0].file_name = pic | |
@agent.submit(pic_form) rescue nil | |
end | |
def update msg | |
page = @agent.get URL + 'home' | |
update_form = page.forms[0] | |
update_form['status'] = msg.toutf8 | |
@agent.submit(update_form) rescue nil | |
end | |
end | |
class TwitShell | |
def initialize env | |
@env = env | |
end | |
def parse src | |
tokens = src.split(/\s/) | |
flg = true | |
xs = [] | |
stmt = [] | |
xs << stmt | |
tokens.each do |t| | |
if flg | |
if t == ";" | |
stmt = [] | |
xs << stmt | |
end | |
stmt << t | |
flg = t[0] != 34 | |
else | |
stmt[stmt.size - 1] += " #{ t }" | |
flg = t[t.size - 1] == 34 | |
end | |
end | |
xs | |
end | |
def run msg, loop | |
statements = parse(msg) | |
statements.each do |stmt| | |
cmd = stmt[0] | |
args = stmt[1..stmt.size - 1] | |
loop[@env, cmd, args] | |
end | |
end | |
def commandline init, loop | |
init[] | |
while (msg = Readline::readline("Twitter:> ")) | |
break if msg == "quit" || msg == "exit" | |
run(msg, loop) | |
end | |
end | |
def set_env args | |
cmd = args[0] | |
@env[cmd] = lambda{ |xargs| | |
stmt = args[1] | |
(0..xargs.size - 1).each do |i| stmt.gsub!(Regexp.new('\$'+i.to_s), xargs[i]) end if xargs.size > 0 | |
msg = stmt[1, stmt.size-2] | |
run(msg, lambda{ | env, cmd, args | | |
if env.has_key? cmd | |
env[cmd][args] | |
else | |
next if cmd == nil | |
puts cmd + " " + args.join(" ") | |
end | |
}) | |
} | |
"add #{ cmd } command." | |
end | |
end | |
t = Twitter.new | |
t.login ARGV[0], ARGV[1], ARGV[2] | |
icons = [] | |
shell = TwitShell.new({ | |
"update" => lambda { |args| | |
t.update args.join(" ") | |
"update '#{ args.join(' ') }'." | |
}, | |
"set_icon" => lambda { |args| | |
icons = args | |
"set #{ icons.join(", ") }" | |
}, | |
"next_icon" => lambda { |args| | |
cnt = 0 | |
swap = lambda{ | |
t.change_icon icons[cnt % icons.size] | |
cnt+=1 | |
} | |
swap[] | |
"change_icon '#{icons[cnt % icons.size]}'." | |
}, | |
"alias" => lambda{ |args| shell.set_env args } | |
}) | |
shell.commandline(lambda{ | |
puts "What are you doing?" | |
}, | |
lambda{ |env, cmd, args| | |
if env.has_key? cmd | |
puts env[cmd][args] | |
else | |
next if cmd == nil | |
puts cmd + " " + args.join(" ") | |
end | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment