Skip to content

Instantly share code, notes, and snippets.

@komasaru
Created December 19, 2012 08:01
Show Gist options
  • Save komasaru/4335177 to your computer and use it in GitHub Desktop.
Save komasaru/4335177 to your computer and use it in GitHub Desktop.
Ruby script to get twitter 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 TwitterGetLists
def initialize
@count = 0 # 件数カウント用
end
# リスト取得
def get_list
begin
# OAuth 設定
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
# JSON データ取得
client = Twitter::Client.new
res = client.lists(:screen_name => USER_NAME)
res.each do |list|
list_id = list[:id]
list_name = list[:name]
mode = list[:mode]
created_at = Time.parse(list[:created_at].to_s).strftime("%Y-%m-%d %H:%M:%S")
member_count = list[:member_count]
puts "[#{list_id}] #{list_name}, #{mode}, #{created_at}, #{member_count}"
@count += 1
end
# 件数
puts "COUNT : #{@count}"
rescue => e
str_msg = "[EXCEPTION][" + self.class.name + ".get_list] " + e.to_s
STDERR.puts str_msg
exit 1
end
end
end
################
#### MAIN ####
################
begin
puts "STARTED!!"
puts "-" * 40
# 処理クラスインスタンス化
obj_main = TwitterGetLists.new
# リスト取得
obj_main.get_list
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