-
Notifications
You must be signed in to change notification settings - Fork 13
/
twitter_reudy.rb
122 lines (99 loc) · 2.95 KB
/
twitter_reudy.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#encoding: utf-8
#Copyright (C) 2011 Glass_saga <[email protected]>
$REUDY_DIR= "./lib/reudy" unless defined?($REUDY_DIR)
Interval = 60 # タイムラインを取得する間隔
Abort_on_API_limit = false # API制限に引っかかった時にabortするかどうか
trap(:INT){ exit }
require 'optparse'
require 'rubytter'
require 'highline'
require 'time'
require $REUDY_DIR+'/bot_irc_client'
require $REUDY_DIR+'/reudy'
require $REUDY_DIR+'/reudy_common'
module Gimite
class TwitterClient
include(Gimite)
def initialize(user)
@user = user
@user.client = self
@last_tweet = Time.now
key = user.settings[:twitter][:key]
secret = user.settings[:twitter][:secret]
cons = OAuth::Consumer.new(key, secret, :site => "http://api.twitter.com")
unless File.exist?(File.dirname(__FILE__)+"/token")
request_token = cons.get_request_token
puts "Access This URL and press 'Allow' => #{request_token.authorize_url}"
pin = HighLine.new.ask('Input key shown by twitter: ')
access_token = request_token.get_access_token(:oauth_verifier => pin)
open(File.dirname(__FILE__)+"/token","w") do |f|
f.puts access_token.token
f.puts access_token.secret
end
end
keys = File.read(File.dirname(__FILE__)+"/token").split(/\r?\n/).map(&:chomp)
token = OAuth::AccessToken.new(cons, keys[0], keys[1])
@r = OAuthRubytter.new(token)
end
attr_accessor :r
def onTweet(status)
@user.onOtherSpeak(status.user.screen_name, status.text)
end
#補助情報を出力
def outputInfo(s)
puts "(#{s})"
end
#発言する
def speak(s)
time = Time.now
if time - @last_tweet > Interval
@r.update(s)
puts "tweeted: #{s}"
@last_tweet = time
end
end
end
opt = OptionParser.new
directory = 'public'
opt.on('-d DIRECTORY') do |v|
directory = v
end
db = 'pstore'
opt.on('--db DB_TYPE') do |v|
db = v
end
mecab = nil
opt.on('-m','--mecab') do |v|
mecab = true
end
opt.parse!(ARGV)
#twitter用ロイディを作成
client = TwitterClient.new(Reudy.new(directory,{},db,mecab))
loop do
begin
since_id = -1
client.r.friends_timeline(since_id: since_id).each do |status|
puts "#{status.user.screen_name}: #{status.text}"
since_id = status.id
client.onTweet(status)
end
sleep(Interval)
rescue => ex
case ex.message
when "Could not authenticate with OAuth."
abort ex.message
when /Rate limit exceeded./
if Abort_on_API_limit
abort ex.message
else
reset_time = Time.parse(r.limit_status[:reset_time])
puts ex.message
puts "API制限は#{reset_time}に解除されます。"
sleep(reset_time - Time.now)
end
else
puts ex.message
end
end
end
end