Class: Command::Update

Inherits:
CommandBase show all
Extended by:
Memoist
Defined in:
lib/command/update.rb

Defined Under Namespace

Classes: Interval

Constant Summary collapse

LOG_DIR_NAME =
"log"
LOG_NUM_LIMIT =

ログの保存する上限数

30
LOG_FILENAME_FORMAT =
"update_log_%s.txt"
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"

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from CommandBase

execute!, #force_change_settings_function, help, #hook_call, #load_local_settings, #tagname_to_ids

Constructor Details

#initializeUpdate

Returns a new instance of Update.



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
# File 'lib/command/update.rb', line 46

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/

  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("-l", "--log [N]", "最新からN番目のログを表示する(デフォ1)") { |n|
    n = n.to_i
    n -= 1 if n > 0
    @options["log"] = n
    view_log
    exit 0
  }
  @opt.on("--gl", "データベースに最新話掲載日を反映させる") {
    update_general_lastup
    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
  }
end

Class Method Details

.get_newest_hotentry_file_path(device) ⇒ Object



440
441
442
443
# File 'lib/command/update.rb', line 440

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_dirnameObject



436
437
438
# File 'lib/command/update.rb', line 436

def self.hotentry_dirname
  @@__hotentry_dirname ||= File.join(Narou.get_root_dir, HOTENTRY_DIR_NAME)
end

.oneline_helpObject



42
43
44
# File 'lib/command/update.rb', line 42

def self.oneline_help
  "小説を更新します"
end

Instance Method Details

#convert_hotentry(hotentry, cmd_convert) ⇒ Object



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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
# File 'lib/command/update.rb', line 350

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)
      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
    })
  }
  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



423
424
425
# File 'lib/command/update.rb', line 423

def copy_hotentry(ebook_path, cmd_convert)
  cmd_convert.copy_to_converted_file(ebook_path)
end

#create_inclusive_directory(path) ⇒ Object



419
420
421
# File 'lib/command/update.rb', line 419

def create_inclusive_directory(path)
  FileUtils.mkdir_p(File.dirname(path))
end

#create_log_dirObject



268
269
270
271
272
# File 'lib/command/update.rb', line 268

def create_log_dir
  logdir = log_dirname
  return if File.directory?(logdir)
  FileUtils.mkdir_p(logdir)
end

#execute(argv) ⇒ Object



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
# File 'lib/command/update.rb', line 129

def execute(argv)
  super
  mistook_count = 0
  update_target_list = argv.dup
  @options["no-open"] = false
  if update_target_list.empty?
    Database.instance.each_key do |id|
      update_target_list << id
    end
    @options["no-open"] = true
  end
  tagname_to_ids(update_target_list)

  sort_key = @options["sort-by"]
  if sort_key
    sort_key.downcase!
    unless valid_sort_key?(sort_key)
      error "#{sort_key} は正しいキーではありません。次の中から選択して下さい\n " \
            "#{Narou.update_sort_key_summaries(17)}"
      exit Narou::EXIT_ERROR_CODE
    end
  end
  flush_cache    # memoist のキャッシュ削除

  inv = Inventory.load("local_setting")
  @options["hotentry"] = inv["hotentry"]
  @options["hotentry.auto-mail"] = inv["hotentry.auto-mail"]
  hotentry = {}

  interval = Interval.new(@options["interval"])

  update_log = $stdout.capture(quiet: false) do
    sort_by_key(sort_key, update_target_list).each_with_index do |target, i|
      interval.wait
      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"]
        if argv.length > 0
          display_message = "ID:#{data["id"]} #{data["title"]} は凍結中です"
        else
          next
        end
      end
      Helper.print_horizontal_rule if i > 0
      if display_message
        puts display_message
        mistook_count += 1
        next
      end
      downloader = Downloader.new(target)

      if @options["hotentry"]
        downloader.on(:newarrival) do |hash|
          entry = hotentry[hash[:id]] ||= []
          entry << hash[:subtitle_info]
        end
      end

      result = downloader.start_download
      case result.status
      when :ok
        if @options["no-convert"] ||
             (@options["convert-only-new-arrival"] && !result.new_arrivals)
          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
        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

    process_hotentry(hotentry)
  end

  exit mistook_count if mistook_count > 0
rescue Interrupt
  puts "アップデートを中断しました"
  exit Narou::EXIT_INTERRUPT
ensure
  save_log(update_log)
  Database.instance.save_database
end

#get_data_value(target, key) ⇒ Object



91
92
93
94
95
# File 'lib/command/update.rb', line 91

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

#get_latest_dates(target) ⇒ Object

オンラインの目次からgeneral_lastupを取得する ただし、toc.yaml に最新話が存在し、かつsubdateが設定されていたらそれを使う



325
326
327
328
329
330
331
332
333
# File 'lib/command/update.rb', line 325

def get_latest_dates(target)
  downloader = Downloader.new(target)
  old_toc = downloader.load_toc_file
  latest_toc = downloader.get_latest_table_of_contents(old_toc, through_error: true)
  {
    "novelupdated_at" => downloader.get_novelupdated_at,
    "general_lastup" => downloader.get_general_lastup
  }
end

#get_log_pathsObject



238
239
240
# File 'lib/command/update.rb', line 238

def get_log_paths
  Dir.glob(File.join(log_dirname, LOG_FILENAME_FORMAT % "*")).sort.reverse
end

#log_dirnameObject



264
265
266
# File 'lib/command/update.rb', line 264

def log_dirname
  @@__log_dirname ||= File.join(Narou.get_root_dir, LOG_DIR_NAME)
end

#mail_hotentryObject



431
432
433
434
# File 'lib/command/update.rb', line 431

def mail_hotentry
  return unless @options["hotentry.auto-mail"]
  Mail.execute!(["hotentry"])
end

#process_hotentry(hotentry) ⇒ Object

新着話をまとめたデータの作成に関する処理



338
339
340
341
342
343
344
345
346
347
348
# File 'lib/command/update.rb', line 338

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

#remove_old_logObject



274
275
276
277
278
279
# File 'lib/command/update.rb', line 274

def remove_old_log
  list = get_log_paths
  (list[LOG_NUM_LIMIT..-1] || []).each do |path|
    File.delete(path)
  end
end

#save_log(log) ⇒ Object



252
253
254
255
256
257
258
259
260
261
262
# File 'lib/command/update.rb', line 252

def save_log(log)
  return unless @options["logging"]
  create_log_dir
  now = Time.now
  logname = File.join(log_dirname, LOG_FILENAME_FORMAT % now.strftime("%Y%m%d_%H%M%S"))
  File.open(logname, "w:UTF-8") do |fp|
    fp.puts "--- ログ出力日時 #{now.strftime("%Y/%m/%d %H:%M:%S")} ---"
    fp.puts log
  end
  remove_old_log
end

#send_hotentry(ebook_path, cmd_convert) ⇒ Object



427
428
429
# File 'lib/command/update.rb', line 427

def send_hotentry(ebook_path, cmd_convert)
  cmd_convert.send_file_to_device(ebook_path)
end

#sort_by_key(key, list) ⇒ Object

項目名でアップデート対象をソートする

key に偽を渡した場合はソートしない



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/command/update.rb', line 103

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

#update_general_lastup(through_frozen_novel: true) ⇒ Object



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
# File 'lib/command/update.rb', line 281

def update_general_lastup(through_frozen_novel: true)
  completed = false
  database = Database.instance
  puts "最新話掲載日を更新しています..."
  progressbar = ProgressBar.new(database.get_object.size - 1)
  database.each.with_index do |(id, data), i|
    progressbar.output(i)
    if through_frozen_novel
      next if Narou.novel_frozen?(id)
    end
    setting =  Downloader.get_sitesetting_by_target(id)
    begin
      info = NovelInfo.load(setting)
    rescue OpenURI::HTTPError, Errno::ECONNRESET => e
      setting.clear
      next
    end
    if info
      dates = {
        "general_firstup" => info["general_firstup"],
        "novelupdated_at" => info["novelupdated_at"],
        "general_lastup" => info["general_lastup"]
      }
    else
      # 小説情報ページがない場合は目次から取得する
      begin
        dates = get_latest_dates(id)
      rescue OpenURI::HTTPError, Errno::ECONNRESET => e
        setting.clear
        next
      end
    end
    database[id].merge!(dates)
    setting.clear
  end
  database.save_database
  completed = true
ensure
  progressbar.clear if progressbar
  puts "更新が完了しました" if completed
end

#valid_sort_key?(key) ⇒ Boolean

Returns:

  • (Boolean)


125
126
127
# File 'lib/command/update.rb', line 125

def valid_sort_key?(key)
  Narou::UPDATE_SORT_KEYS.keys.include?(key)
end

#view_logObject



242
243
244
245
246
247
248
249
250
# File 'lib/command/update.rb', line 242

def view_log
  list = get_log_paths
  n = @options["log"]
  if list[n]
    puts File.read(list[n], encoding: Encoding::UTF_8)
  else
    error "#{n+1}番目のログはありません"
  end
end