nhkore 0.3.6 → 0.3.10
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.yardopts +3 -0
- data/CHANGELOG.md +63 -2
- data/Gemfile +0 -18
- data/Gemfile.lock +89 -0
- data/README.md +36 -30
- data/Rakefile +38 -52
- data/bin/nhkore +4 -15
- data/lib/nhkore.rb +8 -20
- data/lib/nhkore/app.rb +236 -236
- data/lib/nhkore/article.rb +39 -53
- data/lib/nhkore/article_scraper.rb +301 -287
- data/lib/nhkore/cleaner.rb +20 -32
- data/lib/nhkore/cli/fx_cmd.rb +41 -53
- data/lib/nhkore/cli/get_cmd.rb +59 -70
- data/lib/nhkore/cli/news_cmd.rb +143 -153
- data/lib/nhkore/cli/search_cmd.rb +108 -118
- data/lib/nhkore/cli/sift_cmd.rb +109 -120
- data/lib/nhkore/datetime_parser.rb +89 -103
- data/lib/nhkore/defn.rb +48 -55
- data/lib/nhkore/dict.rb +26 -38
- data/lib/nhkore/dict_scraper.rb +31 -40
- data/lib/nhkore/entry.rb +43 -55
- data/lib/nhkore/error.rb +16 -21
- data/lib/nhkore/fileable.rb +10 -21
- data/lib/nhkore/lib.rb +5 -17
- data/lib/nhkore/missingno.rb +21 -33
- data/lib/nhkore/news.rb +58 -72
- data/lib/nhkore/polisher.rb +22 -34
- data/lib/nhkore/scraper.rb +75 -82
- data/lib/nhkore/search_link.rb +63 -75
- data/lib/nhkore/search_scraper.rb +89 -93
- data/lib/nhkore/sifter.rb +157 -171
- data/lib/nhkore/splitter.rb +19 -31
- data/lib/nhkore/user_agents.rb +28 -32
- data/lib/nhkore/util.rb +72 -84
- data/lib/nhkore/variator.rb +20 -32
- data/lib/nhkore/version.rb +4 -16
- data/lib/nhkore/word.rb +105 -99
- data/nhkore.gemspec +54 -65
- data/samples/looper.rb +71 -0
- data/test/nhkore/test_helper.rb +3 -15
- data/test/nhkore_test.rb +6 -18
- metadata +50 -28
@@ -1,23 +1,11 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
1
|
# encoding: UTF-8
|
3
2
|
# frozen_string_literal: true
|
4
3
|
|
5
4
|
#--
|
6
5
|
# This file is part of NHKore.
|
7
|
-
# Copyright (c) 2020 Jonathan Bradley Whited
|
8
|
-
#
|
9
|
-
#
|
10
|
-
# it under the terms of the GNU Lesser General Public License as published by
|
11
|
-
# the Free Software Foundation, either version 3 of the License, or
|
12
|
-
# (at your option) any later version.
|
13
|
-
#
|
14
|
-
# NHKore is distributed in the hope that it will be useful,
|
15
|
-
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
16
|
-
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
17
|
-
# GNU Lesser General Public License for more details.
|
18
|
-
#
|
19
|
-
# You should have received a copy of the GNU Lesser General Public License
|
20
|
-
# along with NHKore. If not, see <https://www.gnu.org/licenses/>.
|
6
|
+
# Copyright (c) 2020-2021 Jonathan Bradley Whited
|
7
|
+
#
|
8
|
+
# SPDX-License-Identifier: LGPL-3.0-or-later
|
21
9
|
#++
|
22
10
|
|
23
11
|
|
@@ -30,122 +18,122 @@ require 'nhkore/util'
|
|
30
18
|
module NHKore
|
31
19
|
module CLI
|
32
20
|
###
|
33
|
-
# @author Jonathan Bradley Whited
|
21
|
+
# @author Jonathan Bradley Whited
|
34
22
|
# @since 0.3.0
|
35
23
|
###
|
36
24
|
module SearchCmd
|
37
|
-
def build_search_cmd
|
25
|
+
def build_search_cmd
|
38
26
|
app = self
|
39
|
-
|
40
|
-
@search_cmd = @app_cmd.define_command
|
27
|
+
|
28
|
+
@search_cmd = @app_cmd.define_command do
|
41
29
|
name 'search'
|
42
30
|
usage 'search [OPTIONS] [COMMAND]...'
|
43
31
|
aliases :se,:sea
|
44
32
|
summary "Search for links to NHK News Web (Easy) (aliases: #{app.color_alias('se sea')})"
|
45
|
-
|
46
|
-
description <<-
|
33
|
+
|
34
|
+
description <<-DESC
|
47
35
|
Search for links (using a Search Engine, etc.) to NHK News Web (Easy) &
|
48
36
|
save to folder: #{SearchLinks::DEFAULT_DIR}
|
49
|
-
|
50
|
-
|
51
|
-
option :i,:in,<<-
|
37
|
+
DESC
|
38
|
+
|
39
|
+
option :i,:in,<<-DESC,argument: :required,transform: lambda { |value|
|
52
40
|
file to read instead of URL (for offline testing and/or slow internet;
|
53
41
|
see '--show-*' options)
|
54
|
-
|
42
|
+
DESC
|
55
43
|
app.check_empty_opt(:in,value)
|
56
|
-
|
57
|
-
option :o,:out,<<-
|
44
|
+
}
|
45
|
+
option :o,:out,<<-DESC,argument: :required,transform: lambda { |value|
|
58
46
|
'directory/file' to save links to; if you only specify a directory or a file, it will attach the
|
59
47
|
appropriate default directory/file name
|
60
48
|
(defaults: #{SearchLinks::DEFAULT_YASASHII_FILE}, #{SearchLinks::DEFAULT_FUTSUU_FILE})
|
61
|
-
|
49
|
+
DESC
|
62
50
|
app.check_empty_opt(:out,value)
|
63
|
-
|
51
|
+
}
|
64
52
|
option :r,:results,'number of results per page to request from search',argument: :required,
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
option nil,:'show-count',<<-
|
53
|
+
default: SearchScraper::DEFAULT_RESULT_COUNT,transform: lambda { |value|
|
54
|
+
value = value.to_i
|
55
|
+
value = 1 if value < 1
|
56
|
+
value
|
57
|
+
}
|
58
|
+
option nil,:'show-count',<<-DESC
|
71
59
|
show the number of links scraped and exit;
|
72
60
|
useful for manually writing/updating scripts (but not for use in a variable);
|
73
61
|
implies '--dry-run' option
|
74
|
-
|
75
|
-
option nil,:'show-urls',<<-
|
62
|
+
DESC
|
63
|
+
option nil,:'show-urls',<<-DESC
|
76
64
|
show the URLs -- if any -- used when searching & scraping and exit;
|
77
65
|
you can download these for offline testing and/or slow internet
|
78
66
|
(see '--in' option)
|
79
|
-
|
80
|
-
|
67
|
+
DESC
|
68
|
+
|
81
69
|
run do |opts,args,cmd|
|
82
|
-
opts.each
|
83
|
-
key = key.to_s
|
84
|
-
|
70
|
+
opts.each do |key,value|
|
71
|
+
key = key.to_s
|
72
|
+
|
85
73
|
if key.include?('show')
|
86
74
|
raise CLIError,"must specify a sub command for option[#{key}]"
|
87
75
|
end
|
88
76
|
end
|
89
|
-
|
77
|
+
|
90
78
|
puts cmd.help
|
91
79
|
end
|
92
80
|
end
|
93
|
-
|
94
|
-
@search_easy_cmd = @search_cmd.define_command
|
81
|
+
|
82
|
+
@search_easy_cmd = @search_cmd.define_command do
|
95
83
|
name 'easy'
|
96
84
|
usage 'easy [OPTIONS] [COMMAND]...'
|
97
85
|
aliases :e,:ez
|
98
86
|
summary "Search for NHK News Web Easy (Yasashii) links (aliases: #{app.color_alias('e ez')})"
|
99
|
-
|
100
|
-
description <<-
|
87
|
+
|
88
|
+
description <<-DESC
|
101
89
|
Search for NHK News Web Easy (Yasashii) links &
|
102
90
|
save to file: #{SearchLinks::DEFAULT_YASASHII_FILE}
|
103
|
-
|
104
|
-
|
91
|
+
DESC
|
92
|
+
|
105
93
|
run do |opts,args,cmd|
|
106
94
|
app.refresh_cmd(opts,args,cmd)
|
107
|
-
app.run_search_help
|
95
|
+
app.run_search_help
|
108
96
|
end
|
109
97
|
end
|
110
|
-
|
111
|
-
@search_regular_cmd = @search_cmd.define_command
|
98
|
+
|
99
|
+
@search_regular_cmd = @search_cmd.define_command do
|
112
100
|
name 'regular'
|
113
101
|
usage 'regular [OPTIONS] [COMMAND]...'
|
114
102
|
aliases :r,:reg
|
115
103
|
summary "Search for NHK News Web Regular (Futsuu) links (aliases: #{app.color_alias('r reg')})"
|
116
|
-
|
117
|
-
description <<-
|
104
|
+
|
105
|
+
description <<-DESC
|
118
106
|
Search for NHK News Web Regular (Futsuu) links &
|
119
107
|
save to file: #{SearchLinks::DEFAULT_FUTSUU_FILE}
|
120
|
-
|
121
|
-
|
108
|
+
DESC
|
109
|
+
|
122
110
|
run do |opts,args,cmd|
|
123
111
|
app.refresh_cmd(opts,args,cmd)
|
124
|
-
app.run_search_help
|
112
|
+
app.run_search_help
|
125
113
|
end
|
126
114
|
end
|
127
|
-
|
128
|
-
@search_bing_cmd = Cri::Command.define
|
115
|
+
|
116
|
+
@search_bing_cmd = Cri::Command.define do
|
129
117
|
name 'bing'
|
130
118
|
usage 'bing [OPTIONS] [COMMAND]...'
|
131
119
|
aliases :b
|
132
120
|
summary "Search bing.com for links (aliases: #{app.color_alias('b')})"
|
133
|
-
|
134
|
-
description <<-
|
121
|
+
|
122
|
+
description <<-DESC
|
135
123
|
Search bing.com for links & save to folder: #{SearchLinks::DEFAULT_DIR}
|
136
|
-
|
137
|
-
|
124
|
+
DESC
|
125
|
+
|
138
126
|
run do |opts,args,cmd|
|
139
127
|
app.refresh_cmd(opts,args,cmd)
|
140
|
-
app.run_search_cmd(cmd.supercommand.name.to_sym
|
128
|
+
app.run_search_cmd(cmd.supercommand.name.to_sym,:bing)
|
141
129
|
end
|
142
130
|
end
|
143
|
-
|
131
|
+
|
144
132
|
# dup()/clone() must be called for `cmd.supercommand` to work appropriately.
|
145
|
-
@search_easy_cmd.add_command @search_bing_cmd.dup
|
146
|
-
@search_regular_cmd.add_command @search_bing_cmd.dup
|
133
|
+
@search_easy_cmd.add_command @search_bing_cmd.dup
|
134
|
+
@search_regular_cmd.add_command @search_bing_cmd.dup
|
147
135
|
end
|
148
|
-
|
136
|
+
|
149
137
|
def run_search_cmd(nhk_type,search_type)
|
150
138
|
case nhk_type
|
151
139
|
when :easy
|
@@ -153,130 +141,132 @@ module CLI
|
|
153
141
|
when :regular
|
154
142
|
nhk_type = :futsuu
|
155
143
|
end
|
156
|
-
|
144
|
+
|
157
145
|
return if show_search_urls(search_type)
|
158
|
-
|
146
|
+
|
159
147
|
@cmd_opts[:dry_run] = true if @cmd_opts[:show_count]
|
160
|
-
|
148
|
+
|
161
149
|
build_in_file(:in)
|
162
|
-
|
150
|
+
|
163
151
|
case nhk_type
|
164
152
|
when :futsuu
|
165
|
-
build_out_file(:out,default_dir: SearchLinks::DEFAULT_DIR,
|
153
|
+
build_out_file(:out,default_dir: SearchLinks::DEFAULT_DIR,
|
154
|
+
default_filename: SearchLinks::DEFAULT_FUTSUU_FILENAME)
|
166
155
|
when :yasashii
|
167
|
-
build_out_file(:out,default_dir: SearchLinks::DEFAULT_DIR,
|
156
|
+
build_out_file(:out,default_dir: SearchLinks::DEFAULT_DIR,
|
157
|
+
default_filename: SearchLinks::DEFAULT_YASASHII_FILENAME)
|
168
158
|
else
|
169
159
|
raise ArgumentError,"invalid nhk_type[#{nhk_type}]"
|
170
160
|
end
|
171
|
-
|
161
|
+
|
172
162
|
return unless check_in_file(:in,empty_ok: true)
|
173
163
|
return unless check_out_file(:out)
|
174
|
-
|
164
|
+
|
175
165
|
dry_run = @cmd_opts[:dry_run]
|
176
166
|
in_file = @cmd_opts[:in]
|
177
167
|
out_file = @cmd_opts[:out]
|
178
168
|
result_count = @cmd_opts[:results]
|
179
|
-
result_count = SearchScraper::DEFAULT_RESULT_COUNT if result_count.nil?
|
169
|
+
result_count = SearchScraper::DEFAULT_RESULT_COUNT if result_count.nil?
|
180
170
|
show_count = @cmd_opts[:show_count]
|
181
|
-
|
171
|
+
|
182
172
|
start_spin("Scraping #{search_type}") unless show_count
|
183
|
-
|
184
|
-
is_file = !in_file.nil?
|
173
|
+
|
174
|
+
is_file = !in_file.nil?
|
185
175
|
links = nil
|
186
176
|
new_links = [] # For --dry-run
|
187
|
-
next_page = NextPage.new
|
177
|
+
next_page = NextPage.new
|
188
178
|
page_count = 0
|
189
179
|
page_num = 1
|
190
180
|
url = in_file # nil will use default URL, else a file
|
191
|
-
|
181
|
+
|
192
182
|
# Load previous links for 'scraped?' vars.
|
193
183
|
if File.exist?(out_file)
|
194
184
|
links = SearchLinks.load_file(out_file)
|
195
185
|
else
|
196
|
-
links = SearchLinks.new
|
186
|
+
links = SearchLinks.new
|
197
187
|
end
|
198
|
-
|
188
|
+
|
199
189
|
links_count = links.length
|
200
|
-
|
190
|
+
|
201
191
|
if show_count
|
202
192
|
scraped_count = 0
|
203
|
-
|
204
|
-
links.links.
|
205
|
-
scraped_count += 1 if link.scraped?
|
193
|
+
|
194
|
+
links.links.each_value do |link|
|
195
|
+
scraped_count += 1 if link.scraped?
|
206
196
|
end
|
207
|
-
|
197
|
+
|
208
198
|
puts "#{scraped_count} of #{links_count} links scraped."
|
209
|
-
|
199
|
+
|
210
200
|
return
|
211
201
|
end
|
212
|
-
|
213
|
-
range = (0..
|
214
|
-
|
202
|
+
|
203
|
+
range = (0..10_000) # Do a range to prevent an infinite loop; ichiman!
|
204
|
+
|
215
205
|
case search_type
|
216
206
|
# Anything that extends SearchScraper.
|
217
207
|
when :bing
|
218
|
-
range.each
|
208
|
+
range.each do
|
219
209
|
scraper = nil
|
220
|
-
|
210
|
+
|
221
211
|
case search_type
|
222
212
|
when :bing
|
223
213
|
scraper = BingScraper.new(nhk_type,count: result_count,is_file: is_file,url: url,**@scraper_kargs)
|
224
214
|
else
|
225
215
|
raise NHKore::Error,"internal code broken; add missing search_type[#{search_type}]"
|
226
216
|
end
|
227
|
-
|
217
|
+
|
228
218
|
next_page = scraper.scrape(links,next_page)
|
229
|
-
|
219
|
+
|
230
220
|
new_links.concat(links.links.values[links_count..-1])
|
231
221
|
links_count = links.length
|
232
222
|
page_count = next_page.count if next_page.count > 0
|
233
|
-
|
234
|
-
update_spin_detail(" (page=#{page_num}, count=#{page_count}, links=#{links.length},
|
235
|
-
"new_links=#{new_links.length})")
|
236
|
-
|
237
|
-
break if next_page.empty?
|
238
|
-
|
223
|
+
|
224
|
+
update_spin_detail(" (page=#{page_num}, count=#{page_count}, links=#{links.length}," \
|
225
|
+
" new_links=#{new_links.length})")
|
226
|
+
|
227
|
+
break if next_page.empty?
|
228
|
+
|
239
229
|
page_num += 1
|
240
230
|
url = next_page.url
|
241
|
-
|
242
|
-
sleep_scraper
|
231
|
+
|
232
|
+
sleep_scraper
|
243
233
|
end
|
244
234
|
else
|
245
235
|
raise ArgumentError,"invalid search_type[#{search_type}]"
|
246
236
|
end
|
247
|
-
|
248
|
-
stop_spin
|
237
|
+
|
238
|
+
stop_spin
|
249
239
|
puts
|
250
240
|
puts 'Last URL scraped:'
|
251
241
|
puts "> #{url}"
|
252
242
|
puts
|
253
|
-
|
243
|
+
|
254
244
|
if dry_run
|
255
|
-
new_links.each
|
245
|
+
new_links.each do |link|
|
256
246
|
puts link.to_s(mini: true)
|
257
247
|
end
|
258
248
|
else
|
259
249
|
links.save_file(out_file)
|
260
|
-
|
250
|
+
|
261
251
|
puts 'Saved scraped links to file:'
|
262
252
|
puts "> #{out_file}"
|
263
253
|
end
|
264
254
|
end
|
265
|
-
|
266
|
-
def run_search_help
|
255
|
+
|
256
|
+
def run_search_help
|
267
257
|
if @cmd_opts[:show_count] || @cmd_opts[:show_urls]
|
268
|
-
run_search_cmd(@cmd.name.to_sym
|
258
|
+
run_search_cmd(@cmd.name.to_sym,nil)
|
269
259
|
else
|
270
260
|
puts @cmd.help
|
271
261
|
end
|
272
262
|
end
|
273
|
-
|
263
|
+
|
274
264
|
def show_search_urls(search_type)
|
275
265
|
return false unless @cmd_opts[:show_urls]
|
276
|
-
|
266
|
+
|
277
267
|
count = @cmd_opts[:results]
|
278
|
-
count = SearchScraper::DEFAULT_RESULT_COUNT if count.nil?
|
279
|
-
|
268
|
+
count = SearchScraper::DEFAULT_RESULT_COUNT if count.nil?
|
269
|
+
|
280
270
|
case search_type
|
281
271
|
when :bing
|
282
272
|
puts 'Bing:'
|
@@ -285,7 +275,7 @@ module CLI
|
|
285
275
|
else
|
286
276
|
raise CLIError,'must specify a sub command for option[show-urls]'
|
287
277
|
end
|
288
|
-
|
278
|
+
|
289
279
|
return true
|
290
280
|
end
|
291
281
|
end
|
data/lib/nhkore/cli/sift_cmd.rb
CHANGED
@@ -1,23 +1,11 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
1
|
# encoding: UTF-8
|
3
2
|
# frozen_string_literal: true
|
4
3
|
|
5
4
|
#--
|
6
5
|
# This file is part of NHKore.
|
7
|
-
# Copyright (c) 2020 Jonathan Bradley Whited
|
8
|
-
#
|
9
|
-
#
|
10
|
-
# it under the terms of the GNU Lesser General Public License as published by
|
11
|
-
# the Free Software Foundation, either version 3 of the License, or
|
12
|
-
# (at your option) any later version.
|
13
|
-
#
|
14
|
-
# NHKore is distributed in the hope that it will be useful,
|
15
|
-
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
16
|
-
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
17
|
-
# GNU Lesser General Public License for more details.
|
18
|
-
#
|
19
|
-
# You should have received a copy of the GNU Lesser General Public License
|
20
|
-
# along with NHKore. If not, see <https://www.gnu.org/licenses/>.
|
6
|
+
# Copyright (c) 2020-2021 Jonathan Bradley Whited
|
7
|
+
#
|
8
|
+
# SPDX-License-Identifier: LGPL-3.0-or-later
|
21
9
|
#++
|
22
10
|
|
23
11
|
|
@@ -33,192 +21,193 @@ require 'nhkore/util'
|
|
33
21
|
module NHKore
|
34
22
|
module CLI
|
35
23
|
###
|
36
|
-
# @author Jonathan Bradley Whited
|
24
|
+
# @author Jonathan Bradley Whited
|
37
25
|
# @since 0.2.0
|
38
26
|
###
|
39
27
|
module SiftCmd
|
40
28
|
DEFAULT_SIFT_EXT = :csv
|
41
29
|
DEFAULT_SIFT_FUTSUU_FILE = "#{Sifter::DEFAULT_FUTSUU_FILE}{search.criteria}{file.ext}"
|
42
30
|
DEFAULT_SIFT_YASASHII_FILE = "#{Sifter::DEFAULT_YASASHII_FILE}{search.criteria}{file.ext}"
|
43
|
-
SIFT_EXTS = [
|
44
|
-
|
31
|
+
SIFT_EXTS = %i[csv htm html json yaml yml].freeze
|
32
|
+
|
45
33
|
attr_accessor :sift_datetime_text
|
46
34
|
attr_accessor :sift_search_criteria
|
47
|
-
|
48
|
-
def build_sift_cmd
|
35
|
+
|
36
|
+
def build_sift_cmd
|
49
37
|
app = self
|
50
|
-
|
38
|
+
|
51
39
|
@sift_datetime_text = nil
|
52
40
|
@sift_search_criteria = nil
|
53
|
-
|
54
|
-
@sift_cmd = @app_cmd.define_command
|
41
|
+
|
42
|
+
@sift_cmd = @app_cmd.define_command do
|
55
43
|
name 'sift'
|
56
44
|
usage 'sift [OPTIONS] [COMMAND]...'
|
57
45
|
aliases :s
|
58
|
-
summary
|
59
|
-
|
60
|
-
|
46
|
+
summary 'Sift NHK News Web (Easy) articles data for the frequency of words' \
|
47
|
+
" (aliases: #{app.color_alias('s')})"
|
48
|
+
|
49
|
+
description(<<-DESC)
|
61
50
|
Sift NHK News Web (Easy) articles data for the frequency of words &
|
62
51
|
save to folder: #{Sifter::DEFAULT_DIR}
|
63
|
-
|
64
|
-
|
65
|
-
option :d,:datetime,<<-
|
52
|
+
DESC
|
53
|
+
|
54
|
+
option :d,:datetime,<<-DESC,argument: :required,transform: lambda { |value|
|
66
55
|
date time to filter on; examples:
|
67
56
|
'2020-7-1 13:10...2020-7-31 11:11';
|
68
57
|
'2020-12' (2020, December 1st-31st);
|
69
58
|
'7-4...7-9' (July 4th-9th of Current Year);
|
70
59
|
'7-9' (July 9th of Current Year);
|
71
60
|
'9' (9th of Current Year & Month)
|
72
|
-
|
61
|
+
DESC
|
73
62
|
app.sift_datetime_text = value # Save the original value for the file name
|
74
|
-
|
63
|
+
|
75
64
|
value = DatetimeParser.parse_range(value)
|
76
|
-
|
77
|
-
app.check_empty_opt(:datetime,value) if value.nil?
|
78
|
-
|
65
|
+
|
66
|
+
app.check_empty_opt(:datetime,value) if value.nil?
|
67
|
+
|
79
68
|
value
|
80
|
-
|
81
|
-
option :e,:ext,<<-
|
69
|
+
}
|
70
|
+
option :e,:ext,<<-DESC,argument: :required,default: DEFAULT_SIFT_EXT,transform: lambda { |value|
|
82
71
|
type of file (extension) to save; valid options: [#{SIFT_EXTS.join(', ')}];
|
83
72
|
not needed if you specify a file extension with the '--out' option: '--out sift.html'
|
84
|
-
|
85
|
-
value = Util.unspace_web_str(value).downcase
|
86
|
-
|
73
|
+
DESC
|
74
|
+
value = Util.unspace_web_str(value).downcase.to_sym
|
75
|
+
|
87
76
|
raise CLIError,"invalid ext[#{value}] for option[#{ext}]" unless SIFT_EXTS.include?(value)
|
88
|
-
|
77
|
+
|
89
78
|
value
|
90
|
-
|
91
|
-
option :i,:in,<<-
|
79
|
+
}
|
80
|
+
option :i,:in,<<-DESC,argument: :required,transform: lambda { |value|
|
92
81
|
file of NHK News Web (Easy) articles data to sift (see '#{App::NAME} news';
|
93
82
|
defaults: #{YasashiiNews::DEFAULT_FILE}, #{FutsuuNews::DEFAULT_FILE})
|
94
|
-
|
83
|
+
DESC
|
95
84
|
app.check_empty_opt(:in,value)
|
96
|
-
|
85
|
+
}
|
97
86
|
flag :D,:'no-defn','do not output the definitions for words (which can be quite long)'
|
98
87
|
flag :E,:'no-eng','do not output the English translations for words'
|
99
|
-
option :o,:out,<<-
|
88
|
+
option :o,:out,<<-DESC,argument: :required,transform: lambda { |value|
|
100
89
|
'directory/file' to save sifted data to; if you only specify a directory or a file, it will attach
|
101
90
|
the appropriate default directory/file name
|
102
91
|
(defaults: #{DEFAULT_SIFT_YASASHII_FILE}, #{DEFAULT_SIFT_FUTSUU_FILE})
|
103
|
-
|
92
|
+
DESC
|
104
93
|
app.check_empty_opt(:out,value)
|
105
|
-
|
106
|
-
flag :H,'no-sha256',<<-
|
94
|
+
}
|
95
|
+
flag :H,'no-sha256',<<-DESC
|
107
96
|
if you used this option with the 'news' command, then you'll also need this option here
|
108
97
|
to not fail on "duplicate" articles; see '#{App::NAME} news'
|
109
|
-
|
98
|
+
DESC
|
110
99
|
option :t,:title,'title to filter on, where search text only needs to be somewhere in the title',
|
111
100
|
argument: :required
|
112
101
|
option :u,:url,'URL to filter on, where search text only needs to be somewhere in the URL',
|
113
102
|
argument: :required
|
114
|
-
|
103
|
+
|
115
104
|
run do |opts,args,cmd|
|
116
105
|
puts cmd.help
|
117
106
|
end
|
118
107
|
end
|
119
|
-
|
120
|
-
@sift_easy_cmd = @sift_cmd.define_command
|
108
|
+
|
109
|
+
@sift_easy_cmd = @sift_cmd.define_command do
|
121
110
|
name 'easy'
|
122
111
|
usage 'easy [OPTIONS] [COMMAND]...'
|
123
112
|
aliases :e,:ez
|
124
113
|
summary "Sift NHK News Web Easy (Yasashii) articles data (aliases: #{app.color_alias('e ez')})"
|
125
|
-
|
126
|
-
description <<-
|
114
|
+
|
115
|
+
description <<-DESC
|
127
116
|
Sift NHK News Web Easy (Yasashii) articles data for the frequency of words &
|
128
117
|
save to file: #{DEFAULT_SIFT_YASASHII_FILE}
|
129
|
-
|
130
|
-
|
118
|
+
DESC
|
119
|
+
|
131
120
|
run do |opts,args,cmd|
|
132
121
|
app.refresh_cmd(opts,args,cmd)
|
133
122
|
app.run_sift_cmd(:yasashii)
|
134
123
|
end
|
135
124
|
end
|
136
|
-
|
137
|
-
@sift_regular_cmd = @sift_cmd.define_command
|
125
|
+
|
126
|
+
@sift_regular_cmd = @sift_cmd.define_command do
|
138
127
|
name 'regular'
|
139
128
|
usage 'regular [OPTIONS] [COMMAND]...'
|
140
129
|
aliases :r,:reg
|
141
130
|
summary "Sift NHK News Web Regular (Futsuu) articles data (aliases: #{app.color_alias('r reg')})"
|
142
|
-
|
143
|
-
description
|
131
|
+
|
132
|
+
description(<<-DESC)
|
144
133
|
Sift NHK News Web Regular (Futsuu) articles data for the frequency of words &
|
145
134
|
save to file: #{DEFAULT_SIFT_FUTSUU_FILE}
|
146
|
-
|
147
|
-
|
135
|
+
DESC
|
136
|
+
|
148
137
|
run do |opts,args,cmd|
|
149
138
|
app.refresh_cmd(opts,args,cmd)
|
150
139
|
app.run_sift_cmd(:futsuu)
|
151
140
|
end
|
152
141
|
end
|
153
142
|
end
|
154
|
-
|
143
|
+
|
155
144
|
def build_sift_filename(filename)
|
156
145
|
@sift_search_criteria = []
|
157
|
-
|
158
|
-
@sift_search_criteria << Util.strip_web_str(@sift_datetime_text.to_s
|
159
|
-
@sift_search_criteria << Util.strip_web_str(@cmd_opts[:title].to_s
|
160
|
-
@sift_search_criteria << Util.strip_web_str(@cmd_opts[:url].to_s
|
161
|
-
@sift_search_criteria.filter!
|
162
|
-
|
163
|
-
clean_regex = /[^[[:alnum:]]\-_
|
164
|
-
clean_search_criteria = ''.dup
|
165
|
-
|
166
|
-
@sift_search_criteria.each
|
146
|
+
|
147
|
+
@sift_search_criteria << Util.strip_web_str(@sift_datetime_text.to_s)
|
148
|
+
@sift_search_criteria << Util.strip_web_str(@cmd_opts[:title].to_s)
|
149
|
+
@sift_search_criteria << Util.strip_web_str(@cmd_opts[:url].to_s)
|
150
|
+
@sift_search_criteria.filter! { |sc| !sc.empty? }
|
151
|
+
|
152
|
+
clean_regex = /[^[[:alnum:]]\-_.]+/
|
153
|
+
clean_search_criteria = ''.dup
|
154
|
+
|
155
|
+
@sift_search_criteria.each do |sc|
|
167
156
|
clean_search_criteria << sc.gsub(clean_regex,'')
|
168
157
|
end
|
169
|
-
|
170
|
-
@sift_search_criteria = @sift_search_criteria.empty?
|
171
|
-
|
158
|
+
|
159
|
+
@sift_search_criteria = @sift_search_criteria.empty? ? nil : @sift_search_criteria.join(', ')
|
160
|
+
|
172
161
|
# Limit the file name length.
|
173
162
|
# If length is smaller, [..] still works appropriately.
|
174
163
|
clean_search_criteria = clean_search_criteria[0..32]
|
175
|
-
|
176
|
-
clean_search_criteria.prepend('_') unless clean_search_criteria.empty?
|
177
|
-
|
164
|
+
|
165
|
+
clean_search_criteria.prepend('_') unless clean_search_criteria.empty?
|
166
|
+
|
178
167
|
file_ext = @cmd_opts[:ext]
|
179
|
-
|
180
|
-
if file_ext.nil?
|
168
|
+
|
169
|
+
if file_ext.nil?
|
181
170
|
# Try to get from '--out' if it exists.
|
182
|
-
if !@cmd_opts[:out].nil?
|
183
|
-
file_ext = Util.unspace_web_str(File.extname(@cmd_opts[:out])).downcase
|
171
|
+
if !@cmd_opts[:out].nil?
|
172
|
+
file_ext = Util.unspace_web_str(File.extname(@cmd_opts[:out])).downcase
|
184
173
|
file_ext = file_ext.sub(/\A\./,'') # Remove '.'; can't be nil for to_sym()
|
185
|
-
file_ext = file_ext.to_sym
|
186
|
-
|
174
|
+
file_ext = file_ext.to_sym
|
175
|
+
|
187
176
|
file_ext = nil unless SIFT_EXTS.include?(file_ext)
|
188
177
|
end
|
189
|
-
|
190
|
-
file_ext = DEFAULT_SIFT_EXT if file_ext.nil?
|
178
|
+
|
179
|
+
file_ext = DEFAULT_SIFT_EXT if file_ext.nil?
|
191
180
|
@cmd_opts[:ext] = file_ext
|
192
181
|
end
|
193
|
-
|
182
|
+
|
194
183
|
filename = "#{filename}#{clean_search_criteria}.#{file_ext}"
|
195
|
-
|
184
|
+
|
196
185
|
return filename
|
197
186
|
end
|
198
|
-
|
187
|
+
|
199
188
|
def run_sift_cmd(type)
|
200
189
|
news_name = nil
|
201
|
-
|
190
|
+
|
202
191
|
case type
|
203
192
|
when :futsuu
|
204
193
|
build_in_file(:in,default_dir: News::DEFAULT_DIR,default_filename: FutsuuNews::DEFAULT_FILENAME)
|
205
194
|
build_out_file(:out,default_dir: Sifter::DEFAULT_DIR,
|
206
195
|
default_filename: build_sift_filename(Sifter::DEFAULT_FUTSUU_FILENAME))
|
207
|
-
|
196
|
+
|
208
197
|
news_name = 'Regular'
|
209
198
|
when :yasashii
|
210
199
|
build_in_file(:in,default_dir: News::DEFAULT_DIR,default_filename: YasashiiNews::DEFAULT_FILENAME)
|
211
200
|
build_out_file(:out,default_dir: Sifter::DEFAULT_DIR,
|
212
201
|
default_filename: build_sift_filename(Sifter::DEFAULT_YASASHII_FILENAME))
|
213
|
-
|
202
|
+
|
214
203
|
news_name = 'Easy'
|
215
204
|
else
|
216
205
|
raise ArgumentError,"invalid type[#{type}]"
|
217
206
|
end
|
218
|
-
|
207
|
+
|
219
208
|
return unless check_in_file(:in,empty_ok: false)
|
220
209
|
return unless check_out_file(:out)
|
221
|
-
|
210
|
+
|
222
211
|
datetime_filter = @cmd_opts[:datetime]
|
223
212
|
dry_run = @cmd_opts[:dry_run]
|
224
213
|
file_ext = @cmd_opts[:ext]
|
@@ -229,55 +218,55 @@ module CLI
|
|
229
218
|
out_file = @cmd_opts[:out]
|
230
219
|
title_filter = @cmd_opts[:title]
|
231
220
|
url_filter = @cmd_opts[:url]
|
232
|
-
|
221
|
+
|
233
222
|
start_spin("Sifting NHK News Web #{news_name} data")
|
234
|
-
|
223
|
+
|
235
224
|
news = (type == :yasashii) ?
|
236
225
|
YasashiiNews.load_file(in_file,overwrite: no_sha256) :
|
237
226
|
FutsuuNews.load_file(in_file,overwrite: no_sha256)
|
238
|
-
|
227
|
+
|
239
228
|
sifter = Sifter.new(news)
|
240
|
-
|
241
|
-
sifter.filter_by_datetime(datetime_filter) unless datetime_filter.nil?
|
242
|
-
sifter.filter_by_title(title_filter) unless title_filter.nil?
|
243
|
-
sifter.filter_by_url(url_filter) unless url_filter.nil?
|
229
|
+
|
230
|
+
sifter.filter_by_datetime(datetime_filter) unless datetime_filter.nil?
|
231
|
+
sifter.filter_by_title(title_filter) unless title_filter.nil?
|
232
|
+
sifter.filter_by_url(url_filter) unless url_filter.nil?
|
244
233
|
sifter.ignore(:defn) if no_defn
|
245
234
|
sifter.ignore(:eng) if no_eng
|
246
|
-
|
247
|
-
sifter.caption = "NHK News Web #{news_name}".dup
|
248
|
-
|
249
|
-
if !@sift_search_criteria.nil?
|
250
|
-
if [
|
251
|
-
sifter.caption << " — #{Util.escape_html(@sift_search_criteria.to_s
|
235
|
+
|
236
|
+
sifter.caption = "NHK News Web #{news_name}".dup
|
237
|
+
|
238
|
+
if !@sift_search_criteria.nil?
|
239
|
+
if %i[htm html].any?(file_ext)
|
240
|
+
sifter.caption << " — #{Util.escape_html(@sift_search_criteria.to_s)}"
|
252
241
|
else
|
253
242
|
sifter.caption << " -- #{@sift_search_criteria}"
|
254
243
|
end
|
255
244
|
end
|
256
|
-
|
245
|
+
|
257
246
|
case file_ext
|
258
247
|
when :csv
|
259
|
-
sifter.put_csv!
|
248
|
+
sifter.put_csv!
|
260
249
|
when :htm,:html
|
261
|
-
sifter.put_html!
|
250
|
+
sifter.put_html!
|
262
251
|
when :json
|
263
|
-
sifter.put_json!
|
252
|
+
sifter.put_json!
|
264
253
|
when :yaml,:yml
|
265
|
-
sifter.put_yaml!
|
254
|
+
sifter.put_yaml!
|
266
255
|
else
|
267
256
|
raise ArgumentError,"invalid file ext[#{file_ext}]"
|
268
257
|
end
|
269
|
-
|
270
|
-
stop_spin
|
258
|
+
|
259
|
+
stop_spin
|
271
260
|
puts
|
272
|
-
|
261
|
+
|
273
262
|
if dry_run
|
274
|
-
puts sifter.to_s
|
263
|
+
puts sifter.to_s
|
275
264
|
else
|
276
265
|
start_spin('Saving sifted data to file')
|
277
|
-
|
266
|
+
|
278
267
|
sifter.save_file(out_file)
|
279
|
-
|
280
|
-
stop_spin
|
268
|
+
|
269
|
+
stop_spin
|
281
270
|
puts "> #{out_file}"
|
282
271
|
end
|
283
272
|
end
|