nhkore 0.3.7 → 0.3.11
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +53 -2
- data/Gemfile +0 -18
- data/Gemfile.lock +36 -33
- data/README.md +36 -30
- data/Rakefile +38 -52
- data/bin/nhkore +4 -15
- data/lib/nhkore/app.rb +235 -234
- data/lib/nhkore/article.rb +39 -53
- data/lib/nhkore/article_scraper.rb +293 -285
- 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 +88 -104
- 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 +74 -83
- data/lib/nhkore/search_link.rb +62 -76
- data/lib/nhkore/search_scraper.rb +81 -92
- 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 +99 -97
- data/lib/nhkore.rb +8 -20
- data/nhkore.gemspec +30 -51
- data/samples/looper.rb +18 -29
- data/test/nhkore/test_helper.rb +3 -15
- data/test/nhkore_test.rb +6 -18
- metadata +33 -24
@@ -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,12 +18,12 @@ require 'nhkore/util'
|
|
30
18
|
|
31
19
|
module NHKore
|
32
20
|
###
|
33
|
-
# @author Jonathan Bradley Whited
|
21
|
+
# @author Jonathan Bradley Whited
|
34
22
|
# @since 0.3.4
|
35
23
|
###
|
36
24
|
class DatetimeParser
|
37
25
|
extend AttrBool::Ext
|
38
|
-
|
26
|
+
|
39
27
|
# Order matters!
|
40
28
|
FMTS = [
|
41
29
|
'%Y-%m-%d %H:%M',
|
@@ -54,13 +42,13 @@ module NHKore
|
|
54
42
|
'%H:%M',
|
55
43
|
'%d',
|
56
44
|
'%Y',
|
57
|
-
]
|
58
|
-
|
45
|
+
].freeze
|
46
|
+
|
59
47
|
def self.guess_year(year)
|
60
48
|
if year < 1000
|
61
49
|
century = Util::JST_YEAR / 100 * 100 # 2120 -> 2100
|
62
50
|
millennium = Util::JST_YEAR / 1000 * 1000 # 2120 -> 2000
|
63
|
-
|
51
|
+
|
64
52
|
# If year <= 23 (2022 -> 23)...
|
65
53
|
if year <= ((Util::JST_YEAR % 100) + 1)
|
66
54
|
# Assume this century.
|
@@ -71,34 +59,34 @@ module NHKore
|
|
71
59
|
# Assume this millennium.
|
72
60
|
# So if the current year is 2200, and year is 150,
|
73
61
|
# then it will be 2000 + 150 = 2150.
|
74
|
-
year = millennium + year
|
75
62
|
else
|
76
63
|
# Assume previous millennium (2000 -> 1000),
|
77
64
|
# so year 999 will become 1999.
|
78
65
|
millennium -= 1000 if millennium >= 1000
|
79
|
-
year = millennium + year
|
80
66
|
end
|
67
|
+
|
68
|
+
year = millennium + year
|
81
69
|
else
|
82
70
|
# Assume previous century (2000 -> 1900).
|
83
71
|
century -= 100 if century >= 100
|
84
72
|
year = century + year
|
85
73
|
end
|
86
74
|
end
|
87
|
-
|
75
|
+
|
88
76
|
return year
|
89
77
|
end
|
90
|
-
|
78
|
+
|
91
79
|
def self.parse_range(value)
|
92
80
|
# Do not use unspace_web_str(), want spaces for formats.
|
93
81
|
value = Util.strip_web_str(Util.reduce_space(value))
|
94
82
|
values = value.split('...',2)
|
95
|
-
|
96
|
-
return nil if values.empty?
|
97
|
-
|
83
|
+
|
84
|
+
return nil if values.empty? # For '' or '...'
|
85
|
+
|
98
86
|
# For '2020...' or '...2020'.
|
99
87
|
if value.include?('...')
|
100
88
|
# values.length is always 2 because of 2 in split() above.
|
101
|
-
|
89
|
+
|
102
90
|
# For '2020...'.
|
103
91
|
if Util.empty_web_str?(values[1])
|
104
92
|
values[1] = :infinity
|
@@ -107,81 +95,79 @@ module NHKore
|
|
107
95
|
values[0] = :infinity
|
108
96
|
end
|
109
97
|
end
|
110
|
-
|
98
|
+
|
111
99
|
datetimes = [
|
112
|
-
DatetimeParser.new
|
113
|
-
DatetimeParser.new
|
100
|
+
DatetimeParser.new, # "From" date time
|
101
|
+
DatetimeParser.new, # "To" date time
|
114
102
|
]
|
115
|
-
|
116
|
-
values.each_with_index
|
103
|
+
|
104
|
+
values.each_with_index do |v,i|
|
117
105
|
dt = datetimes[i]
|
118
|
-
|
106
|
+
|
119
107
|
# Minimum/Maximum date time for '2020...' or '...2020'.
|
120
108
|
if v == :infinity
|
121
109
|
# "From" date time.
|
122
110
|
if i == 0
|
123
|
-
dt.min!
|
111
|
+
dt.min!
|
124
112
|
# "To" date time.
|
125
113
|
else
|
126
|
-
dt.max!
|
114
|
+
dt.max!
|
127
115
|
end
|
128
116
|
else
|
129
117
|
v = Util.strip_web_str(v)
|
130
|
-
|
131
|
-
FMTS.each_with_index
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
raise if i >= (FMTS.length - 1)
|
146
|
-
end
|
118
|
+
|
119
|
+
FMTS.each_with_index do |fmt,j|
|
120
|
+
# If don't do this, "%d" values will be parsed using "%d %H".
|
121
|
+
# It seems as though strptime() ignores space.
|
122
|
+
raise ArgumentError if fmt.include?(' ') && !v.include?(' ')
|
123
|
+
|
124
|
+
# If don't do this, "%y..." values will be parsed using "%d...".
|
125
|
+
raise ArgumentError if fmt.start_with?('%d') && v.split(' ')[0].length > 2
|
126
|
+
|
127
|
+
dt.parse!(v,fmt)
|
128
|
+
|
129
|
+
break # No problem; this format worked
|
130
|
+
rescue ArgumentError
|
131
|
+
# Out of formats.
|
132
|
+
raise if j >= (FMTS.length - 1)
|
147
133
|
end
|
148
134
|
end
|
149
135
|
end
|
150
|
-
|
136
|
+
|
151
137
|
from = datetimes[0]
|
152
138
|
to = datetimes[1]
|
153
|
-
|
139
|
+
|
154
140
|
from.autofill!(:from,to)
|
155
141
|
to.autofill!(:to,from)
|
156
|
-
|
157
|
-
return [from.jst_time
|
142
|
+
|
143
|
+
return [from.jst_time,to.jst_time]
|
158
144
|
end
|
159
|
-
|
145
|
+
|
160
146
|
attr_accessor :day
|
161
147
|
attr_accessor :hour
|
162
148
|
attr_accessor :min
|
163
149
|
attr_accessor :month
|
164
150
|
attr_accessor :sec
|
165
151
|
attr_accessor :year
|
166
|
-
|
152
|
+
|
167
153
|
attr_accessor? :has_day
|
168
154
|
attr_accessor? :has_hour
|
169
155
|
attr_accessor? :has_min
|
170
156
|
attr_accessor? :has_month
|
171
157
|
attr_accessor? :has_sec
|
172
158
|
attr_accessor? :has_year
|
173
|
-
|
159
|
+
|
174
160
|
attr_reader? :min_or_max
|
175
|
-
|
161
|
+
|
176
162
|
def initialize(year=nil,month=nil,day=nil,hour=nil,min=nil,sec=nil)
|
177
163
|
super()
|
178
|
-
|
164
|
+
|
179
165
|
set!(year,month,day,hour,min,sec)
|
180
|
-
|
166
|
+
|
181
167
|
self.has = false
|
182
168
|
@min_or_max = false
|
183
169
|
end
|
184
|
-
|
170
|
+
|
185
171
|
def autofill!(type,other)
|
186
172
|
case type
|
187
173
|
when :from
|
@@ -191,15 +177,15 @@ module NHKore
|
|
191
177
|
else
|
192
178
|
raise ArgumentError,"invalid type[#{type}]"
|
193
179
|
end
|
194
|
-
|
180
|
+
|
195
181
|
return self if @min_or_max
|
196
|
-
|
182
|
+
|
197
183
|
has_small = false
|
198
184
|
jst_now = Util.jst_now()
|
199
|
-
|
185
|
+
|
200
186
|
# Must be from smallest to biggest.
|
201
|
-
|
202
|
-
if @has_sec || other.has_sec?
|
187
|
+
|
188
|
+
if @has_sec || other.has_sec?
|
203
189
|
@sec = other.sec unless @has_sec
|
204
190
|
has_small = true
|
205
191
|
else
|
@@ -209,8 +195,8 @@ module NHKore
|
|
209
195
|
@sec = is_from ? 0 : 59
|
210
196
|
end
|
211
197
|
end
|
212
|
-
|
213
|
-
if @has_min || other.has_min?
|
198
|
+
|
199
|
+
if @has_min || other.has_min?
|
214
200
|
@min = other.min unless @has_min
|
215
201
|
has_small = true
|
216
202
|
else
|
@@ -220,8 +206,8 @@ module NHKore
|
|
220
206
|
@min = is_from ? 0 : 59
|
221
207
|
end
|
222
208
|
end
|
223
|
-
|
224
|
-
if @has_hour || other.has_hour?
|
209
|
+
|
210
|
+
if @has_hour || other.has_hour?
|
225
211
|
@hour = other.hour unless @has_hour
|
226
212
|
has_small = true
|
227
213
|
else
|
@@ -231,8 +217,8 @@ module NHKore
|
|
231
217
|
@hour = is_from ? 0 : 23
|
232
218
|
end
|
233
219
|
end
|
234
|
-
|
235
|
-
if @has_day || other.has_day?
|
220
|
+
|
221
|
+
if @has_day || other.has_day?
|
236
222
|
@day = other.day unless @has_day
|
237
223
|
has_small = true
|
238
224
|
else
|
@@ -242,8 +228,8 @@ module NHKore
|
|
242
228
|
@day = is_from ? 1 : :last_day
|
243
229
|
end
|
244
230
|
end
|
245
|
-
|
246
|
-
if @has_month || other.has_month?
|
231
|
+
|
232
|
+
if @has_month || other.has_month?
|
247
233
|
@month = other.month unless @has_month
|
248
234
|
has_small = true
|
249
235
|
else
|
@@ -253,10 +239,10 @@ module NHKore
|
|
253
239
|
@month = is_from ? 1 : 12
|
254
240
|
end
|
255
241
|
end
|
256
|
-
|
257
|
-
if @has_year || other.has_year?
|
242
|
+
|
243
|
+
if @has_year || other.has_year?
|
258
244
|
@year = other.year unless @has_year
|
259
|
-
has_small = true
|
245
|
+
has_small = true # rubocop:disable Lint/UselessAssignment
|
260
246
|
else
|
261
247
|
if has_small
|
262
248
|
@year = jst_now.year
|
@@ -264,49 +250,49 @@ module NHKore
|
|
264
250
|
@year = is_from ? Util::MIN_SANE_YEAR : jst_now.year
|
265
251
|
end
|
266
252
|
end
|
267
|
-
|
253
|
+
|
268
254
|
# Must be after setting @year & @month.
|
269
255
|
if @day == :last_day
|
270
256
|
@day = Date.new(@year,@month,-1).day
|
271
257
|
end
|
272
|
-
|
258
|
+
|
273
259
|
return self
|
274
260
|
end
|
275
|
-
|
276
|
-
def max!
|
261
|
+
|
262
|
+
def max!
|
277
263
|
@min_or_max = true
|
278
|
-
|
264
|
+
|
279
265
|
# Ex: 2020-12-31 23:59:59
|
280
266
|
return set!(Util::JST_YEAR,12,31,23,59,59)
|
281
267
|
end
|
282
|
-
|
283
|
-
def min!
|
268
|
+
|
269
|
+
def min!
|
284
270
|
@min_or_max = true
|
285
|
-
|
271
|
+
|
286
272
|
# Ex: 1924-01-01 00:00:00
|
287
273
|
return set!(Util::MIN_SANE_YEAR,1,1,0,0,0)
|
288
274
|
end
|
289
|
-
|
275
|
+
|
290
276
|
def parse!(value,fmt)
|
291
277
|
value = Time.strptime(value,fmt,&self.class.method(:guess_year))
|
292
|
-
|
278
|
+
|
293
279
|
@has_day = fmt.include?('%d')
|
294
280
|
@has_hour = fmt.include?('%H')
|
295
281
|
@has_min = fmt.include?('%M')
|
296
282
|
@has_month = fmt.include?('%m')
|
297
283
|
@has_sec = fmt.include?('%S')
|
298
284
|
@has_year = fmt.include?('%Y')
|
299
|
-
|
285
|
+
|
300
286
|
@day = value.day if @has_day
|
301
287
|
@hour = value.hour if @has_hour
|
302
288
|
@min = value.min if @has_min
|
303
289
|
@month = value.month if @has_month
|
304
290
|
@sec = value.sec if @has_sec
|
305
291
|
@year = value.year if @has_year
|
306
|
-
|
292
|
+
|
307
293
|
return self
|
308
294
|
end
|
309
|
-
|
295
|
+
|
310
296
|
def set!(year=nil,month=nil,day=nil,hour=nil,min=nil,sec=nil)
|
311
297
|
@year = year
|
312
298
|
@month = month
|
@@ -314,10 +300,10 @@ module NHKore
|
|
314
300
|
@hour = hour
|
315
301
|
@min = min
|
316
302
|
@sec = sec
|
317
|
-
|
303
|
+
|
318
304
|
return self
|
319
305
|
end
|
320
|
-
|
306
|
+
|
321
307
|
def has=(value)
|
322
308
|
@has_day = value
|
323
309
|
@has_hour = value
|
@@ -325,19 +311,17 @@ module NHKore
|
|
325
311
|
@has_month = value
|
326
312
|
@has_sec = value
|
327
313
|
@has_year = value
|
328
|
-
|
329
|
-
return self
|
330
314
|
end
|
331
|
-
|
332
|
-
def jst_time
|
333
|
-
return Util.jst_time(time
|
315
|
+
|
316
|
+
def jst_time
|
317
|
+
return Util.jst_time(time)
|
334
318
|
end
|
335
|
-
|
336
|
-
def time
|
319
|
+
|
320
|
+
def time
|
337
321
|
return Time.new(@year,@month,@day,@hour,@min,@sec)
|
338
322
|
end
|
339
|
-
|
340
|
-
def to_s
|
323
|
+
|
324
|
+
def to_s
|
341
325
|
return "#{@year}-#{@month}-#{@day} #{@hour}:#{@min}:#{@sec}"
|
342
326
|
end
|
343
327
|
end
|
data/lib/nhkore/defn.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
|
|
@@ -29,75 +17,80 @@ require 'nhkore/word'
|
|
29
17
|
|
30
18
|
module NHKore
|
31
19
|
###
|
32
|
-
# @author Jonathan Bradley Whited
|
20
|
+
# @author Jonathan Bradley Whited
|
33
21
|
# @since 0.2.0
|
34
22
|
###
|
35
23
|
class Defn
|
36
24
|
attr_reader :hyoukis
|
37
25
|
attr_accessor :text
|
38
26
|
attr_reader :words
|
39
|
-
|
40
|
-
def initialize
|
27
|
+
|
28
|
+
def initialize
|
41
29
|
super()
|
42
|
-
|
30
|
+
|
43
31
|
@hyoukis = []
|
44
|
-
@text = ''.dup
|
32
|
+
@text = ''.dup
|
45
33
|
@words = []
|
46
34
|
end
|
47
|
-
|
35
|
+
|
48
36
|
# If no data, don't raise errors; don't care if have a definition or not.
|
49
37
|
def self.scrape(hash,missingno: nil,url: nil)
|
50
|
-
defn = Defn.new
|
51
|
-
|
38
|
+
defn = Defn.new
|
39
|
+
|
52
40
|
hyoukis = hash['hyouki']
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
defn.hyoukis << hyouki
|
60
|
-
end
|
41
|
+
|
42
|
+
hyoukis&.each() do |hyouki|
|
43
|
+
next if hyouki.nil?
|
44
|
+
next if (hyouki = Util.strip_web_str(hyouki)).empty?
|
45
|
+
|
46
|
+
defn.hyoukis << hyouki
|
61
47
|
end
|
62
|
-
|
48
|
+
|
63
49
|
def_str = hash['def']
|
64
|
-
|
50
|
+
|
65
51
|
if Util.empty_web_str?(def_str)
|
66
|
-
return defn.hyoukis.empty?
|
52
|
+
return defn.hyoukis.empty? ? nil : defn
|
67
53
|
end
|
68
|
-
|
54
|
+
|
69
55
|
doc = Nokogiri::HTML(def_str)
|
70
|
-
doc = doc.css('body') # Auto-added by Nokogiri
|
71
|
-
|
72
|
-
doc.children.each
|
73
|
-
name = Util.unspace_web_str(child.name).downcase
|
74
|
-
|
56
|
+
doc = doc.css('body') # Auto-added by Nokogiri.
|
57
|
+
|
58
|
+
doc.children.each do |child|
|
59
|
+
name = Util.unspace_web_str(child.name).downcase if child.respond_to?(:name)
|
60
|
+
|
75
61
|
is_text = false
|
76
|
-
|
77
|
-
|
62
|
+
words = []
|
63
|
+
|
78
64
|
if name == 'ruby'
|
79
|
-
|
80
|
-
|
81
|
-
|
65
|
+
# Returns an array.
|
66
|
+
words = Word.scrape_ruby_tag(child,missingno: missingno,url: url)
|
67
|
+
elsif child.respond_to?(:text) # Don't do child.text?(), just want content.
|
68
|
+
words << Word.scrape_text_node(child,url: url)
|
82
69
|
is_text = true
|
83
70
|
end
|
84
|
-
|
85
|
-
|
71
|
+
|
72
|
+
# All word-scraping methods can return nil,
|
73
|
+
# so remove all nils for empty?() check.
|
74
|
+
words = words&.compact
|
75
|
+
|
76
|
+
if words.nil? || words.empty?
|
86
77
|
defn.text << Util.reduce_jpn_space(child.text) if is_text
|
87
78
|
else
|
88
|
-
|
89
|
-
|
79
|
+
words.each do |word|
|
80
|
+
defn.text << Util.reduce_jpn_space(word.word)
|
81
|
+
defn.words << word unless Util.empty_web_str?(word.word)
|
82
|
+
end
|
90
83
|
end
|
91
84
|
end
|
92
|
-
|
93
|
-
return nil if defn.hyoukis.empty?
|
94
|
-
|
85
|
+
|
86
|
+
return nil if defn.hyoukis.empty? && defn.words.empty?
|
87
|
+
|
95
88
|
defn.text = Util.strip_web_str(defn.text)
|
96
|
-
|
89
|
+
|
97
90
|
return defn
|
98
91
|
end
|
99
|
-
|
100
|
-
def to_s
|
92
|
+
|
93
|
+
def to_s
|
101
94
|
return @text
|
102
95
|
end
|
103
96
|
end
|
data/lib/nhkore/dict.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
|
|
@@ -27,53 +15,53 @@ require 'nhkore/error'
|
|
27
15
|
|
28
16
|
module NHKore
|
29
17
|
###
|
30
|
-
# @author Jonathan Bradley Whited
|
18
|
+
# @author Jonathan Bradley Whited
|
31
19
|
# @since 0.2.0
|
32
20
|
###
|
33
21
|
class Dict
|
34
22
|
attr_reader :entries
|
35
|
-
|
36
|
-
def initialize
|
23
|
+
|
24
|
+
def initialize
|
37
25
|
super()
|
38
|
-
|
26
|
+
|
39
27
|
@entries = {}
|
40
28
|
end
|
41
|
-
|
29
|
+
|
42
30
|
def [](id)
|
43
31
|
return @entries[id]
|
44
32
|
end
|
45
|
-
|
33
|
+
|
46
34
|
def []=(id,entry)
|
47
|
-
|
35
|
+
@entries[id] = entry
|
48
36
|
end
|
49
|
-
|
37
|
+
|
50
38
|
def self.scrape(hash,missingno: nil,url: nil)
|
51
|
-
dict = Dict.new
|
52
|
-
|
53
|
-
hash.each
|
39
|
+
dict = Dict.new
|
40
|
+
|
41
|
+
hash.each do |id,array|
|
54
42
|
entry = Entry.scrape(id,array,missingno: missingno,url: url)
|
55
|
-
|
56
|
-
next if entry.nil?
|
43
|
+
|
44
|
+
next if entry.nil?
|
57
45
|
raise ScrapeError,"duplicate ID[#{id}] at URL[#{url}] in hash[#{hash}]" if dict.key?(id)
|
58
|
-
|
46
|
+
|
59
47
|
dict[id] = entry
|
60
48
|
end
|
61
|
-
|
49
|
+
|
62
50
|
return dict
|
63
51
|
end
|
64
|
-
|
52
|
+
|
65
53
|
def key?(id)
|
66
54
|
return @entries.key?(id)
|
67
55
|
end
|
68
|
-
|
69
|
-
def to_s
|
70
|
-
s = ''.dup
|
71
|
-
|
72
|
-
@entries.each
|
56
|
+
|
57
|
+
def to_s
|
58
|
+
s = ''.dup
|
59
|
+
|
60
|
+
@entries.each do |id,entry|
|
73
61
|
s << "#{id}:\n"
|
74
|
-
s << " #{entry.to_s
|
62
|
+
s << " #{entry.to_s.gsub("\n","\n ").rstrip}\n"
|
75
63
|
end
|
76
|
-
|
64
|
+
|
77
65
|
return s
|
78
66
|
end
|
79
67
|
end
|