Created
April 22, 2012 05:59
-
-
Save nobuoka/2459813 to your computer and use it in GitHub Desktop.
Twitter のリストにユーザーを放り込む Ruby スクリプト
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
# coding: UTF-8 | |
#============================== | |
# 指定した自分のリストに指定したユーザーを放り込みます. | |
# 多量のユーザーをリストに放り込みたいときに便利. | |
#============================== | |
# 本プログラムは OAuthSimple ライブラリを使用します. | |
# https://github.com/nobuoka/ruby-OAuthSimple | |
# で公開しているので, git clone するなりして使用してください. | |
# (まだ開発中なんで適当にソースを読んで使ってください) | |
$LOAD_PATH.unshift File.join( File.dirname(__FILE__), 'modules', 'OAuthSimple', 'lib' ) | |
require 'oauth_simple' | |
# OAuth の client credentials と user credentials | |
client_credentials = [ | |
'YOUR_CONSUMER_KEY', | |
'YOUR_CONSUMER_SECRET', | |
] | |
user_credentials = [ | |
'YOUR_ACCESS_TOKEN', | |
'YOUR_ACCESS_TOKEN_SECRET', | |
] | |
# 追加先のリストの id | |
list_id = '34234' | |
# 追加するユーザーの screen_name 一覧 | |
users_str = <<'END_OF_USERS'.split( /\n/ ).join( ',' ) | |
screen_name_1 | |
screen_name_2 | |
END_OF_USERS | |
# OAuth 機能をもつ Net::HTTP のサブクラス (OAuthSimple::HTTP) | |
# をサブクラス化して, デフォルトの OAuth パラメータを持たせる | |
http_class = OAuthSimple::HTTP.create_subclass_with_default_oauth_params() | |
http_class.set_default_oauth_client_credentials( *client_credentials ) | |
http_class.set_default_oauth_user_credentials( *user_credentials ) | |
http_class.set_default_oauth_signature_method( 'HMAC-SHA1' ) | |
# 接続先情報を与えてインスタンス化 | |
host = 'api.twitter.com' | |
http = http_class.new( host, 443 ) | |
http.use_ssl = true | |
http.verify_mode = OpenSSL::SSL::VERIFY_PEER | |
http.verify_depth = 5 | |
# 接続してリクエストを投げる | |
http.start do |http| | |
path = '/1/lists/members/create_all.json' | |
params = OAuthSimple::RequestParamList.new [ | |
[ 'list_id' , list_id ], | |
[ 'screen_name', users_str ], | |
] | |
http.request_post( path, params.to_query_string ) do |res| | |
p res # 200 番なら成功だが, 200 以外が返ってきてもちゃんと追加できていたこともあった | |
p res.body | |
end | |
end |
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
# coding: UTF-8 | |
#============================== | |
# 自分のリストの情報を取得します. | |
# リストの id 確認等に. | |
#============================== | |
$LOAD_PATH.unshift File.join( File.dirname(__FILE__), 'modules', 'OAuthSimple', 'lib' ) | |
require 'json' | |
require 'oauth_simple' | |
client_credentials = [ | |
'YOUR_CONSUMER_KEY', | |
'YOUR_CONSUMER_SECRET', | |
] | |
user_credentials = [ | |
'YOUR_ACCESS_TOKEN', | |
'YOUR_ACCESS_TOKEN_SECRET', | |
] | |
http_class = OAuthSimple::HTTP.create_subclass_with_default_oauth_params() | |
http_class.set_default_oauth_client_credentials( *client_credentials ) | |
http_class.set_default_oauth_user_credentials( *user_credentials ) | |
http_class.set_default_oauth_signature_method( 'HMAC-SHA1' ) | |
host = 'api.twitter.com' | |
http = http_class.new( host, 443 ) | |
http.use_ssl = true | |
http.verify_mode = OpenSSL::SSL::VERIFY_PEER | |
http.verify_depth = 5 | |
http.start do |http| | |
path = '/1/lists/all.json' | |
http.request_get( path ) do |res| | |
if res.code == '200' | |
lists = JSON.parse( res.body ) | |
lists.each do |list| | |
puts "id: #{list['id_str']}, slug: #{list['slug']}, name: #{list['name']}" | |
end | |
else | |
# なんらかのエラー | |
p res | |
p res.body | |
exit 1 | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment