Skip to content

Instantly share code, notes, and snippets.

@komasaru
Created December 19, 2012 08:42
Show Gist options
  • Save komasaru/4335340 to your computer and use it in GitHub Desktop.
Save komasaru/4335340 to your computer and use it in GitHub Desktop.
Ruby script to get members of lists by Twitter API.
# -*- coding: utf-8 -*-
require 'twitter'
require 'oauth'
require 'time'
# Twitter UserName
USER_NAME = "foo"
# Consumer key, Secret の設定
CONSUMER_KEY = "XXXXXXXXXXXXXXXXXXXX"
CONSUMER_SECRET = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
# Access Token Key, Secret の設定
ACCESS_TOKEN_KEY = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
ACCESS_SECRET = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
# メインクラス
class TwitterGetListMembers
def initialize
@ary_lists = [111111, 222222, 333333] # 調べたいリストのID
@count = 0 # 件数カウント用
end
# ユーザ取得
def get_members
begin
# Twitter.configure 設定
Twitter.configure do |config|
config.consumer_key = CONSUMER_KEY
config.consumer_secret = CONSUMER_SECRET
config.oauth_token = ACCESS_TOKEN_KEY
config.oauth_token_secret = ACCESS_SECRET
end
client = Twitter::Client.new
@ary_lists.each do |list_id|
res = client.list_members(:list_id => list_id)
res.each do |member|
user_id = member[:id]
puts "[#{list_id}] #{user_id}"
end
end
rescue => e
str_msg = "[EXCEPTION][" + self.class.name + ".get_members] " + e.to_s
STDERR.puts str_msg
exit 1
end
end
end
################
#### MAIN ####
################
begin
puts "STARTED!!"
puts "-" * 40
# 処理クラスインスタンス化
obj_main = TwitterGetListMembers.new
# 登録ユーザ取得
obj_main.get_members
puts "-" * 40
puts "FINISHED!"
rescue => e
str_msg = "[EXCEPTION]" + e.to_s
STDERR.puts str_msg
exit 1
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment