Class: Command::Update
Constant Summary
collapse
- LOG_DIR_NAME =
"log"
- LOG_NUM_LIMIT =
30
- LOG_FILENAME_FORMAT =
"update_log_%s.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
#initialize ⇒ Update
Returns a new instance of Update.
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
|
# File 'lib/command/update.rb', line 19
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
}
end
|
Class Method Details
.oneline_help ⇒ Object
15
16
17
|
# File 'lib/command/update.rb', line 15
def self.oneline_help
"小説を更新します"
end
|
Instance Method Details
#create_log_dir ⇒ Object
146
147
148
149
150
|
# File 'lib/command/update.rb', line 146
def create_log_dir
logdir = log_dirname
return if File.directory?(logdir)
FileUtils.mkdir_p(logdir)
end
|
#execute(argv) ⇒ Object
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
|
# File 'lib/command/update.rb', line 58
def execute(argv)
super
mistook_count = 0
update_target_list = argv.dup
no_open = false
if update_target_list.empty?
Database.instance.each_key do |id|
update_target_list << id
end
no_open = true
end
tagname_to_ids(update_target_list)
update_log = $stdout.capture(quiet: false) do
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)
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
result = Downloader.start(target)
case result.status
when :ok
unless @options["no-convert"] ||
(@options["convert-only-new-arrival"] && !result.new_arrivals)
convert_argv = [target]
convert_argv << "--no-open" if no_open
Convert.execute!(convert_argv)
end
when :failed
puts "ID:#{data["id"]} #{data["title"]} の更新は失敗しました"
mistook_count += 1
when :canceled
puts "ID:#{data["id"]} #{data["title"]} の更新はキャンセルされました"
mistook_count += 1
when :none
puts "#{data["title"]} に更新はありません"
end
end
end
save_log(update_log)
exit mistook_count if mistook_count > 0
rescue Interrupt
puts "アップデートを中断しました"
exit Narou::EXIT_ERROR_CODE
end
|
#get_latest_dates(setting) ⇒ Object
オンラインの目次からgeneral_lastupを取得する ただし、toc.yaml に最新話が存在し、かつsubdateが設定されていたらそれを使う
200
201
202
203
204
205
206
207
208
|
# File 'lib/command/update.rb', line 200
def get_latest_dates(setting)
downloader = Downloader.new(setting)
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_paths ⇒ Object
116
117
118
|
# File 'lib/command/update.rb', line 116
def get_log_paths
Dir.glob(File.join(log_dirname, LOG_FILENAME_FORMAT % "*")).sort.reverse
end
|
#remove_old_log ⇒ Object
152
153
154
155
156
157
|
# File 'lib/command/update.rb', line 152
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
130
131
132
133
134
135
136
137
138
139
140
|
# File 'lib/command/update.rb', line 130
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
|
#update_general_lastup(through_frozen_novel: true) ⇒ Object
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
|
# File 'lib/command/update.rb', line 159
def update_general_lastup(through_frozen_novel: true)
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(setting)
rescue OpenURI::HTTPError, Errno::ECONNRESET => e
setting.clear
next
end
end
database[id].merge!(dates)
setting.clear
end
database.save_database
progressbar.clear
puts "更新が完了しました"
end
|
#view_log ⇒ Object
120
121
122
123
124
125
126
127
128
|
# File 'lib/command/update.rb', line 120
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
|