Class: Command::Restore346

Inherits:
CommandBase show all
Defined in:
lib/command/restore346.rb

Instance Attribute Summary

Attributes inherited from CommandBase

#stream_io

Class Method Summary collapse

Instance Method Summary collapse

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

#initializeRestore346

Returns a new instance of Restore346.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/command/restore346.rb', line 16

def initialize
  super("<target> [<target2> ...]")
  @opt.separator <<-HELP

  ・ver 3.4.6 で更新して壊れた toc.yaml の更新日時部分を復元します
  ・このコマンドを実行しなくても更新は出来ますが、改稿がある話のDLが走ってしまいます

  Examples:
narou restore346

  HELP

  @opt.on('-f') do
    @options["f"] = true
  end
end

Class Method Details

.oneline_helpObject



12
13
14
# File 'lib/command/restore346.rb', line 12

def self.oneline_help
  "ver 3.4.6 で壊れた toc.yaml の復元"
end

Instance Method Details

#execute(argv) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/command/restore346.rb', line 33

def execute(argv)
  super
  database_values = Database.instance.get_object.values
  database_values.each do |data|
    begin
      puts "<gray>#{data["title"].escape}</gray>".termcolor
      downloader = Downloader.new(data["id"])
      toc = downloader.load_toc_file
      modified = @options["f"] ? restore_subupdate(toc) : restore(toc)
      downloader.save_toc_once(toc) if modified
    rescue StandardError
      next
    end
  end
end

#restore(toc) ⇒ Object



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

def restore(toc)
  subtitles = toc["subtitles"]
  modified = false
  subtitles.each do |subtitle|
    next unless subtitle["subdate"] =~ /\A(?<subdate>.+?)<span title="(?<subupdate>.+?) 改稿">/

    subdate = Regexp.last_match[:subdate]
    subupdate = Regexp.last_match[:subupdate]

    download_time = subtitle["download_time"]
    if Time.parse(subupdate) > download_time
      subupdate = ''
    end

    subtitle["subdate"] = subdate
    subtitle["subupdate"] = subupdate
    modified = true
  end

  if modified
    puts "<green>#{toc["title"].escape} の目次データを復元しました</green>".termcolor
  end

  modified
end

#restore_subupdate(toc) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/command/restore346.rb', line 75

def restore_subupdate(toc)
  subtitles = toc["subtitles"]
  modified = false
  subtitles.each do |subtitle|
    subupdate = subtitle["subupdate"]

    next if subupdate.blank?

    download_time = subtitle["download_time"]
    next if Time.parse(subupdate) <= download_time

    subtitle["subupdate"] = ''
    modified = true
  end

  if modified
    puts "<green>#{toc["title"].escape} の目次データを復元しました</green>".termcolor
  end

  modified
end