Class: Command::Update
- Inherits:
-
CommandBase
show all
- Extended by:
- Memoist
- Includes:
- Narou::Eventable
- Defined in:
- lib/command/update.rb,
lib/command/update/interval.rb,
lib/command/update/general_lastup_updater.rb
Defined Under Namespace
Classes: GeneralLastupUpdater, Interval
Constant Summary
collapse
- HOTENTRY_DIR_NAME =
"hotentry"
- HOTENTRY_TEMPLATE_NAME =
"hotentry.txt"
- HOTENTRY_TITLE_PATTERN =
"hotentry %y/%m/%d %H:%M"
- HOTENTRY_FILE_PATTERN =
"hotentry_%y-%m-%d_%H%M.txt"
Narou::Eventable::EVENTS_CONTAINER
Instance Attribute Summary
Attributes inherited from CommandBase
#stream_io
Class Method Summary
collapse
Instance Method Summary
collapse
#add_event_listener, included, #one, #remove_event_listener, #trigger_event
Methods inherited from CommandBase
#disable_logging, #display_help!, execute!, #execute!, #force_change_settings_function, help, #hook_call, #load_local_settings, #tagname_to_ids
Constructor Details
#initialize ⇒ Update
Returns a new instance of Update.
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
|
# File 'lib/command/update.rb', line 32
def initialize
super("[<target> ...] [options]")
@opt.separator <<-EOS
・管理対象の小説を更新します。
更新したい小説のNコード、URL、タイトル、IDもしくは別名を指定して下さい。
IDは #{@opt.program_name} list を参照して下さい。
・対象を指定しなかった場合、すべての小説の更新をチェックします。
・一度に複数の小説を指定する場合は空白で区切って下さい。
・全て更新する場合、convert.no-openが設定されていなくても保存フォルダは開きません。
Examples:
narou update # 全て更新
narou u # 短縮コマンド
narou update 0 1 2 4
narou update n9669bk 異世界迷宮で奴隷ハーレムを
narou update http://ncode.syosetu.com/n9669bk/
# foo タグが付いた小説と bar タグが付いた小説を更新(タグのOR指定)
narou u foo bar
# foo タグ及び bar タグが両方付いた小説のみ更新(タグのAND指定)
narou tag foo bar | narou u
narou l -t "foo bar" | narou u # こっちでも同じ(覚えやすい方を使う)
Options:
EOS
@opt.on("-n", "--no-convert", "変換をせずアップデートのみ実行する") {
@options["no-convert"] = true
}
@opt.on("-a", "--convert-only-new-arrival", "新着がある場合のみ変換を実行する") {
@options["convert-only-new-arrival"] = true
}
@opt.on("--gl [OPT]", <<-EOS) { |option|
データベースに最新話掲載日を反映させる
| OPT | 概要
| 指定なし | 全ての小説を対象にする
| narou | なろうAPIを使える小説のみ対象
| other | なろうAPIが使えない小説のみ対象
EOS
if option && !["narou", "other"].include?(option)
error "--gl で指定可能なオプションではありません。詳細は narou u -h を参照"
exit Narou::EXIT_ERROR_CODE
end
update_general_lastup(option)
exit 0
}
@opt.on("-f", "--force", "凍結済みも更新する") {
@options["force"] = true
}
@opt.on("-s", "--sort-by KEY", "アップデートする順番を変更する\n#{Narou.update_sort_key_summaries}") { |key|
@options["sort-by"] = key
}
@opt.on("-i", "--ignore-all", "<target>を省略した場合の全件更新処理を無効化する") {
@options["ignore-all"] = true
}
end
|
Class Method Details
.get_newest_hotentry_file_path(device) ⇒ Object
360
361
362
363
|
# File 'lib/command/update.rb', line 360
def self.get_newest_hotentry_file_path(device)
pattern = File.join(Update.hotentry_dirname, "hotentry_*#{device.ebook_file_ext}")
Dir.glob(pattern).sort.last
end
|
.hotentry_dirname ⇒ Object
.oneline_help ⇒ Object
28
29
30
|
# File 'lib/command/update.rb', line 28
def self.oneline_help
"小説を更新します"
end
|
Instance Method Details
#convert_hotentry(hotentry, cmd_convert) ⇒ Object
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
|
# File 'lib/command/update.rb', line 264
def convert_hotentry(hotentry, cmd_convert)
output_filename = nil
display_inspector = false
ignore_force = false
ignore_default = false
converted_text_array = []
use_dakuten_font = false
Helper.print_horizontal_rule
puts "hotentry の変換を開始"
subtitles_size = hotentry.inject(0) { |sum, (_, subtitles)| subtitles.size + sum }
progressbar = ProgressBar.new(subtitles_size)
total_progress = 0
begin
hotentry.each do |id, subtitles|
setting = NovelSetting.load(id, ignore_force, ignore_default)
setting.enable_illust = false novel_converter = NovelConverter.new(
setting, output_filename,
display_inspector, Update.hotentry_dirname,
stream_io: stream_io
)
last_num = 0
novel_converter.on(:"convert_main.loop") do |i|
progressbar.output(total_progress + i)
last_num = i
end
converted_text_array << {
setting: setting,
text: novel_converter.convert_main_for_novel(subtitles, true)
}
use_dakuten_font |= novel_converter.use_dakuten_font
total_progress += last_num + 1
end
ensure
progressbar.clear
end
puts "縦書用の変換が終了しました"
device = Narou.get_device
now = Time.now
hotentry_title = now.strftime(HOTENTRY_TITLE_PATTERN)
hotentry_text = Template.get(HOTENTRY_TEMPLATE_NAME, binding, 1.0)
txt_output_path = File.join(Update.hotentry_dirname, now.strftime(HOTENTRY_FILE_PATTERN))
create_inclusive_directory(txt_output_path)
File.write(txt_output_path, hotentry_text)
relay_proc = -> {
NovelConverter.convert_txt_to_ebook_file(
txt_output_path,
use_dakuten_font: use_dakuten_font,
device: device,
stream_io: stream_io
)
}
if device
cmd_convert.extend(device.get_hook_module)
cmd_convert.converted_txt_path = txt_output_path
cmd_convert.hook_call(:change_settings)
end
if cmd_convert.respond_to?(:hook_convert_txt_to_ebook_file)
ebook_path = cmd_convert.hook_convert_txt_to_ebook_file(&relay_proc)
else
ebook_path = relay_proc.call
end
ebook_path
end
|
#copy_hotentry(ebook_path, cmd_convert) ⇒ Object
342
343
344
|
# File 'lib/command/update.rb', line 342
def copy_hotentry(ebook_path, cmd_convert)
cmd_convert.copy_to_converted_file(ebook_path, io: stream_io)
end
|
#create_inclusive_directory(path) ⇒ Object
338
339
340
|
# File 'lib/command/update.rb', line 338
def create_inclusive_directory(path)
FileUtils.mkdir_p(File.dirname(path))
end
|
#execute(argv) ⇒ Object
rubocop:disable Metrics/BlockLength
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
|
# File 'lib/command/update.rb', line 139
def execute(argv)
super
mistook_count = 0
update_target_list = argv.dup
@options["no-open"] = false
if update_target_list.empty?
exit 0 if @options["ignore-all"]
update_target_list += Database.instance.get_object.keys
@options["no-open"] = true
end
tagname_to_ids(update_target_list)
flush_cache
hotentry_manager = HotentryManager.new
interval = Interval.new(@options["interval"])
begin
sort_by_key(sort_key, update_target_list).each_with_index do |target, i|
display_message = nil
data = Downloader.get_data_by_target(target)
if !data
display_message = "<bold><red>[ERROR]</red></bold> #{target} は管理小説の中に存在しません".termcolor
elsif Narou.novel_frozen?(target) && !@options["force"]
next if argv.empty?
display_message = "ID:#{data["id"]} #{data["title"]} は凍結中です"
end
Helper.print_horizontal_rule if i > 0
if display_message
puts display_message
mistook_count += 1
next
end
interval.wait
downloader = Downloader.new(target)
hotentry_manager.connect(downloader)
delete_modified_tag = -> do
tags = data["tags"] || []
data["tags"] = tags - [Narou::MODIFIED_TAG] if tags.include?(Narou::MODIFIED_TAG)
data["last_check_date"] = Time.now
end
result = downloader.start_download
case result.status
when :ok
delete_modified_tag.call
trigger(:success, data)
puts "#{data["title"]} の更新が完了しました" if Narou.concurrency_enabled?
if @options["no-convert"] ||
(@options["convert-only-new-arrival"] && !result.new_arrivals)
interval.force_wait
next
end
when :failed
puts "ID:#{data["id"]} #{data["title"]} の更新は失敗しました"
mistook_count += 1
next
when :canceled
puts "ID:#{data["id"]} #{data["title"]} の更新はキャンセルされました"
mistook_count += 1
next
when :none
delete_modified_tag.call
puts "#{data["title"]} に更新はありません"
next unless data["_convert_failure"]
end
if data["_convert_failure"]
puts "<yellow>前回変換できなかったので再変換します</yellow>".termcolor
end
convert_argv = [target]
convert_argv << "--no-open" if @options["no-open"]
convert_status = Convert.execute!(convert_argv)
if convert_status > 0
data["_convert_failure"] = true
raise Interrupt if convert_status == Narou::EXIT_INTERRUPT
else
data.delete("_convert_failure")
end
end
ensure
Database.instance.save_database
process_hotentry(hotentry_manager.hotentries)
end
exit mistook_count if mistook_count > 0
rescue Interrupt
puts "アップデートを中断しました"
Narou::Worker.cancel if Narou.concurrency_enabled?
exit Narou::EXIT_INTERRUPT
end
|
#get_data_value(target, key) ⇒ Object
90
91
92
93
94
|
# File 'lib/command/update.rb', line 90
def get_data_value(target, key)
data = Downloader.get_data_by_target(target) or return nil
value = data[key]
value ? value : Time.new(0)
end
|
#mail_hotentry ⇒ Object
350
351
352
353
354
|
# File 'lib/command/update.rb', line 350
def mail_hotentry
hotentry_manager = HotentryManager.new
return unless hotentry_manager.auto_mail?
Mail.execute!("hotentry")
end
|
#process_hotentry(hotentry) ⇒ Object
252
253
254
255
256
257
258
259
260
261
262
|
# File 'lib/command/update.rb', line 252
def process_hotentry(hotentry)
return if hotentry.empty?
cmd_convert = Command::Convert.new
cmd_convert.load_local_settings
cmd_convert.device = Narou.get_device
ebook_path = convert_hotentry(hotentry, cmd_convert)
copy_hotentry(ebook_path, cmd_convert)
send_hotentry(ebook_path, cmd_convert)
mail_hotentry
end
|
#send_hotentry(ebook_path, cmd_convert) ⇒ Object
346
347
348
|
# File 'lib/command/update.rb', line 346
def send_hotentry(ebook_path, cmd_convert)
cmd_convert.send_file_to_device(ebook_path, io: stream_io)
end
|
#sort_by_key(key, list) ⇒ Object
項目名でアップデート対象をソートする
key に偽を渡した場合はソートしない
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
# File 'lib/command/update.rb', line 102
def sort_by_key(key, list)
return list unless key
list.sort { |a, b|
value_a, value_b = [a, b].map { |target|
get_data_value(target, key)
}
if value_a.nil? && !value_b.nil?
next 1
elsif !value_a.nil? && value_b.nil?
next -1
elsif value_a.nil? && value_b.nil?
next 0
end
if value_a.class == Time
value_b <=> value_a
else
value_a <=> value_b
end
}
end
|
#sort_key ⇒ Object
128
129
130
131
132
133
134
135
136
|
# File 'lib/command/update.rb', line 128
def sort_key
key = @options["sort-by"]
return nil unless key
key.downcase!
return key if valid_sort_key?(key)
error "#{key} は正しいキーではありません。次の中から選択して下さい\n " \
"#{Narou.update_sort_key_summaries(17)}"
exit Narou::EXIT_ERROR_CODE
end
|
#update_general_lastup(option = nil) ⇒ Object
235
236
237
238
239
240
241
242
243
244
245
246
247
|
# File 'lib/command/update.rb', line 235
def update_general_lastup(option = nil)
puts "最新話掲載日を確認しています..."
updater = GeneralLastupUpdater.new(@options)
updater.update_narou_novels if !option || option == "narou"
if !option || option == "other"
sleep Narou::API::REQUEST_INTERVAL unless option
updater.update_other_novels
end
updater.save
puts "確認が完了しました"
end
|
#valid_sort_key?(key) ⇒ Boolean
124
125
126
|
# File 'lib/command/update.rb', line 124
def valid_sort_key?(key)
Narou::UPDATE_SORT_KEYS.keys.include?(key)
end
|