Class: Command::Download

Inherits:
CommandBase show all
Defined in:
lib/command/download.rb

Constant Summary collapse

SUPPORT_NOVEL_SITES =
%w(小説家になろう(小説を読もう) ノクターンノベルズ ムーンライトノベルズ Arcadia ハーメルン )

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

#initializeDownload

Returns a new instance of Download.



13
14
15
16
17
18
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
# File 'lib/command/download.rb', line 13

def initialize
  super("[<target> <target2> ...] [options]")
  @opt.separator <<-EOS

  ・ダウンロードしたい小説のNコードもしくはURLを指定して下さい。
  ・対応サイトは#{SUPPORT_NOVEL_SITES.join("")}です。
  ・ArcadiaのURLを入力するときは" "で囲って下さい。
  ・ダウンロード終了後に変換処理を行います。ダウンロードのみする場合は-nオプションを指定して下さい。
  ・すでにダウンロード済みの小説の場合は何もしません。
  ・--remove オプションをつけてダウンロードすると、ダウンロード(とその後の変換、送信)が終わったあと削除します。データベースのインデックスを外すだけなので、変換した書籍データは残ったままになります。ファイルを全て消す場合は手動で削除する必要があります。
  ・NコードもURLも指定しなかった場合、対話モード移行します。

  Examples:
narou download n9669bk
narou download http://ncode.syosetu.com/n9669bk/
narou download n9669bk http://ncode.syosetu.com/n4259s/
narou download 0 1 -f
narou download n9669bk -n
narou download n6864bt --remove

  Options:
  EOS
  @opt.on("-f", "--force", "全話を強制再ダウンロードする") {
    @options["force"] = true
  }
  @opt.on("-n", "--no-convert", "変換をせずダウンロードのみ実行する") {
    @options["no-convert"] = true
  }
  @opt.on("-z", "--freeze", "ダウンロードが終了したあと凍結する") {
    @options["freeze"] = true
  }
  @opt.on("-r", "--remove", "ダウンロードが終了したあと削除する") {
    @options["remove"] = true
  }
end

Class Method Details

.oneline_helpObject



135
136
137
# File 'lib/command/download.rb', line 135

def self.oneline_help
  "指定した小説をダウンロードします"
end

Instance Method Details

#execute(argv) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
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
# File 'lib/command/download.rb', line 91

def execute(argv)
  super
  if argv.empty?
    targets = interactive_mode
    return if targets.count == 0
    argv += targets
  end
  tagname_to_ids(argv)
  argv.each.with_index(1) do |target, i|
    download_target ||= target
    Helper.print_horizontal_rule if i > 1
    data = Downloader.get_data_by_target(download_target)
    if Narou.novel_frozen?(download_target)
      puts "#{data["title"]} は凍結中です\nダウンロードを中止しました"
      next
    end
    if !@options["force"] && data
      if Downloader.get_novel_data_dir_by_target(download_target)
        puts "#{download_target} はダウンロード済みです。"
        puts "ID: #{data["id"]}"
        puts "title: #{data["title"]}"
      else
        if Helper.confirm("再ダウンロードしますか")
          download_target = data["toc_url"]
          redo
        end
      end
      next
    end
    if Downloader.start(download_target, @options["force"], true).status != :ok
      next
    end
    unless @options["no-convert"]
      Convert.execute!([download_target])
    end
    if @options["freeze"]
      Freeze.execute!([download_target])
    elsif @options["remove"]
      # --freeze オプションが指定された場合は --remove オプションは無視する
      Remove.execute!([download_target, "-y"])
    end
  end
end

#interactive_modeObject



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
# File 'lib/command/download.rb', line 65

def interactive_mode
  targets = []
  puts "【対話モード】"
  puts "ダウンロードしたい小説のNコードもしくはURLを入力して下さい。(1行に1つ)"
  puts "連続して複数の小説を入力していきます。"
  puts "対応サイトは#{SUPPORT_NOVEL_SITES.join("")}です。"
  puts "入力を終了してダウンロードを開始するには未入力のままエンターを押して下さい。"
  puts
  print_prompt(targets)
  while input = $stdin.gets
    input.strip!
    break if input == ""
    if valid_target?(input)
      if targets.include?(input)
        error "入力済みです"
      else
        targets << input
      end
    else
      error "対応外の小説です"
    end
    print_prompt(targets)
  end
  targets
end


61
62
63
# File 'lib/command/download.rb', line 61

def print_prompt(targets)
  print "#{targets.count}> "
end

#valid_target?(target) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
52
53
54
55
56
57
58
59
# File 'lib/command/download.rb', line 49

def valid_target?(target)
  @site_settings ||= Downloader.load_settings
  case Downloader.get_target_type(target)
  when :ncode
    true
  when :url
    !!@site_settings.find { |s| s.multi_match(target, "url") }
  else
    false
  end
end