-
Notifications
You must be signed in to change notification settings - Fork 1
/
mikutter-rss-tl.rb
393 lines (299 loc) · 9.77 KB
/
mikutter-rss-tl.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
# -*- coding: utf-8 -*-
require 'rubygems'
require 'feed-normalizer'
require 'date'
require 'open-uri'
# IDからシンボルを作る
def sym(base, id)
(base + id.to_s).to_sym
end
# 検索クラス
class Satoshi
attr_reader :last_fetch_time
ICON_COLORS = {
:red => ["レッド", File.dirname(__FILE__) + "/red-icon-128.png"],
:blue => ["ブルー", File.dirname(__FILE__) + "/blue-icon-128.png"],
:black => ["ブラック", File.dirname(__FILE__) + "/black-icon-128.png"],
:bronze => ["ブロンズ", File.dirname(__FILE__) + "/bronze-icon-128.png"],
:green => ["グリーン", File.dirname(__FILE__) + "/green-icon-128.png"],
:lightblue => ["ライトブルー", File.dirname(__FILE__) + "/lightblue-icon-128.png"],
:mistred => ["ミストレッド", File.dirname(__FILE__) + "/mistred-icon-128.png"],
:purple => ["パープル", File.dirname(__FILE__) + "/purple-icon-128.png"],
:mikutter => ["みくったーちゃん", MUI::Skin.get("icon.png")],
}
# コンストラクタ
def initialize(service, user_config, id)
@service = service
@last_config = Hash.new
@result_queue = Array.new()
@queue_lock = Mutex.new()
@last_result_time = nil
@last_fetch_time = Time.now
@user_config = user_config
@id = id
end
# インスタンス毎のコンフィグを読み込む
def [](key)
@user_config[sym(key.to_s, @id)]
end
# コンフィグの初期化
def init_user_config()
@user_config[sym("rss_url", @id)] ||= ""
@user_config[sym("rss_reverse", @id)] ||= false
@user_config[sym("rss_custom_style", @id)] ||= false
@user_config[sym("rss_font_face", @id)] ||= 'Sans 10'
@user_config[sym("rss_font_color", @id)] ||= [0, 0, 0]
@user_config[sym("rss_background_color", @id)] ||= [65535, 65535, 65535]
@user_config[sym("rss_loop", @id)] ||= false
@user_config[sym("rss_icon", @id)] ||= :red
end
# 設定画面の生成
def setting(plugin, prefix)
id = @id
plugin.settings prefix + "フィード" + id.to_s do
input("URL", sym("rss_url", id))
boolean("新しい記事を優先する", sym("rss_reverse", id))
boolean("ループさせる", sym("rss_loop", id))
select("アイコンの色", sym("rss_icon", id), ICON_COLORS.inject({}){ |result, kv|
result[kv[0]] = kv[1][0]
result
})
settings "カスタムスタイル" do
boolean("カスタムスタイルを使う", sym("rss_custom_style", id))
fontcolor("フォント", sym("rss_font_face", id), sym("rss_font_color", id))
color("背景色", sym("rss_background_color", id))
end
end
end
# 検索結果を取り出す
def fetch()
msg = nil
@queue_lock.synchronize {
if @user_config[sym("rss_reverse", @id)] then
msg = @result_queue.pop
else
msg = @result_queue.shift
end
}
if msg != nil then
@last_fetch_time = Time.now
end
# puts @urls.to_s + @result_queue.size.to_s
return msg
end
# メッセージ保有してる?
def empty?()
@result_queue.empty?
end
# 検索する
def search()
begin
url = @user_config[sym("rss_url", @id)]
# 検索オプションが変わったら、キャッシュを破棄する
is_reload = [sym("rss_url", @id)]
.inject(false) { |result, key|
result = result || (@user_config[key] != @last_config[key])
@last_config[key] = @user_config[key]
result
}
if is_reload then
p "ID:" + @id.to_s + " setting changed"
@queue_lock.synchronize {
@result_queue.clear
@last_result_time = nil
}
end
# ループさせる
if @user_config[sym("rss_loop", @id)] && empty? then
@last_result_time = nil
end
if @user_config[sym("rss_url", @id)].empty? then
return true
end
# RSSを読み込む
feed = FeedNormalizer::FeedNormalizer.parse(open(@user_config[sym("rss_url", @id)]))
drop_time = Time.now.to_i - (24 * 60 * 60 * @user_config["rss_drop"].to_i)
entries = feed.entries.select { |entry|
result_tmp = false
if entry.last_updated != nil && entry.last_updated.to_i < drop_time then
next
elsif @last_result_time == nil then
result_tmp = true
elsif entry.last_updated != nil && @last_result_time < entry.last_updated then
result_tmp = true
else
result_tmp = false
end
result_tmp
}
if entries.size == 0 then
return true
end
msgs = entries.map { |entry|
# どうせタイムライン表示時に自動展開されちゃうので短縮はしない
# links = MessageConverters.shrink_url([item.link.to_s])
msg = Message.new(:message => ("【" + feed.title.force_encoding("utf-8") + "】\n" + entry.title.force_encoding("utf-8") + "\n\n[記事を読む]"), :system => true)
msg[:rss_feed_url] = entry.url.force_encoding("utf-8")
msg[:created] = entry.last_updated
if feed.image.empty? then
image_url = ICON_COLORS[@user_config[sym("rss_icon", @id)]][1]
else
image_url = feed.image
end
title = feed.title.force_encoding("utf-8")
msg[:user] = User.new(:id => -3939,
:idname => "RSS",
:name => title,
:profile_image_url => image_url)
if entry.last_updated != nil && (@last_result_time == nil || @last_result_time < entry.last_updated) then
@last_result_time = entry.last_updated
end
msg
}
p "new message:" + msgs.size.to_s
p "last time:" + $last_time.to_s
@queue_lock.synchronize {
@result_queue.concat(msgs.reverse)
}
rescue => e
puts e
puts e.backtrace
return false
end
return true
end
end
Plugin.create :rss do
# グローバル変数の初期化
$satoshis = []
# カスタムスタイルを選択する
def choice_style(message, key, default)
if message[:rss] != nil && message[:rss][:rss_custom_style] then
message[:rss][key]
else
default
end
end
# 更新用ループ
def search_loop(service)
search_url(service)
Reserver.new(UserConfig[:rss_period]){
search_loop(service)
}
end
# 混ぜ込みループ
def insert_loop(service)
begin
# 混ぜ込むべきインスタンスを取得
target_satoshi = $satoshis.select { |a| a != nil }
.sort { |a, b| a.last_fetch_time <=> b.last_fetch_time }
.find { |satoshi| !satoshi.empty? }
if target_satoshi != nil then
msg = target_satoshi.fetch()
msg[:modified] = Time.now
msg[:rss] = target_satoshi
# タイムラインに登録
if defined?(timeline)
timeline(:home_timeline) << [msg]
else
Plugin.call(:update, service, [msg])
end
# puts "last message :" + $result_queue.size.to_s
end
Reserver.new(UserConfig[:rss_insert_period]){
insert_loop(service)
}
rescue => e
puts e
puts e.backtrace
end
end
# 検索
def search_url(service)
begin
$satoshis.each { |satoshi|
result = satoshi.search()
if !result then
msg = Message.new(:message => "フィードの取得に失敗しました。 " + satoshi[:rss_url], :system => true)
msg[:user] = User.new(:id => -3939,
:idname => "RSS",
:name => "エラー",
:profile_image_url => MUI::Skin.get("icon.png"))
# タイムラインに登録
if defined?(timeline)
timeline(:home_timeline) << [msg]
else
Plugin.call(:update, service, [msg])
end
end
}
rescue => e
puts e
puts e.backtrace
end
end
# 起動時処理
on_boot do |service|
(0..5 - 1).each {|i|
$satoshis << Satoshi.new(service, UserConfig, i + 1)
}
# コンフィグの初期化
UserConfig[:rss_period] ||= 60
UserConfig[:rss_insert_period] ||= 3
UserConfig[:rss_drop] ||= 7
$satoshis.each {|satoshi|
satoshi.init_user_config
}
# 設定画面
settings "RSS混ぜ込み" do
adjustment("ポーリング間隔(秒)", :rss_period, 1, 6000)
adjustment("混ぜ込み間隔(秒)", :rss_insert_period, 1, 600)
adjustment("一定期間より前のフィードは流さない(日)", :rss_drop, 1, 365)
end
$satoshis.each {|satoshi|
if satoshi.equal?($satoshis[-1]) then
satoshi.setting(self, "┗")
else
satoshi.setting(self, "┣")
end
}
search_loop(service)
insert_loop(service)
end
# 背景色決定
filter_message_background_color do |message, color|
begin
color = choice_style(message.message, :rss_background_color, color)
[message, color]
rescue => e
puts e
puts e.backtrace
end
end
# フォント色決定
filter_message_font_color do |message, color|
begin
color = choice_style(message.message, :rss_font_color, color)
[message, color]
rescue => e
puts e
puts e.backtrace
end
end
# フォント決定
filter_message_font do |message, font|
begin
font = choice_style(message.message, :rss_font_face, font)
[message, font]
rescue => e
puts e
puts e.backtrace
end
end
# リンクの処理
Message::Entity.addlinkrule(:rss, /\[記事を読む\]/){ |segment|
if segment[:message][:rss_feed_url] != nil then
Gtk::TimeLine.openurl(segment[:message][:rss_feed_url])
end
}
end