簡単でした.そうrubytterならね.
# -*- coding: utf-8 -*-
require 'logger'
require 'time'
require 'uri'
require 'net/http'
require 'rubytter'
LOG = Logger.new(STDOUT)
USER = '' # your twitter account
PASS = '' # your twitter password
LIST_NAME = '' # create twitter list name ex) last-post-was-quarter-year-ago
PAST_TIME = Time.now - 60 * 60 * 24 * 90 # quarter year
#oauth = Rubytter::OAuth.new(CONSUMER_KEY, CONSUMER_SECRET)
#access_token = OAuth::AccessToken.new(oauth.create_consumer, ACCESS_TOKEN, ACCESS_SECRET)
#@client = OAuthRubytter.new(access_token) # 2010/05/16現在 OAuth だと list 操作が 404 Not Found になる
@client = Rubytter.new(USER, PASS)
begin
@client.list(USER, LIST_NAME)
rescue Rubytter::APIError
@client.create_list(LIST_NAME) # リストが無ければ作成する
end
def all_friends
ary = []
next_cursor = -1
while next_cursor != 0
begin
res = @client.friends(USER, {cursor:next_cursor})
rescue => ex
LOG.warn ex
sleep 10
retry
end
ary.concat res.users
next_cursor = res.next_cursor
LOG.info "next_cursor is #{next_cursor}"
sleep 60
end
ary
end
all_friends.select { |friend|
if friend.statuses_count == 0
true
else
Time.parse(friend.status.created_at) < PAST_TIME
end
}.tap{ |e| LOG.info "対象は#{e.size}人くらいいます" }.each{ |friend|
name = friend.screen_name
LOG.info "#{name}処理中..."
begin
@client.add_member_to_list(LIST_NAME, name) if @client.user(name).statuses_count != 0 # 0ポストの人はリスト登録に失敗する
@client.leave(name)
sleep 60
rescue => ex
LOG.warn ex
sleep 10
retry
end
}
通常のTwitterAPIではcursorというのを指定して値を全取得します.rubytterでcursorを使うにはfriendsの2番目の引数にハッシュで指定してやればOKでした.
# -*- coding: utf-8 -*-
require 'rubytter'
require 'oauth'
USER = ''
CONSUMER_KEY = ''
CONSUMER_SECRET = ''
ACCESS_TOKEN = ''
ACCESS_TOKEN_SECRET = ''
oauth = Rubytter::OAuth.new(CONSUMER_KEY, CONSUMER_SECRET)
access_token = OAuth::AccessToken.new(oauth.create_consumer, ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
@client = OAuthRubytter.new(access_token)
def all_friends
ary = []
next_cursor = -1
while next_cursor != 0
res = @client.friends(USER, {cursor:next_cursor})
ary.concat res.users
next_cursor = res.next_cursor
p next_cursor
end
ary
end
p all_friends