43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
|
# File 'lib/tw/app/main.rb', line 43
def run(argv)
@args = ArgsParser.parse argv, :style => :equal do
arg :user, 'user account', :alias => :u
arg 'user:add', 'add user'
arg 'user:list', 'show user list'
arg 'user:default', 'set default user'
arg :timeline, 'show timeline', :alias => :tl
arg :dm, 'show direct messages'
arg 'dm:to', 'create direct message'
arg :favorite, 'favorite tweet', :alias => :fav
arg :retweet, 'retweet', :alias => :rt
arg :delete, 'delete tweet', :alias => :del
arg :search, 'search public timeline', :alias => :s
arg :stream, 'show user stream', :alias => :st
arg :status_id, 'show status_id', :alias => :id
arg :file, 'upload file'
arg :pipe, 'pipe tweet'
arg :format, 'output format', :default => 'text'
arg :silent, 'silent mode'
arg :yes, 'do not show dialogue'
arg :conf, 'config file', :default => Tw::Conf.conf_file
arg :version, 'show version', :alias => :v
arg :help, 'show help', :alias => :h
validate :user, 'invalid user name' do |v|
v =~ /^[a-zA-Z0-9_]+$/
end
validate 'user:default', 'invalid user name' do |v|
v =~ /^[a-zA-Z0-9_]+$/
end
validate 'dm:to', 'invalid user name' do |v|
v =~ /^[a-zA-Z0-9_]+$/
end
validate :file, "file does not exists" do |v|
File.exists? v
end
end
if @args.has_option? :help
STDERR.puts "Tw - Twitter client on Ruby v#{Tw::VERSION}"
STDERR.puts " http://shokai.github.io/tw"
STDERR.puts
STDERR.puts @args.help
STDERR.puts
STDERR.puts "e.g."
STDERR.puts "tweet tw hello world"
STDERR.puts " echo 'hello' | tw --pipe"
STDERR.puts " tw 'yummy!!' --file=food.jpg --yes"
STDERR.puts "read tw @username"
STDERR.puts " tw @username @user2 @user2/listname"
STDERR.puts " tw --search=ruby"
STDERR.puts " tw --stream"
STDERR.puts " tw --stream:filter=ruby,java"
STDERR.puts " tw --dm:to=username \"hello!\""
STDERR.puts "id tw @shokai --id"
STDERR.puts " tw --id=334749349588377601"
STDERR.puts 'reply tw "@shokai wow!!" --id=334749349588377601'
STDERR.puts " tw --fav=334749349588377601"
STDERR.puts " tw --rt=334749349588377601"
STDERR.puts " tw --format=json"
STDERR.puts ' tw --format="@#{user} #{text} - http://twitter.com/#{user}/#{id}"'
STDERR.puts "delete tw --del=334749349588377601"
on_exit
end
Render.silent = (@args.has_option? :silent or @args.has_param? :format)
Render.show_status_id = @args[:status_id]
Tw::Conf.conf_file = @args[:conf]
regist_cmds
cmds.each do |name, cmd|
next unless @args[name]
cmd.call @args[name], @args
end
auth
if @args.argv.size < 1
if @args.has_param? :status_id
client.show_status @args[:status_id]
else
Render.display client.mentions, @args[:format]
end
elsif all_requests?(@args.argv)
Render.display Parallel.map(@args.argv, :in_threads => @args.argv.size){|arg|
if user = username?(arg)
res = client.user_timeline user
elsif (user, list = listname?(arg)) != false
res = client.list_timeline(user, list)
end
res
}, @args[:format]
else
message = @args.argv.join(' ')
= {}
if (len = message.char_length_with_t_co) > 280
STDERR.puts "tweet too long (#{len} chars)"
on_error
else
if @args.has_param? :status_id
client.show_status @args[:status_id]
puts "--"
puts "reply \"#{message}\"? (#{len} chars)"
[:in_reply_to_status_id] = @args[:status_id]
else
puts "tweet \"#{message}\"? (#{len} chars)"
if @args.has_param? :file
puts "upload \"#{@args[:file]}\"? (#{File.size @args[:file]} bytes)"
end
end
unless @args.has_option? :yes
puts '[Y/n]'
on_exit if STDIN.gets.strip =~ /^n/i
end
end
begin
if @args.has_param? :file
client. message, File.open(@args[:file]),
else
client. message,
end
rescue => e
STDERR.puts e.message
end
end
end
|