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.
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
|
# File 'lib/command/update.rb', line 22
def initialize
super("[<target> ...] [options]")
@opt.separator "\n \u30FB\u7BA1\u7406\u5BFE\u8C61\u306E\u5C0F\u8AAC\u3092\u66F4\u65B0\u3057\u307E\u3059\u3002\n\u66F4\u65B0\u3057\u305F\u3044\u5C0F\u8AAC\u306EN\u30B3\u30FC\u30C9\u3001URL\u3001\u30BF\u30A4\u30C8\u30EB\u3001ID\u3082\u3057\u304F\u306F\u5225\u540D\u3092\u6307\u5B9A\u3057\u3066\u4E0B\u3055\u3044\u3002\nID\u306F \#{@opt.program_name} list \u3092\u53C2\u7167\u3057\u3066\u4E0B\u3055\u3044\u3002\n \u30FB\u5BFE\u8C61\u3092\u6307\u5B9A\u3057\u306A\u304B\u3063\u305F\u5834\u5408\u3001\u3059\u3079\u3066\u306E\u5C0F\u8AAC\u306E\u66F4\u65B0\u3092\u30C1\u30A7\u30C3\u30AF\u3057\u307E\u3059\u3002\n \u30FB\u4E00\u5EA6\u306B\u8907\u6570\u306E\u5C0F\u8AAC\u3092\u6307\u5B9A\u3059\u308B\u5834\u5408\u306F\u7A7A\u767D\u3067\u533A\u5207\u3063\u3066\u4E0B\u3055\u3044\u3002\n \u30FB\u5168\u3066\u66F4\u65B0\u3059\u308B\u5834\u5408\u3001convert.no-open\u304C\u8A2D\u5B9A\u3055\u308C\u3066\u3044\u306A\u304F\u3066\u3082\u4FDD\u5B58\u30D5\u30A9\u30EB\u30C0\u306F\u958B\u304D\u307E\u305B\u3093\u3002\n\n Examples:\nnarou update # \u5168\u3066\u66F4\u65B0\nnarou u # \u77ED\u7E2E\u30B3\u30DE\u30F3\u30C9\nnarou update 0 1 2 4\nnarou update n9669bk \u7570\u4E16\u754C\u8FF7\u5BAE\u3067\u5974\u96B7\u30CF\u30FC\u30EC\u30E0\u3092\nnarou update http://ncode.syosetu.com/n9669bk/\n\n Options:\n EOS\n @opt.on(\"-n\", \"--no-convert\", \"\u5909\u63DB\u3092\u305B\u305A\u30A2\u30C3\u30D7\u30C7\u30FC\u30C8\u306E\u307F\u5B9F\u884C\u3059\u308B\") {\n @options[\"no-convert\"] = true\n }\n @opt.on(\"-a\", \"--convert-only-new-arrival\", \"\u65B0\u7740\u306E\u307F\u5909\u63DB\u3092\u5B9F\u884C\u3059\u308B\") {\n @options[\"convert-only-new-arrival\"] = true\n }\n @opt.on(\"-l\", \"--log [N]\", \"\u6700\u65B0\u304B\u3089N\u756A\u76EE\u306E\u30ED\u30B0\u3092\u8868\u793A\u3059\u308B(\u30C7\u30D5\u30A91)\") { |n|\n n = n.to_i\n n -= 1 if n > 0\n @options[\"log\"] = n\n view_log\n exit 0\n }\n @opt.on(\"--gl\", \"\u30C7\u30FC\u30BF\u30D9\u30FC\u30B9\u306B\u6700\u65B0\u8A71\u63B2\u8F09\u65E5\u3092\u53CD\u6620\u3055\u305B\u308B\") {\n update_general_lastup\n exit 0\n }\n @opt.on(\"-s\", \"--sort-by KEY\", \"\u30A2\u30C3\u30D7\u30C7\u30FC\u30C8\u3059\u308B\u9806\u756A\u3092\u5909\u66F4\u3059\u308B\\n\#{Narou.update_sort_key_summaries}\") { |key|\n @options[\"sort-by\"] = key\n }\nend\n"
|
Class Method Details
.oneline_help ⇒ Object
18
19
20
|
# File 'lib/command/update.rb', line 18
def self.oneline_help
"小説を更新します"
end
|
Instance Method Details
#create_log_dir ⇒ Object
202
203
204
205
206
|
# File 'lib/command/update.rb', line 202
def create_log_dir
logdir = log_dirname
return if File.directory?(logdir)
FileUtils.mkdir_p(logdir)
end
|
#execute(argv) ⇒ Object
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
|
# File 'lib/command/update.rb', line 102
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)
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
update_log = $stdout.capture(quiet: false) do
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)
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_data_value(target, key) ⇒ Object
64
65
66
67
68
|
# File 'lib/command/update.rb', line 64
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(setting) ⇒ Object
オンラインの目次からgeneral_lastupを取得するただし、toc.yaml に最新話が存在し、かつsubdateが設定されていたらそれを使う
256
257
258
259
260
261
262
263
264
|
# File 'lib/command/update.rb', line 256
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
172
173
174
|
# File 'lib/command/update.rb', line 172
def get_log_paths
Dir.glob(File.join(log_dirname, LOG_FILENAME_FORMAT % "*")).sort.reverse
end
|
#log_dirname ⇒ Object
198
199
200
|
# File 'lib/command/update.rb', line 198
def log_dirname
@@__log_dirname ||= File.join(Narou.get_root_dir, LOG_DIR_NAME)
end
|
#remove_old_log ⇒ Object
208
209
210
211
212
213
|
# File 'lib/command/update.rb', line 208
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
186
187
188
189
190
191
192
193
194
195
196
|
# File 'lib/command/update.rb', line 186
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
|
#sort_by_key(key, list) ⇒ Object
項目名でアップデート対象をソートする
key に偽を渡した場合はソートしない
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
# File 'lib/command/update.rb', line 76
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
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
|
# File 'lib/command/update.rb', line 215
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
|
#valid_sort_key?(key) ⇒ Boolean
98
99
100
|
# File 'lib/command/update.rb', line 98
def valid_sort_key?(key)
Narou::UPDATE_SORT_KEYS.keys.include?(key)
end
|
#view_log ⇒ Object
176
177
178
179
180
181
182
183
184
|
# File 'lib/command/update.rb', line 176
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
|