This repository has been archived by the owner on Aug 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.rb
194 lines (156 loc) · 3.6 KB
/
plugin.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
require 'pluggable_lite'
require 'cgi'
module Hibarichan
class Plugin
extend PluggableLite::Plugin
def initialize(rest, markov, repository)
@rest = rest
@markov = markov
@repository = repository
end
def on_retweet(_tweet)
on_any_event
end
def on_reply(_tweet)
on_any_event
end
def on_tweet(_tweet)
on_any_event
end
def on_dmessage(_dmessage)
on_any_event
end
def on_delete(_tweet)
on_any_event
end
def on_event(_event)
on_any_event
end
def on_friendlist(_friendlist)
on_any_event
end
def on_stallwarning(_stallwarning)
on_any_event
end
def on_any_event
end
private
def update(*args)
if $debug
puts(*args)
else
@rest.update(*args)
end
rescue => e
p e
end
def learn(str)
@markov.learn(str)
end
def repo(name = self.class.name.split('::')[-1])
@repository[name]
end
def get_sentence(length = 140)
# 文字列生成
@markov.get_sentence(length)
rescue => e
# 例外が発生したらそのまま投げる
raise e
end
def unescape_html(text)
CGI.unescapeHTML(text)
end
def get_stripped_text(tweet)
text = tweet.text.dup
# メンションを削除
if tweet.user_mentions?
tweet.user_mentions.each do |mnt|
text.gsub!('@' + mnt.attrs[:screen_name], '')
end
end
# ハッシュタグを削除
if tweet.hashtags?
tweet.hashtags.each do |htag|
text.gsub!('#' + htag.attrs[:text], '')
end
end
# URLを削除
if tweet.uris?
tweet.uris.each do |uri|
text.gsub!(uri.attrs[:url], '')
end
end
# メディアを削除
if tweet.media?
tweet.media.each do |med|
text.gsub!(med.attrs[:url], '')
end
end
# シンボルを削除
if tweet.symbols?
tweet.symbols.each do |sym|
text.gsub!('$' + sym.attrs[:text], '')
end
end
# 連続する空白を集約
text.gsub!(/ +/, ' ')
# 先頭・末尾の空白の除去
text.strip!
text
end
end
class PluginManager
extend PluggableLite::PluginManager
# Pluginクラスをベースクラスとして登録
register Plugin
def initialize(rest, markov, repository)
# ディレクトリ登録
PluginManager.load('plugins')
# プラグインのインスタンス作成
@plugins = []
PluginManager.plugins.each do |plugin_class|
@plugins << plugin_class.new(rest, markov, repository)
end
end
def on_tweet(tweet)
@plugins.each do |plugin|
plugin.on_tweet(tweet)
end
end
def on_reply(tweet)
@plugins.each do |plugin|
plugin.on_reply(tweet)
end
end
def on_retweet(tweet)
@plugins.each do |plugin|
plugin.on_retweet(tweet)
end
end
def on_dmessage(dmessage)
@plugins.each do |plugin|
plugin.on_dmessage(dmessage)
end
end
def on_delete(deletedtweet)
@plugins.each do |plugin|
plugin.on_delete(deletedtweet)
end
end
def on_event(event)
@plugins.each do |plugin|
plugin.on_event(event)
end
end
def on_friendlist(friendlist)
@plugins.each do |plugin|
plugin.on_friendlist(friendlist)
end
end
def on_stallwarning(stallwarning)
@plugins.each do |plugin|
plugin.on_stallwarning(stallwarning)
end
end
end
end