Class: Downloader
- Inherits:
-
Object
- Object
- Downloader
- Defined in:
- lib/downloader.rb
Overview
小説サイトからのダウンロード
Defined Under Namespace
Classes: NoSerialNovel
Constant Summary collapse
- NOVEL_SITE_SETTING_DIR =
"webnovel/"
- SECTION_SAVE_DIR_NAME =
本文を保存するディレクトリの名前
"本文"
- CACHE_SAVE_DIR_NAME =
差分用キャッシュ保存用ディレクトリの名前
"cache"
- TOC_FILE_NAME =
"toc.yaml"
- WAITING_TIME_FOR_503 =
503 のときに待機する秒数
20
- RETRY_MAX_FOR_503 =
503 のときに何回再試行するか
5
- ENTITIES =
{ quot: '"', amp: "&", nbsp: " ", lt: "<", gt: ">", copy: "(c)" }
- @@settings =
load_settings
- @@database =
Database.instance
Instance Attribute Summary collapse
-
#id ⇒ Object
readonly
Returns the value of attribute id.
Class Method Summary collapse
-
.get_cache_list(target) ⇒ Object
差分用キャッシュのディレクトリ一覧取得.
-
.get_cache_root_dir(target) ⇒ Object
差分用キャッシュの保存ディレクトリ取得.
-
.get_data_by_target(target) ⇒ Object
target からデータベースのデータを取得.
-
.get_id_by_target(target) ⇒ Object
target のIDを取得.
-
.get_novel_data_dir_by_target(target) ⇒ Object
指定されたIDとかから小説の保存ディレクトリを取得.
-
.get_novel_section_save_dir(archive_path) ⇒ Object
本文格納用ディレクトリを取得.
- .get_sitesetting_by_sitename(sitename) ⇒ Object
-
.get_sitesetting_by_target(target) ⇒ Object
小説サイト設定を取得する.
-
.get_target_type(target) ⇒ Object
target の種別を判別する.
-
.get_toc_data(archive_path) ⇒ Object
toc 読込.
-
.get_toc_url(target) ⇒ Object
指定の小説の目次ページのURLを取得する.
-
.load_settings ⇒ Object
小説サイトの定義ファイルを全部読み込む.
- .novel_exists?(target) ⇒ Boolean
- .remove_novel(target, with_file = false) ⇒ Object
-
.start(target, force = false) ⇒ Object
ターゲット(ID、URL、Nコード、小説名)を指定して小説データのダウンロードを開始する.
Instance Method Summary collapse
-
#a_section_download(subtitle_info) ⇒ Object
指定された話数の本文をダウンロード.
-
#confirm_over18? ⇒ Boolean
18歳以上か確認する.
-
#create_cache_dir ⇒ Object
差分用キャッシュ保存ディレクトリ作成.
-
#different_section?(relative_path, subtitle_info) ⇒ Boolean
すでに保存されている内容とDLした内容が違うかどうか.
-
#extract_elements_in_section(section, subtitle) ⇒ Object
本文を解析して前書き・本文・後書きの要素に分解する.
-
#get_latest_table_of_contents ⇒ Object
目次データを取得する.
-
#get_novel_data_dir ⇒ Object
小説データの格納ディレクトリパス.
-
#get_subtitles(toc_source) ⇒ Object
各話の情報を取得.
-
#init_novel_dir ⇒ Object
小説データの格納ディレクトリを初期設定する.
-
#initialize(site_setting, force = false) ⇒ Downloader
constructor
コンストラクタ.
-
#load_novel_data(filename) ⇒ Object
小説データの格納ディレクトリから読み込む.
-
#move_to_cache_dir(relative_path) ⇒ Object
差分用のキャッシュとして保存.
-
#pretreatment_source(src) ⇒ Object
ダウンロードしてきたデータを使いやすいように処理.
-
#remove_cache_dir ⇒ Object
差分用キャッシュ保存ディレクトリを削除.
-
#restor_entity(str) ⇒ Object
エンティティ復号.
-
#save_novel_data(filename, object) ⇒ Object
小説データの格納ディレクトリに保存.
-
#sections_download_and_save(subtitles) ⇒ Object
小説本文をまとめてダウンロードして保存.
-
#serial_novel? ⇒ Boolean
連載小説かどうか調べる.
- #slice_introduction(lines) ⇒ Object
- #slice_postscript(lines) ⇒ Object
-
#start_download ⇒ Object
ダウンロード処理本体.
-
#update_check(old_subtitles, latest_subtitles) ⇒ Object
更新チェック.
-
#update_database ⇒ Object
データベース更新.
Constructor Details
#initialize(site_setting, force = false) ⇒ Downloader
コンストラクタ
274 275 276 277 278 279 |
# File 'lib/downloader.rb', line 274 def initialize(site_setting, force = false) @setting = site_setting @force = force @cache_dir = nil @id = @@database.get_id("toc_url", @setting["toc_url"]) || @@database.get_new_id end |
Instance Attribute Details
#id ⇒ Object (readonly)
Returns the value of attribute id.
26 27 28 |
# File 'lib/downloader.rb', line 26 def id @id end |
Class Method Details
.get_cache_list(target) ⇒ Object
差分用キャッシュのディレクトリ一覧取得
258 259 260 261 262 263 264 |
# File 'lib/downloader.rb', line 258 def self.get_cache_list(target) dir = get_cache_root_dir(target) if dir return Dir.glob("#{dir}/*") end nil end |
.get_cache_root_dir(target) ⇒ Object
差分用キャッシュの保存ディレクトリ取得
247 248 249 250 251 252 253 |
# File 'lib/downloader.rb', line 247 def self.get_cache_root_dir(target) dir = get_novel_data_dir_by_target(target) if dir return File.join(dir, SECTION_SAVE_DIR_NAME, CACHE_SAVE_DIR_NAME) end nil end |
.get_data_by_target(target) ⇒ Object
target からデータベースのデータを取得
151 152 153 154 155 |
# File 'lib/downloader.rb', line 151 def self.get_data_by_target(target) target = Narou.alias_to_id(target) toc_url = get_toc_url(target) or return nil @@database.get_data("toc_url", toc_url) end |
.get_id_by_target(target) ⇒ Object
target のIDを取得
142 143 144 145 146 |
# File 'lib/downloader.rb', line 142 def self.get_id_by_target(target) target = Narou.alias_to_id(target) toc_url = get_toc_url(target) or return nil @@database.get_id("toc_url", toc_url) end |
.get_novel_data_dir_by_target(target) ⇒ Object
指定されたIDとかから小説の保存ディレクトリを取得
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 |
# File 'lib/downloader.rb', line 110 def self.get_novel_data_dir_by_target(target) target = Narou.alias_to_id(target) type = get_target_type(target) data = nil id = nil case type when :url, :ncode toc_url = get_toc_url(target) data = @@database.get_data("toc_url", toc_url) when :other data = @@database.get_data("title", target) when :id data = @@database[target.to_i] end return nil unless data id = data["id"] file_title = data["file_title"] || data["title"] # 互換性維持のための処理 path = File.join(Database.archive_root_path, data["sitename"], file_title) if File.exists?(path) return path else @@database.delete(id) @@database.save_database error "#{path} が見つかりません。" warn "保存フォルダが消去されていたため、データベースのインデックスを削除しました。" return nil end end |
.get_novel_section_save_dir(archive_path) ⇒ Object
本文格納用ディレクトリを取得
84 85 86 |
# File 'lib/downloader.rb', line 84 def self.get_novel_section_save_dir(archive_path) File.join(archive_path, SECTION_SAVE_DIR_NAME) end |
.get_sitesetting_by_sitename(sitename) ⇒ Object
214 215 216 217 218 219 |
# File 'lib/downloader.rb', line 214 def self.get_sitesetting_by_sitename(sitename) setting = @@settings.find { |s| s["name"] == sitename } return setting if setting error "#{data["sitename"]} の設定ファイルが見つかりません" exit 1 end |
.get_sitesetting_by_target(target) ⇒ Object
小説サイト設定を取得する
72 73 74 75 76 77 78 79 |
# File 'lib/downloader.rb', line 72 def self.get_sitesetting_by_target(target) toc_url = get_toc_url(target) setting = nil if toc_url setting = @@settings.find { |s| s.multi_match(toc_url, "url") } end setting end |
.get_target_type(target) ⇒ Object
target の種別を判別する
ncodeの場合、targetを破壊的に変更する
93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/downloader.rb', line 93 def self.get_target_type(target) case target when URI.regexp :url when /^n\d+[a-z]+$/i target.downcase! :ncode when /^\d+$/, Fixnum :id else :other end end |
.get_toc_data(archive_path) ⇒ Object
toc 読込
160 161 162 |
# File 'lib/downloader.rb', line 160 def self.get_toc_data(archive_path) YAML.load_file(File.join(archive_path, TOC_FILE_NAME)) end |
.get_toc_url(target) ⇒ Object
指定の小説の目次ページのURLを取得する
targetがURLかNコードの場合、実際には小説が存在しないURLが返ってくることがあるのを留意する
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/downloader.rb', line 169 def self.get_toc_url(target) target = Narou.alias_to_id(target) case get_target_type(target) when :url setting = @@settings.find { |s| s.multi_match(target, "url") } return setting["toc_url"] if setting when :ncode @@database.each do |_, data| if data["toc_url"] =~ %r!#{target}/$! return data["toc_url"] end end return "#{@@narou["top_url"]}/#{target}/" when :id data = @@database[target.to_i] return data["toc_url"] if data when :other data = @@database.get_data("title", target) return data["toc_url"] if data end nil end |
.load_settings ⇒ Object
小説サイトの定義ファイルを全部読み込む
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
# File 'lib/downloader.rb', line 224 def self.load_settings settings = [] Dir.glob(File.join(Narou.get_script_dir, NOVEL_SITE_SETTING_DIR, "*.yaml")) do |path| setting = SiteSetting.load_file(path) if setting["name"] == "小説家になろう" @@narou = setting end settings << setting end if settings.empty? error "小説サイトの定義ファイルがひとつもありません" exit 1 end unless @@narou error "小説家になろうの定義ファイルが見つかりませんでした" exit 1 end settings end |
.novel_exists?(target) ⇒ Boolean
192 193 194 195 196 |
# File 'lib/downloader.rb', line 192 def self.novel_exists?(target) target = Narou.alias_to_id(target) id = get_id_by_target(target) or return nil @@database.novel_exists?(id) end |
.remove_novel(target, with_file = false) ⇒ Object
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/downloader.rb', line 198 def self.remove_novel(target, with_file = false) target = Narou.alias_to_id(target) data = get_data_by_target(target) or return nil data_dir = get_novel_data_dir_by_target(target) if with_file FileUtils.remove_entry_secure(data_dir) puts "#{data_dir} を完全に削除しました" else # TOCは消しておかないと再DL時に古いデータがあると誤認する File.delete(File.join(data_dir, TOC_FILE_NAME)) end @@database.delete(data["id"]) @@database.save_database data["title"] end |
.start(target, force = false) ⇒ Object
ターゲット(ID、URL、Nコード、小説名)を指定して小説データのダウンロードを開始する
force が true なら全話強制ダウンロード
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 |
# File 'lib/downloader.rb', line 35 def self.start(target, force = false) setting = nil target = Narou.alias_to_id(target) case type = get_target_type(target) when :url, :ncode setting = get_sitesetting_by_target(target) unless setting error "対応外の#{type}です(#{target})" return false end when :id data = @@database[target.to_i] unless data error "指定のID(#{target})は存在しません" return false end setting = get_sitesetting_by_sitename(data["sitename"]) setting.multi_match(data["toc_url"], "url") when :other data = @@database.get_data("title", target) if data setting = get_sitesetting_by_sitename(data["sitename"]) setting.multi_match(data["toc_url"], "url") else error "指定の小説(#{target})は存在しません" return false end end downloader = Downloader.new(setting, force) result = downloader.start_download setting.clear result end |
Instance Method Details
#a_section_download(subtitle_info) ⇒ Object
指定された話数の本文をダウンロード
551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 |
# File 'lib/downloader.rb', line 551 def a_section_download(subtitle_info) href = subtitle_info["href"] if @setting["tcode"] subtitle_url = @setting.replace_group_values("txtdownload_url", subtitle_info) elsif href[0] == "/" subtitle_url = @setting["top_url"] + href else subtitle_url = @setting["toc_url"] + href end section = nil retry_count = RETRY_MAX_FOR_503 begin open(subtitle_url) do |fp| section = pretreatment_source(fp.read) end rescue OpenURI::HTTPError => e if e. =~ /^503/ if retry_count == 0 error "上限までリトライしましたがファイルがダウンロード出来ませんでした" exit 1 end retry_count -= 1 warn "server message: #{e.}" warn "リトライ待機中……" sleep(WAITING_TIME_FOR_503) retry else raise end end element = extract_elements_in_section(section, subtitle_info["subtitle"]) element end |
#confirm_over18? ⇒ Boolean
18歳以上か確認する
284 285 286 287 288 289 290 291 292 293 294 295 296 |
# File 'lib/downloader.rb', line 284 def confirm_over18? global_setting = GlobalSetting.get["global_setting"] if global_setting.include?("over18") return global_setting["over18"] end if Helper.confirm("年齢認証:あなたは18歳以上ですか") global_setting["over18"] = true GlobalSetting.get.save_settings return true else return false end end |
#create_cache_dir ⇒ Object
差分用キャッシュ保存ディレクトリ作成
351 352 353 354 355 356 357 |
# File 'lib/downloader.rb', line 351 def create_cache_dir now = Time.now name = now.strftime("%Y.%m.%d@%H;%M;%S") cache_dir = File.join(get_novel_data_dir, SECTION_SAVE_DIR_NAME, CACHE_SAVE_DIR_NAME, name) FileUtils.mkdir_p(cache_dir) cache_dir end |
#different_section?(relative_path, subtitle_info) ⇒ Boolean
すでに保存されている内容とDLした内容が違うかどうか
529 530 531 532 533 534 535 536 |
# File 'lib/downloader.rb', line 529 def different_section?(relative_path, subtitle_info) path = File.join(get_novel_data_dir, relative_path) if File.exists?(path) return YAML.load_file(path) != subtitle_info else return true end end |
#extract_elements_in_section(section, subtitle) ⇒ Object
本文を解析して前書き・本文・後書きの要素に分解する
本文に含まれるタイトルは消す
590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 |
# File 'lib/downloader.rb', line 590 def extract_elements_in_section(section, subtitle) lines = section.lines.map(&:rstrip) introduction = slice_introduction(lines) postscript = slice_postscript(lines) if lines[0] == subtitle if lines[1] == "" lines.slice!(0, 2) else lines.slice!(0, 1) end end { "introduction" => introduction, "body" => lines.join("\n"), "postscript" => postscript } end |
#get_latest_table_of_contents ⇒ Object
目次データを取得する
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 |
# File 'lib/downloader.rb', line 399 def get_latest_table_of_contents toc_url = @setting["toc_url"] return nil unless toc_url toc_source = "" begin open(toc_url) do |toc_fp| if toc_fp.base_uri.to_s != toc_url # リダイレクトされた場合。 # ノクターン・ムーンライトのNコードを ncode.syosetu.com に渡すと、novel18.syosetu.com に飛ばされる # 目次の定義が微妙に ncode.syosetu.com と違うので、設定を取得し直す @setting.clear @setting = Downloader.get_sitesetting_by_target(toc_fp.base_uri.to_s) toc_url = @setting["toc_url"] end toc_source = pretreatment_source(toc_fp.read) end rescue OpenURI::HTTPError => e if e. =~ /^404/ error "<red>[404]</red> 小説が削除されている可能性があります" return false else raise end end if @setting["narou_api_url"] && !serial_novel? raise NoSerialNovel end @setting.multi_match(toc_source, "title", "author", "story", "tcode") @title = @setting["title"] @file_title = Helper.replace_filename_special_chars(@title) toc_objects = { "title" => @title, "author" => @setting["author"], "toc_url" => @setting["toc_url"], "story" => @setting["story"], "subtitles" => get_subtitles(toc_source) } toc_objects end |
#get_novel_data_dir ⇒ Object
小説データの格納ディレクトリパス
630 631 632 633 |
# File 'lib/downloader.rb', line 630 def get_novel_data_dir raise "小説名がまだ設定されていません" unless @file_title File.join(Database.archive_root_path, @setting["name"], @file_title) end |
#get_subtitles(toc_source) ⇒ Object
各話の情報を取得
468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 |
# File 'lib/downloader.rb', line 468 def get_subtitles(toc_source) subtitles = [] toc_post = toc_source.dup loop do match_data = @setting.multi_match(toc_post, "subtitles") break unless match_data toc_post = match_data.post_match subtitles << { "index" => @setting["index"], "href" => @setting["href"], "chapter" => @setting["chapter"], "subtitle" => @setting["subtitle"], "file_subtitle" => Helper.replace_filename_special_chars(@setting["subtitle"]), "subdate" => @setting["subdate"], "subupdate" => @setting["subupdate"] } end subtitles end |
#init_novel_dir ⇒ Object
小説データの格納ディレクトリを初期設定する
659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 |
# File 'lib/downloader.rb', line 659 def init_novel_dir novel_dir_path = get_novel_data_dir FileUtils.mkdir_p(novel_dir_path) unless File.exists?(novel_dir_path) default_settings = NovelSetting::DEFAULT_SETTINGS special_preset_dir = File.join(Narou.get_preset_dir, @setting["domain"], @setting["ncode"]) exists_special_preset_dir = File.exists?(special_preset_dir) [NovelSetting::INI_NAME, "converter.rb", NovelSetting::REPLACE_NAME].each do |filename| if exists_special_preset_dir preset_file_path = File.join(special_preset_dir, filename) if File.exists?(preset_file_path) unless File.exists?(File.join(novel_dir_path, filename)) FileUtils.cp(preset_file_path, novel_dir_path) end next end end Template.write(filename, novel_dir_path, binding) end end |
#load_novel_data(filename) ⇒ Object
小説データの格納ディレクトリから読み込む
649 650 651 652 653 654 |
# File 'lib/downloader.rb', line 649 def load_novel_data(filename) dir_path = get_novel_data_dir YAML.load_file(File.join(dir_path, filename)) rescue Errno::ENOENT nil end |
#move_to_cache_dir(relative_path) ⇒ Object
差分用のキャッシュとして保存
541 542 543 544 545 546 |
# File 'lib/downloader.rb', line 541 def move_to_cache_dir(relative_path) path = File.join(get_novel_data_dir, relative_path) if File.exists?(path) && @cache_dir FileUtils.mv(path, @cache_dir) end end |
#pretreatment_source(src) ⇒ Object
ダウンロードしてきたデータを使いやすいように処理
682 683 684 |
# File 'lib/downloader.rb', line 682 def pretreatment_source(src) restor_entity(src.force_encoding(@setting["encoding"])).gsub("\r", "") end |
#remove_cache_dir ⇒ Object
差分用キャッシュ保存ディレクトリを削除
362 363 364 |
# File 'lib/downloader.rb', line 362 def remove_cache_dir FileUtils.remove_entry_secure(@cache_dir) if @cache_dir end |
#restor_entity(str) ⇒ Object
エンティティ復号
690 691 692 693 694 695 696 |
# File 'lib/downloader.rb', line 690 def restor_entity(str) result = str.dup ENTITIES.each do |key, value| result.gsub!("&#{key};", value) end result end |
#save_novel_data(filename, object) ⇒ Object
小説データの格納ディレクトリに保存
638 639 640 641 642 643 644 645 |
# File 'lib/downloader.rb', line 638 def save_novel_data(filename, object) path = File.join(get_novel_data_dir, filename) dir_path = File.dirname(path) unless File.exists?(dir_path) FileUtils.mkdir_p(dir_path) end File.write(path, YAML.dump(object)) end |
#sections_download_and_save(subtitles) ⇒ Object
小説本文をまとめてダウンロードして保存
subtitles にダウンロードしたいものをまとめた subtitle info を渡す
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 |
# File 'lib/downloader.rb', line 493 def sections_download_and_save(subtitles) max = subtitles.count return if max == 0 puts ("<bold><green>" + TermColor.escape("ID:#{@id} #{@title} のDL開始") + "</green></bold>").termcolor interval_sleep_time = LocalSetting.get["local_setting"]["download.interval"] || 0 interval_sleep_time = 0 if interval_sleep_time < 0 save_least_one = false subtitles.each_with_index do |subtitle_info, i| if @setting["domain"] =~ /syosetu.com/ && (i % 10 == 0 && i >= 10) # MEMO: # 小説家になろうは連続DL規制があるため、ウェイトを入れる必要がある。 # 10話ごとに規制が入るため、10話ごとにウェイトを挟む。 # 1話ごとに1秒待機を10回繰り返そうと、11回目に規制が入るため、ウェイトは必ず必要。 sleep(5) else sleep(interval_sleep_time) if i > 0 end index, subtitle, file_subtitle = %w(index subtitle file_subtitle).map {|k| subtitle_info[k] } print "第#{index}部分 #{subtitle} (#{i+1}/#{max})" section_element = a_section_download(subtitle_info) info = subtitle_info.dup info["element"] = section_element section_file_name = "#{index} #{file_subtitle}.yaml" section_file_path = File.join(SECTION_SAVE_DIR_NAME, section_file_name) print " (更新あり)" if @force && different_section?(section_file_path, info) move_to_cache_dir(section_file_path) save_novel_data(section_file_path, info) save_least_one = true puts end remove_cache_dir unless save_least_one end |
#serial_novel? ⇒ Boolean
連載小説かどうか調べる
386 387 388 389 390 391 392 393 394 |
# File 'lib/downloader.rb', line 386 def serial_novel? if @@database[@id] @novel_type = @@database[@id]["novel_type"] || 1 else api = Narou::API.new(@setting, "nt") @novel_type = api["novel_type"] end @novel_type == 1 end |
#slice_introduction(lines) ⇒ Object
608 609 610 611 612 613 614 615 616 |
# File 'lib/downloader.rb', line 608 def slice_introduction(lines) lines.each_with_index do |line, lineno| if line =~ ConverterBase::AUTHOR_INTRODUCTION_SPLITTER lines.slice!(lineno, 1) return lines.slice!(0...lineno).join("\n") end end "" end |
#slice_postscript(lines) ⇒ Object
618 619 620 621 622 623 624 625 626 |
# File 'lib/downloader.rb', line 618 def slice_postscript(lines) lines.each_with_index do |line, lineno| if line =~ ConverterBase::AUTHOR_POSTSCRIPT_SPLITTER lines.slice!(lineno, 1) return lines.slice!(lineno..-1).join("\n") end end "" end |
#start_download ⇒ Object
ダウンロード処理本体
返り値:ダウンロードしたものが1話でもあったかどうか(Boolean)
nil なら何らかの原因でダウンロード自体出来なかった
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 337 338 339 340 341 342 343 344 345 346 |
# File 'lib/downloader.rb', line 304 def start_download begin latest_toc = get_latest_table_of_contents rescue NoSerialNovel error @setting["ncode"] + " は短編小説です。現在短編小説には対応していません" return nil end unless latest_toc error @setting["toc_url"] + " の目次データが取得出来ませんでした" return nil end if @setting["confirm_over18"] unless confirm_over18? puts "18歳以上のみ閲覧出来る小説です。ダウンロードを中止しました" return nil end end old_toc = load_novel_data(TOC_FILE_NAME) unless old_toc init_novel_dir old_toc = {} end if @force update_subtitles = latest_toc["subtitles"] else update_subtitles = update_check(old_toc["subtitles"], latest_toc["subtitles"]) end if update_subtitles.count > 0 @cache_dir = create_cache_dir if old_toc.length > 0 begin sections_download_and_save(update_subtitles) rescue Interrupt remove_cache_dir puts "ダウンロードを中断しました" exit 1 end update_database save_novel_data(TOC_FILE_NAME, latest_toc) return true else return false end end |
#update_check(old_subtitles, latest_subtitles) ⇒ Object
更新チェック
更新された subtitle だけまとまった配列を返す
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 |
# File 'lib/downloader.rb', line 444 def update_check(old_subtitles, latest_subtitles) return latest_subtitles unless old_subtitles latest_subtitles.dup.keep_if do |latest| index = latest["index"] index_in_old_toc = old_subtitles.index { |item| item["index"] == index } next true unless index_in_old_toc old = old_subtitles[index_in_old_toc] # タイトルチェック if old["subtitle"] != latest["subtitle"] next true end # 更新日チェック old_subupdate = old["subupdate"] latest_subupdate = latest["subupdate"] if old_subupdate == "" next latest_subupdate != "" end latest_subupdate > old_subupdate end end |
#update_database ⇒ Object
データベース更新
369 370 371 372 373 374 375 376 377 378 379 380 381 |
# File 'lib/downloader.rb', line 369 def update_database @@database[@id] = { "id" => @id, "author" => @setting["author"], "title" => @setting["title"], "file_title" => @file_title, "toc_url" => @setting["toc_url"], "sitename" => @setting["name"], "novel_type" => @novel_type, "last_update" => Time.now } @@database.save_database end |