Module: NHKore::CLI::GetCmd

Included in:
App
Defined in:
lib/nhkore/cli/get_cmd.rb

Constant Summary collapse

DEFAULT_GET_CHUNK_SIZE =
4 * 1024
DEFAULT_GET_URL_LENGTH =

Just a generous estimation used as a fallback; may be outdated.

11_000_000
GET_URL_FILENAME =
'nhkore-core.zip'
GET_URL =
"https://github.com/esotericpig/nhkore/releases/latest/download/#{GET_URL_FILENAME}"

Instance Method Summary collapse

Instance Method Details

#build_get_cmdObject



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
# File 'lib/nhkore/cli/get_cmd.rb', line 23

def build_get_cmd
  app = self

  @get_cmd = @app_cmd.define_command do
    name    'get'
    usage   'get [OPTIONS] [COMMAND]...'
    aliases :g
    summary "Download NHKore's pre-scraped files from the latest release" \
            " (aliases: #{app.color_alias('g')})"

    description(<<-DESC)
      Download NHKore's pre-scraped files from the latest release &
      save to folder: #{Util::CORE_DIR}

      Note: the latest NHK articles may not have been scraped yet.
    DESC

    option :o,:out,'directory to save downloaded files to',argument: :required,default: Util::CORE_DIR,
        transform: lambda { |value|
          app.check_empty_opt(:out,value)
        }
    flag nil,:'show-url','show download URL and exit (for downloading manually)' do |value,cmd|
      puts GET_URL
      exit
    end

    run do |opts,args,cmd|
      app.refresh_cmd(opts,args,cmd)
      app.run_get_cmd
    end
  end
end

#run_get_cmdObject



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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/nhkore/cli/get_cmd.rb', line 56

def run_get_cmd
  require 'down/net_http'
  require 'tempfile'
  require 'zip'

  build_out_dir(:out,default_dir: Util::CORE_DIR)

  return unless check_out_dir(:out)

  chunk_size = DEFAULT_GET_CHUNK_SIZE
  down = nil
  dry_run = @cmd_opts[:dry_run]
  force = @cmd_opts[:force]
  max_retries = @scraper_kargs[:max_retries]
  max_retries = 3 if max_retries.nil?
  out_dir = @cmd_opts[:out]

  begin
    start_spin('Opening URL')

    begin
      down = Down::NetHttp.open(GET_URL,rewindable: false,**@scraper_kargs)
    rescue Down::ConnectionError
      raise if (max_retries -= 1) < 0
      retry
    end

    stop_spin

    return if dry_run

    Tempfile.create(["#{App::NAME}_",'.zip'],binmode: true) do |file|
      puts
      puts "Downloading #{GET_URL_FILENAME} to temp file:"
      puts "> #{file.path}"

      len = down.size
      len = DEFAULT_GET_LENGTH if len.nil? || len < 1
      bar = build_progress_bar('> Downloading',download: true,total: len)

      bar.start

      while !down.eof?
        file.write(down.read(chunk_size))
        bar.advance(chunk_size)
      end

      down.close
      file.close
      bar.finish

      puts
      puts "Extracting #{GET_URL_FILENAME}..."

      # We manually ask the user whether to overwrite each file, so set this to
      # true so that Zip extract() will force overwrites and not raise an error.
      Zip.on_exists_proc = true

      Zip::File.open(file) do |zip_file|
        zip_file.each do |entry|
          if !entry.name_safe?
            raise ZipError,"unsafe entry name[#{entry.name}] in Zip file"
          end

          name = Util.strip_web_str(File.basename(entry.name))

          next if name.empty?

          out_file = File.join(out_dir,name)

          puts "> #{name}"

          if !force && File.exist?(out_file)
            puts
            puts 'Warning: output file already exists!'
            puts "> '#{out_file}'"

            overwrite = @high.agree('Overwrite this file (yes/no)? ')
            puts

            next unless overwrite
          end

          entry.extract(out_file)
        end
      end

      puts
      puts "Extracted #{GET_URL_FILENAME} to directory:"
      puts "> #{out_dir}"
    end
  ensure
    down.close if !down.nil? && !down.closed?
  end
end