Class: Command::Update::GeneralLastupUpdater

Inherits:
Object
  • Object
show all
Defined in:
lib/command/update/general_lastup_updater.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ GeneralLastupUpdater

Returns a new instance of GeneralLastupUpdater.



11
12
13
14
15
16
17
# File 'lib/command/update/general_lastup_updater.rb', line 11

def initialize(options)
  @options = options
  @database = Database.instance
  @narou_novels = Hash.new { |hash, key| hash[key] = [] }
  @other_novels = []
  prepare
end

Instance Method Details

#get_latest_dates(target) ⇒ Object

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



94
95
96
97
98
99
100
101
102
# File 'lib/command/update/general_lastup_updater.rb', line 94

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

#prepareObject



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/command/update/general_lastup_updater.rb', line 23

def prepare
  @database.each_key do |id|
    next if Narou.novel_frozen?(id)
    setting = Downloader.get_sitesetting_by_target(id)
    if setting["narou_api_url"]
      @narou_novels[setting["narou_api_url"]] << setting["ncode"]
    else
      @other_novels << id
    end
    setting.clear
  end
end

#saveObject



19
20
21
# File 'lib/command/update/general_lastup_updater.rb', line 19

def save
  @database.save_database
end

#update_narou_novelsObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/command/update/general_lastup_updater.rb', line 36

def update_narou_novels
  @narou_novels.each do |api_url, ncodes|
    api = Narou::API.new(api_url: api_url, ncodes: ncodes, of: "nu-gl")
    api.request.each do |result|
      ncode = result["ncode"]
      data = Downloader.get_data_by_target(ncode)
      last_check_date = data["last_check_date"] || data["last_update"]
      if result["novelupdated_at"] > last_check_date
        data["novelupdated_at"] = result["novelupdated_at"]
        data["general_lastup"] = result["general_lastup"]
        tags = data["tags"] ||= []
        tags << Narou::MODIFIED_TAG unless tags.include?(Narou::MODIFIED_TAG)
      end
      data["last_check_date"] = Time.now
    end
    @other_novels += api.private_novels
  end
end

#update_other_novelsObject



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

def update_other_novels
  progressbar = ProgressBar.new(@other_novels.size - 1)
  interval = Interval.new(@options["interval"])

  @other_novels.each_with_index do |id, index|
    progressbar.output(index)
    interval.wait
    begin
      setting = Downloader.get_sitesetting_by_target(id)
      info = NovelInfo.load(setting)
      dates = if info
                {
                  "novelupdated_at" => info["novelupdated_at"],
                  "general_lastup" => info["general_lastup"]
                }
              else
                # 小説情報ページがない場合は目次から取得する
                get_latest_dates(id)
              end
    rescue OpenURI::HTTPError, Errno::ECONNRESET
      setting.clear
      next
    end
    data = @database[id]
    data.merge!(dates)
    last_check_date = data["last_check_date"] || data["last_update"]
    if data["novelupdated_at"] > last_check_date
      tags = @database[id]["tags"] ||= []
      tags << Narou::MODIFIED_TAG unless tags.include?(Narou::MODIFIED_TAG)
    end
    data["last_check_date"] = Time.now
    setting.clear
  end
ensure
  progressbar.clear if progressbar
end