lothianbusestimetable 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c4f7f7c0e1ed96b68f78ef7f9bddf81375b8ecd2
4
- data.tar.gz: a6e0bdb8b9ca8f2c0599033de6d07c8937afa21b
3
+ metadata.gz: f073bd2a668161f0a017b6ab9cb91050de66f68c
4
+ data.tar.gz: a796eef94ca37403bac667f6e50497ccde111cb7
5
5
  SHA512:
6
- metadata.gz: 942477a77c162efcca923b19bcd46d24c71ac60a9a5ba1309aafdae6b8b1c98dbbd66f2c2aa0caeb5ab02fcac30f079f9cffd23cdcde5e0de8ce674a7c7efb12
7
- data.tar.gz: 1f530cbb5f9f553557794c7ee82a7adb1b37ed688e86c17298b20855524e9100515fad05ff8aeb3052036e4cb01486e4053e74ad8b6202d06290836e306d7230
6
+ metadata.gz: 759f4db26565fa052b63dd6e7088fd39b78846f090b2aea963c447c7228d1e4b155ef4a43eace888f6238f65c303fcbd1a629bbfb9f26b0ffdcaae92b0cf1bb2
7
+ data.tar.gz: ae2d87641c348d0493721a92ee5dca77d065ab6282d1ebcd60d9285192e1328cf33eb510e0eb9c6fd587cc8ee577689688dd5f322bcb71c1fd5701181398ba73
Binary file
data.tar.gz.sig CHANGED
Binary file
@@ -2,6 +2,7 @@
2
2
 
3
3
  # file: lothianbusestimetable.rb
4
4
 
5
+ require 'json'
5
6
  require 'time'
6
7
  require 'nokorexi'
7
8
 
@@ -22,11 +23,37 @@ require 'nokorexi'
22
23
 
23
24
  class LothianBusesTimetable
24
25
 
25
- attr_reader :timetable
26
+ attr_reader :timetable, :timetables
26
27
 
27
- def initialize
28
+ def initialize(localfile=nil)
29
+
28
30
  super()
29
31
  @base_url = 'https://lothianbuses.co.uk/timetables-and-maps/timetables/'
32
+
33
+ if localfile then
34
+
35
+ h = JSON.parse RXFHelper.read(localfile).first
36
+ @timetables = {}
37
+
38
+ h.each do |num, timetable|
39
+
40
+ @timetables[num] = {}
41
+
42
+ @timetables[num][:service] = timetable['service']
43
+
44
+ timetable.to_a[1..-1].each do |subheading, day|
45
+
46
+ @timetables[num][subheading.to_sym] = day.clone
47
+
48
+ day.each do |direction,table|
49
+ @timetables[num][subheading.to_sym][direction.to_sym] = table
50
+ end
51
+ end
52
+
53
+ end
54
+
55
+ end
56
+
30
57
  end
31
58
 
32
59
  def fetch_timetable(service_number)
@@ -47,9 +74,11 @@ class LothianBusesTimetable
47
74
  table.xpath('tr').each do |tr|
48
75
 
49
76
  # get the name
50
- tds = tr.xpath('td')#.map(&:unescape)
51
-
52
- a = tds.flat_map {|x| r = x.xpath('.//text()').map(&:unescape); r.any? ? r : ''}
77
+ tds = tr.xpath('td')
78
+ a = tds.flat_map do |x|
79
+ r = x.xpath('.//text()').map(&:unescape)
80
+ r.any? ? r : ''
81
+ end
53
82
 
54
83
  next unless a.any?
55
84
 
@@ -75,9 +104,11 @@ class LothianBusesTimetable
75
104
  elsif col1.empty? or a.length <= 1
76
105
 
77
106
  tablebreak += 1 if a0.last and a0.last.any?
78
- if (prev_col.empty? or prev_col.length <= 1) and a0.last and a0.last.any? then
107
+ if (prev_col.empty? or prev_col.length <= 1) and a0.last \
108
+ and a0.last.any? then
79
109
 
80
- if a0.last.keys.first == col1 and prev_col.empty? and a0.last.keys.length > 1 then
110
+ if a0.last.keys.first == col1 and prev_col.empty? \
111
+ and a0.last.keys.length > 1 then
81
112
 
82
113
  a0 << {}
83
114
  end
@@ -91,7 +122,8 @@ class LothianBusesTimetable
91
122
 
92
123
  if a.any? and a.length > 2 and a0.last and col1.length < 40 then
93
124
 
94
- if a0.last.keys.last == col1 and prev_col.empty? and a0.last.keys.length > 1 then
125
+ if a0.last.keys.last == col1 and prev_col.empty? and
126
+ a0.last.keys.length > 1 then
95
127
 
96
128
  a0 << {}
97
129
  end
@@ -100,6 +132,7 @@ class LothianBusesTimetable
100
132
  h = a0.last
101
133
 
102
134
  tablebreak = 0 if h.empty?
135
+
103
136
  if h.has_key? col1 then
104
137
 
105
138
  if tablebreak == 0 then
@@ -112,7 +145,6 @@ class LothianBusesTimetable
112
145
  h[col1] = a.take(19)
113
146
  end
114
147
 
115
-
116
148
  else
117
149
 
118
150
  tablebreak += 1
@@ -151,48 +183,8 @@ class LothianBusesTimetable
151
183
  else
152
184
  gaps = []
153
185
  end
186
+
154
187
 
155
-
156
-
157
- # sanitise the times (where short hand times are
158
- # given i.e. minutes only)
159
-
160
- =begin
161
- # sanitise diabled for now
162
- # probably perform this at the end now
163
-
164
- printed_rows.each do |name, row|
165
-
166
- prev_time = nil
167
- printed_rows[name] = row.map.with_index do |col,i|
168
-
169
- if gaps.include? i then
170
- col
171
- else
172
- case col
173
- when /^\d{4}$/
174
- # record the time
175
- prev_time = Time.strptime(col, "%H%M")
176
- col
177
- when /^\d{2}$/
178
-
179
- next if prev_time.nil?
180
- # substitute with a time
181
- val = "%02d%02d" % [prev_time.hour, col.to_i]
182
-
183
- #if col is less than minutes, increment the hour
184
- t = Time.strptime(val, "%H%M")
185
- t = t + (60 * 60) if prev_time.min > t.min
186
- prev_time = t
187
- t.strftime("%H%M")
188
- else
189
- col
190
- end
191
- end
192
- end
193
-
194
- end
195
- =end
196
188
  # fill in the gaps
197
189
 
198
190
  periods = gaps.map {|i| a.map {|k,v| v[i].to_s.gsub(/\W/,' ')}
@@ -236,6 +228,27 @@ class LothianBusesTimetable
236
228
 
237
229
  end
238
230
 
231
+ def fetch_all_timetables()
232
+
233
+ return @timetables if @timetables
234
+
235
+ number = []
236
+ self.services.each {|k,type| type.each {|x| number << x[:number]} }
237
+
238
+ timetables = number.map do |num|
239
+
240
+ puts 'fetching timetable ' + num
241
+
242
+ [num, self.fetch_timetable(num)]
243
+
244
+ end
245
+
246
+ @timetables = timetables.to_h
247
+
248
+ end
249
+
250
+ alias fetch_all fetch_all_timetables
251
+
239
252
  def services()
240
253
 
241
254
  return @services if @services
@@ -258,6 +271,18 @@ class LothianBusesTimetable
258
271
  @services = o.to_h
259
272
 
260
273
  end
274
+
275
+ def save_timetables(filename='bustimetables.json')
276
+
277
+ File.write filename, @timetables.to_json
278
+
279
+ end
280
+
281
+ alias save save_timetables
282
+
283
+ def timetable(num)
284
+ @timetables[num]
285
+ end
261
286
 
262
287
  private
263
288
 
@@ -282,17 +307,14 @@ class LothianBusesTimetable
282
307
  master[:weekday][:inbound] = h
283
308
  h = a0.shift
284
309
  a0.reject! {|x| x.empty?}
285
-
286
310
 
287
311
  if a0.first and a0.first.any? then
288
312
 
289
- #h = a0.shift
290
313
  h = a0.shift until h.any?
291
314
 
292
315
  master[:weekday][:outbound] = h unless h.has_key? :timetable
293
316
 
294
317
  h = a0.shift unless h.has_key? :timetable
295
-
296
318
 
297
319
  if h and a0.any? and h[:timetable] != 'Public Holiday Timetable' then
298
320
 
@@ -307,11 +329,11 @@ class LothianBusesTimetable
307
329
  h = a0.shift until h.any?
308
330
 
309
331
  master[:saturday][:outbound] = h unless h.has_key? :timetable
310
-
311
332
 
312
333
  h = a0.shift until h.any?
313
334
 
314
335
  if h and a0.any? then
336
+
315
337
  h = a0.shift
316
338
  h = a0.shift until h.any?
317
339
  master[:sunday][:desc] = h[:timetable]
@@ -382,12 +404,10 @@ class LothianBusesTimetable
382
404
  [k,times, (start_i+1..i-1)]
383
405
  end.compact
384
406
 
385
-
386
407
  end
387
408
 
388
409
  def every_interval(rows, i, interval=60)
389
- # todo 26-Aug-2017
390
- # here we will add
410
+
391
411
  return unless interval
392
412
 
393
413
  rows.map do |k,v|
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lothianbusestimetable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Robertson
@@ -31,7 +31,7 @@ cert_chain:
31
31
  K7yWGJq+eGl34xrLaucvTbO8Ua3rHayCkBzgfx5LVcch22Axj8quw+YGsPnhuosr
32
32
  BHdCmx9eUF/TKg==
33
33
  -----END CERTIFICATE-----
34
- date: 2017-08-29 00:00:00.000000000 Z
34
+ date: 2017-08-30 00:00:00.000000000 Z
35
35
  dependencies:
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: nokorexi
metadata.gz.sig CHANGED
Binary file