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.



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

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

Instance Method Details

#prepareObject



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

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



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

def save
  @database.save_database
end

#update_narou_novelsObject



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

def update_narou_novels
  @narou_novels.each do |api_url, ncodes|
    api = Narou::API.new(api_url: api_url, ncodes: ncodes, of: "nu-gl-l")
    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"]
        data["length"] = result["length"]
        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



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

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
      downloader = Downloader.new(id)
      next unless downloader.get_latest_table_of_contents(through_error: true)
      dates = {
        "novelupdated_at" => downloader.get_novelupdated_at,
        "general_lastup" => downloader.get_general_lastup,
        "length" => downloader.novel_length
      }
    rescue OpenURI::HTTPError, Errno::ECONNRESET, Errno::ETIMEDOUT, Net::OpenTimeout
      next
    end
    data = @database[id]
    data.merge!(dates)
    last_check_date = data["last_check_date"] || data["last_update"]
    novelupdated_at = data["novelupdated_at"]
    if novelupdated_at && 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
    downloader.setting.clear
  end
ensure
  progressbar.clear if progressbar
end