Skip to content

Instantly share code, notes, and snippets.

@zentooo
Created November 1, 2009 14:57
Show Gist options
  • Save zentooo/223547 to your computer and use it in GitHub Desktop.
Save zentooo/223547 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'optparse'
require 'net/http'
Net::HTTP.version_1_2
class TwitterList
attr_accessor :user_name, :password, :list_name
def initialize
opts = OptionParser.new
opts.on("-h", "--help", String) { show_help }
opts.on("-u VAL", "--username", String) { |val| @user_name = val }
opts.on("-p VAL", "--password", String) { |val| @password = val }
opts.on("-c VAL", "--creare", String) do |val|
create_list(val)
exit
end
opts.on("-d VAL", "--destroy", String) do |val|
destroy_list(val)
exit
end
opts.on("-s VAL", "--show", String) do |val|
show_list(val)
exit
end
opts.on("-l VAL", "--list", String) do |val|
@list_name = val
end
opts.on("-a VAL", "--add", Array) do |val|
add_user(val)
exit
end
opts.on("-r VAL", "--remove", Array) do |val|
remove_user(val)
exit
end
opts.parse!(ARGV)
end
def create_list(list_name)
do_http("http://api.twitter.com/1/#{@user_name}/lists.xml", "Post", "name=#{list_name}&mode=public")
end
def destroy_list(list_name)
do_http("http://api.twitter.com/1/#{@user_name}/lists/#{list_name}.xml", "Delete")
end
def show_all_lists
xml = do_http("http://api.twitter.com/1/#{@user_name}/lists.xml", "Get")
xml.scan(/<slug>([\w-]+)<\/slug>/) do |s|
puts s
end
end
def show_list(list_name)
xml = do_http("http://api.twitter.com/1/#{@user_name}/#{list_name}/members.xml", "Get")
xml.scan(/<screen_name>([\w]+)<\/screen_name>/) do |s|
puts s
end
end
def add_user(screen_names)
exit if @list_name == nil
screen_names.each do |screen_name|
id = get_id_from_screen_name(screen_name)
do_http("http://api.twitter.com/1/#{@user_name}/#{@list_name}/members.xml", "Post", "id=#{id}")
end
end
def remove_user(screen_names)
exit if @list_name == nil
screen_names.each do |screen_name|
id = get_id_from_screen_name(screen_name)
do_http("http://api.twitter.com/1/#{@user_name}/#{@list_name}/members.xml", "Delete", "id=#{id}")
end
end
def get_id_from_screen_name(screen_name)
xml = nil
Net::HTTP.start('twitter.com', 80) do |http|
xml = http.get("/users/show/#{screen_name}.xml").body
end
xml =~ /<id>(\d{1,8})<\/id>/
return $1
end
def do_http(url, method, data = nil)
exit if @user_name == nil or @password == nil
url.concat("?" + data) if data != nil
request_class_name = "Net::HTTP::" + method
req = eval(request_class_name).new(url)
req.basic_auth(@user_name, @password)
Net::HTTP.start('api.twitter.com', 80) do |http|
http.request(req)
end
end
def show_help
puts <<EOS
usage:
-h : show this help
-u username -p password : show all lists you have made
-u username -p password -s listname : show all members in list 'listname'
-u username -p password -c listname : create a list 'listname'
-u username -p password -d listname : destroy the list 'listname'
-u username -p password -l listname -a foo,bar,foobar : add [foo,bar,foobar] to the list 'listname'
-u username -p password -l listname -r foo,bar,foobar : remove [foo,bar,foobar] to the list 'listname'
!!ATTENTION!!
For last two options (-a and -r), do not insert spaces before and after of ','.
It does't work! (e.g. foo, bar, foobar)
EOS
end
end
twitterlist = TwitterList.new
if twitterlist.user_name != nil and twitterlist.password != nil
twitterlist.show_all_lists
else
twitterlist.show_help
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment