Module: NHKore::CLI::SiftCmd

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

Overview

Author:

  • Jonathan Bradley Whited

Since:

  • 0.2.0

Constant Summary collapse

DEFAULT_SIFT_EXT =

Since:

  • 0.2.0

:csv
DEFAULT_SIFT_FUTSUU_FILE =

Since:

  • 0.2.0

"#{Sifter::DEFAULT_FUTSUU_FILE}{search.criteria}{file.ext}"
DEFAULT_SIFT_YASASHII_FILE =

Since:

  • 0.2.0

"#{Sifter::DEFAULT_YASASHII_FILE}{search.criteria}{file.ext}"
SIFT_EXTS =

Since:

  • 0.2.0

%i[csv htm html json yaml yml].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#sift_datetime_textObject

Since:

  • 0.2.0



33
34
35
# File 'lib/nhkore/cli/sift_cmd.rb', line 33

def sift_datetime_text
  @sift_datetime_text
end

#sift_search_criteriaObject

Since:

  • 0.2.0



34
35
36
# File 'lib/nhkore/cli/sift_cmd.rb', line 34

def sift_search_criteria
  @sift_search_criteria
end

Instance Method Details

#build_sift_cmdObject

Since:

  • 0.2.0



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

def build_sift_cmd
  app = self

  @sift_datetime_text = nil
  @sift_search_criteria = nil

  @sift_cmd = @app_cmd.define_command do
    name    'sift'
    usage   'sift [OPTIONS] [COMMAND]...'
    aliases :s
    summary 'Sift NHK News Web (Easy) articles data for the frequency of words' \
            " (aliases: #{app.color_alias('s')})"

    description(<<-DESC)
      Sift NHK News Web (Easy) articles data for the frequency of words &
      save to folder: #{Sifter::DEFAULT_DIR}
    DESC

    option :d,:datetime,<<-DESC,argument: :required,transform: lambda { |value|
      date time to filter on; examples:
      '2020-7-1 13:10...2020-7-31 11:11';
      '2020-12' (2020, December 1st-31st);
      '7-4...7-9' (July 4th-9th of Current Year);
      '7-9' (July 9th of Current Year);
      '9' (9th of Current Year & Month)
    DESC
      app.sift_datetime_text = value # Save the original value for the file name

      value = DatetimeParser.parse_range(value)

      app.check_empty_opt(:datetime,value) if value.nil?

      value
    }
    option :e,:ext,<<-DESC,argument: :required,default: DEFAULT_SIFT_EXT,transform: lambda { |value|
      type of file (extension) to save; valid options: [#{SIFT_EXTS.join(', ')}];
      not needed if you specify a file extension with the '--out' option: '--out sift.html'
    DESC
      value = Util.unspace_web_str(value).downcase.to_sym

      raise CLIError,"invalid ext[#{value}] for option[#{ext}]" unless SIFT_EXTS.include?(value)

      value
    }
    option :i,:in,<<-DESC,argument: :required,transform: lambda { |value|
      file of NHK News Web (Easy) articles data to sift (see '#{App::NAME} news';
      defaults: #{YasashiiNews::DEFAULT_FILE}, #{FutsuuNews::DEFAULT_FILE})
    DESC
      app.check_empty_opt(:in,value)
    }
    flag :D,:'no-defn','do not output the definitions for words (which can be quite long)'
    flag :E,:'no-eng','do not output the English translations for words'
    option :o,:out,<<-DESC,argument: :required,transform: lambda { |value|
      'directory/file' to save sifted data to; if you only specify a directory or a file, it will attach
      the appropriate default directory/file name
      (defaults: #{DEFAULT_SIFT_YASASHII_FILE}, #{DEFAULT_SIFT_FUTSUU_FILE})
    DESC
      app.check_empty_opt(:out,value)
    }
    flag :H,'no-sha256',<<-DESC
      if you used this option with the 'news' command, then you'll also need this option here
      to not fail on "duplicate" articles; see '#{App::NAME} news'
    DESC
    option :t,:title,'title to filter on, where search text only needs to be somewhere in the title',
      argument: :required
    option :u,:url,'URL to filter on, where search text only needs to be somewhere in the URL',
      argument: :required

    run do |opts,args,cmd|
      puts cmd.help
    end
  end

  @sift_easy_cmd = @sift_cmd.define_command do
    name    'easy'
    usage   'easy [OPTIONS] [COMMAND]...'
    aliases :e,:ez
    summary "Sift NHK News Web Easy (Yasashii) articles data (aliases: #{app.color_alias('e ez')})"

    description <<-DESC
      Sift NHK News Web Easy (Yasashii) articles data for the frequency of words &
      save to file: #{DEFAULT_SIFT_YASASHII_FILE}
    DESC

    run do |opts,args,cmd|
      app.refresh_cmd(opts,args,cmd)
      app.run_sift_cmd(:yasashii)
    end
  end

  @sift_regular_cmd = @sift_cmd.define_command do
    name    'regular'
    usage   'regular [OPTIONS] [COMMAND]...'
    aliases :r,:reg
    summary "Sift NHK News Web Regular (Futsuu) articles data (aliases: #{app.color_alias('r reg')})"

    description(<<-DESC)
      Sift NHK News Web Regular (Futsuu) articles data for the frequency of words &
      save to file: #{DEFAULT_SIFT_FUTSUU_FILE}
    DESC

    run do |opts,args,cmd|
      app.refresh_cmd(opts,args,cmd)
      app.run_sift_cmd(:futsuu)
    end
  end
end

#build_sift_filename(filename) ⇒ Object

Since:

  • 0.2.0



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/nhkore/cli/sift_cmd.rb', line 144

def build_sift_filename(filename)
  @sift_search_criteria = []

  @sift_search_criteria << Util.strip_web_str(@sift_datetime_text.to_s)
  @sift_search_criteria << Util.strip_web_str(@cmd_opts[:title].to_s)
  @sift_search_criteria << Util.strip_web_str(@cmd_opts[:url].to_s)
  @sift_search_criteria.filter! { |sc| !sc.empty? }

  clean_regex = /[^[[:alnum:]]\-_.]+/
  clean_search_criteria = ''.dup

  @sift_search_criteria.each do |sc|
    clean_search_criteria << sc.gsub(clean_regex,'')
  end

  @sift_search_criteria = @sift_search_criteria.empty? ? nil : @sift_search_criteria.join(', ')

  # Limit the file name length.
  #   If length is smaller, [..] still works appropriately.
  clean_search_criteria = clean_search_criteria[0..32]

  clean_search_criteria.prepend('_') unless clean_search_criteria.empty?

  file_ext = @cmd_opts[:ext]

  if file_ext.nil?
    # Try to get from '--out' if it exists.
    if !@cmd_opts[:out].nil?
      file_ext = Util.unspace_web_str(File.extname(@cmd_opts[:out])).downcase
      file_ext = file_ext.sub(/\A\./,'') # Remove '.'; can't be nil for to_sym()
      file_ext = file_ext.to_sym

      file_ext = nil unless SIFT_EXTS.include?(file_ext)
    end

    file_ext = DEFAULT_SIFT_EXT if file_ext.nil?
    @cmd_opts[:ext] = file_ext
  end

  filename = "#{filename}#{clean_search_criteria}.#{file_ext}"

  return filename
end

#run_sift_cmd(type) ⇒ Object

Since:

  • 0.2.0



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/nhkore/cli/sift_cmd.rb', line 188

def run_sift_cmd(type)
  news_name = nil

  case type
  when :futsuu
    build_in_file(:in,default_dir: News::DEFAULT_DIR,default_filename: FutsuuNews::DEFAULT_FILENAME)
    build_out_file(:out,default_dir: Sifter::DEFAULT_DIR,
      default_filename: build_sift_filename(Sifter::DEFAULT_FUTSUU_FILENAME))

    news_name = 'Regular'
  when :yasashii
    build_in_file(:in,default_dir: News::DEFAULT_DIR,default_filename: YasashiiNews::DEFAULT_FILENAME)
    build_out_file(:out,default_dir: Sifter::DEFAULT_DIR,
      default_filename: build_sift_filename(Sifter::DEFAULT_YASASHII_FILENAME))

    news_name = 'Easy'
  else
    raise ArgumentError,"invalid type[#{type}]"
  end

  return unless check_in_file(:in,empty_ok: false)
  return unless check_out_file(:out)

  datetime_filter = @cmd_opts[:datetime]
  dry_run = @cmd_opts[:dry_run]
  file_ext = @cmd_opts[:ext]
  in_file = @cmd_opts[:in]
  no_defn = @cmd_opts[:no_defn]
  no_eng = @cmd_opts[:no_eng]
  no_sha256 = @cmd_opts[:no_sha256]
  out_file = @cmd_opts[:out]
  title_filter = @cmd_opts[:title]
  url_filter = @cmd_opts[:url]

  start_spin("Sifting NHK News Web #{news_name} data")

  news = (type == :yasashii) ?
    YasashiiNews.load_file(in_file,overwrite: no_sha256) :
    FutsuuNews.load_file(in_file,overwrite: no_sha256)

  sifter = Sifter.new(news)

  sifter.filter_by_datetime(datetime_filter) unless datetime_filter.nil?
  sifter.filter_by_title(title_filter) unless title_filter.nil?
  sifter.filter_by_url(url_filter) unless url_filter.nil?
  sifter.ignore(:defn) if no_defn
  sifter.ignore(:eng) if no_eng

  sifter.caption = "NHK News Web #{news_name}".dup

  if !@sift_search_criteria.nil?
    if %i[htm html].any?(file_ext)
      sifter.caption << " &mdash; #{Util.escape_html(@sift_search_criteria.to_s)}"
    else
      sifter.caption << " -- #{@sift_search_criteria}"
    end
  end

  case file_ext
  when :csv
    sifter.put_csv!
  when :htm,:html
    sifter.put_html!
  when :json
    sifter.put_json!
  when :yaml,:yml
    sifter.put_yaml!
  else
    raise ArgumentError,"invalid file ext[#{file_ext}]"
  end

  stop_spin
  puts

  if dry_run
    puts sifter.to_s
  else
    start_spin('Saving sifted data to file')

    sifter.save_file(out_file)

    stop_spin
    puts "> #{out_file}"
  end
end