viaggiatreno 1.0.2 → 1.0.5
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 +7 -0
- data/.codeclimate.yml +25 -0
- data/.gitignore +5 -1
- data/.rubocop.yml +46 -0
- data/.travis.yml +17 -0
- data/Gemfile +10 -1
- data/Guardfile +12 -0
- data/LICENSE +21 -0
- data/README.md +8 -2
- data/Rakefile +2 -1
- data/lib/viaggia_treno.rb +1 -0
- data/lib/viaggiatreno.rb +2 -3
- data/lib/viaggiatreno/regex_match_info.rb +11 -0
- data/lib/viaggiatreno/scraper.rb +94 -0
- data/lib/viaggiatreno/string_utils.rb +6 -0
- data/lib/viaggiatreno/train.rb +93 -0
- data/lib/viaggiatreno/train_state.rb +6 -0
- data/lib/viaggiatreno/train_stop.rb +26 -0
- data/lib/viaggiatreno/train_stop_state.rb +5 -0
- data/lib/viaggiatreno/version.rb +1 -2
- data/lib/viaggiatreno/viaggiatreno_urls.rb +14 -0
- data/lib/viaggiatreno/xpath_match_info.rb +9 -0
- data/spec/spec_helper.rb +17 -0
- data/spec/vcr_cassettes/Arrived_train_delay_0_.yml +538 -0
- data/spec/vcr_cassettes/Arrived_train_delay_negative_.yml +621 -0
- data/spec/vcr_cassettes/Arrived_train_delay_positive_.yml +680 -0
- data/spec/vcr_cassettes/Not_departed_train.yml +438 -0
- data/spec/vcr_cassettes/Running_train_delay_positive_.yml +468 -0
- data/spec/viaggiatreno/train_spec.rb +190 -0
- data/spec/viaggiatreno_spec.rb +9 -0
- data/viaggiatreno.gemspec +9 -8
- metadata +57 -25
- data/LICENSE.txt +0 -22
- data/lib/viaggiatreno/RegExpMatchInfo.rb +0 -23
- data/lib/viaggiatreno/Scraper.rb +0 -87
- data/lib/viaggiatreno/StopState.rb +0 -8
- data/lib/viaggiatreno/StringUtils.rb +0 -7
- data/lib/viaggiatreno/Train.rb +0 -92
- data/lib/viaggiatreno/TrainState.rb +0 -11
- data/lib/viaggiatreno/TrainStop.rb +0 -24
- data/lib/viaggiatreno/ViaggiatrenoURLs.rb +0 -17
- data/lib/viaggiatreno/XPathMatchInfo.rb +0 -19
@@ -0,0 +1,190 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'viaggia_treno'
|
3
|
+
|
4
|
+
describe Train do
|
5
|
+
describe 'Arrived train (delay negative)' do
|
6
|
+
before do
|
7
|
+
VCR.use_cassette('Arrived train (delay negative)') do
|
8
|
+
@train = Train.new('2550')
|
9
|
+
@train.update_details
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
it do
|
14
|
+
expect(@train.to_s).to eq \
|
15
|
+
'2550 REG 2550: Il treno e\' arrivato con 2 minuti di anticipo state'\
|
16
|
+
': ARRIVED, delay: -2, last_update: '
|
17
|
+
expect(@train.state).to eq TrainState::ARRIVED
|
18
|
+
expect(@train.delay).to eq(-2)
|
19
|
+
expect(@train.train_number).to eq '2550'
|
20
|
+
expect(@train.train_name).to eq 'REG 2550'
|
21
|
+
expect(@train.status).to eq 'Il treno e\' arrivato con 2 minuti di anticipo'
|
22
|
+
expect(@train.last_update).to eq nil
|
23
|
+
expect(@train.last_stop).to eq '[X] TIRANO = SCHEDULED: 08:52 ACTUAL: 08:50'
|
24
|
+
expect(@train.departing_station).to eq 'MILANO CENTRALE'
|
25
|
+
expect(@train.arriving_station).to eq 'TIRANO'
|
26
|
+
expect(@train.scheduled_departing_time).to eq '06:20'
|
27
|
+
expect(@train.actual_departing_time).to eq '06:20'
|
28
|
+
expect(@train.scheduled_arriving_time).to eq '08:52'
|
29
|
+
expect(@train.actual_arriving_time).to eq '08:50'
|
30
|
+
expect(@train.scheduled_stop_time('COLICO')).to eq '07:47 [DONE]'
|
31
|
+
expect(@train.actual_stop_time('COLICO')).to eq '07:49 [DONE]'
|
32
|
+
expect(@train.train_stops.map(&:to_s)).to eq \
|
33
|
+
[
|
34
|
+
'[X] MILANO CENTRALE = SCHEDULED: 06:20 ACTUAL: 06:20',
|
35
|
+
'[X] MONZA = SCHEDULED: 06:31 ACTUAL: 06:32',
|
36
|
+
'[X] LECCO = SCHEDULED: 06:59 ACTUAL: 06:55',
|
37
|
+
'[X] VARENNA ESINO = SCHEDULED: 07:23 ACTUAL: 07:23',
|
38
|
+
'[X] BELLANO TARTAVELLE TERME = SCHEDULED: 07:28 ACTUAL: 07:29',
|
39
|
+
'[X] COLICO = SCHEDULED: 07:47 ACTUAL: 07:49',
|
40
|
+
'[X] MORBEGNO = SCHEDULED: 07:59 ACTUAL: 08:01',
|
41
|
+
'[X] SONDRIO = SCHEDULED: 08:20 ACTUAL: 08:20',
|
42
|
+
'[X] TRESENDA-APRICA-TEGLIO = SCHEDULED: 08:40 ACTUAL: 08:37',
|
43
|
+
'[X] TIRANO = SCHEDULED: 08:52 ACTUAL: 08:50'
|
44
|
+
]
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
describe 'Arrived train (delay = 0)' do
|
49
|
+
before do
|
50
|
+
VCR.use_cassette('Arrived train (delay = 0)') do
|
51
|
+
@train = Train.new('3961')
|
52
|
+
@train.update_details
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
it do
|
57
|
+
expect(@train.to_s).to eq \
|
58
|
+
'3961 REG 3961: Il treno e\' arrivato in orario state: ARRIVED, delay: 0, last_update: '
|
59
|
+
expect(@train.state).to eq TrainState::ARRIVED
|
60
|
+
expect(@train.delay).to eq 0
|
61
|
+
expect(@train.train_number).to eq '3961'
|
62
|
+
expect(@train.train_name).to eq 'REG 3961'
|
63
|
+
expect(@train.status).to eq 'Il treno e\' arrivato in orario'
|
64
|
+
expect(@train.last_update).to eq nil
|
65
|
+
expect(@train.last_stop).to eq '[X] ALESSANDRIA = SCHEDULED: 09:48 ACTUAL: 09:48'
|
66
|
+
expect(@train.departing_station).to eq 'MILANO CENTRALE'
|
67
|
+
expect(@train.arriving_station).to eq 'ALESSANDRIA'
|
68
|
+
expect(@train.scheduled_departing_time).to eq '08:25'
|
69
|
+
expect(@train.actual_departing_time).to eq '08:27'
|
70
|
+
expect(@train.scheduled_arriving_time).to eq '09:48'
|
71
|
+
expect(@train.actual_arriving_time).to eq '09:48'
|
72
|
+
expect(@train.scheduled_stop_time('TORTONA')).to eq '09:27 [DONE]'
|
73
|
+
expect(@train.actual_stop_time('TORTONA')).to eq '09:27 [DONE]'
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
describe 'Arrived train (delay positive)' do
|
78
|
+
before do
|
79
|
+
VCR.use_cassette('Arrived train (delay positive)') do
|
80
|
+
@train = Train.new('2109')
|
81
|
+
@train.update_details
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
it do
|
86
|
+
expect(@train.to_s).to eq \
|
87
|
+
'2109 REG 2109: Il treno e\' arrivato con 3 minuti di ritardo state: '\
|
88
|
+
'ARRIVED, delay: 3, last_update: '
|
89
|
+
expect(@train.state).to eq TrainState::ARRIVED
|
90
|
+
expect(@train.delay).to eq 3
|
91
|
+
expect(@train.train_number).to eq '2109'
|
92
|
+
expect(@train.train_name).to eq 'REG 2109'
|
93
|
+
expect(@train.status).to eq 'Il treno e\' arrivato con 3 minuti di ritardo'
|
94
|
+
expect(@train.last_update).to eq nil
|
95
|
+
expect(@train.last_stop).to eq '[X] VERONA PORTA NUOVA = SCHEDULED: 20:20 ACTUAL: 20:23'
|
96
|
+
expect(@train.departing_station).to eq 'MILANO CENTRALE'
|
97
|
+
expect(@train.arriving_station).to eq 'VERONA PORTA NUOVA'
|
98
|
+
expect(@train.scheduled_departing_time).to eq '18:25'
|
99
|
+
expect(@train.actual_departing_time).to eq '18:29'
|
100
|
+
expect(@train.scheduled_arriving_time).to eq '20:20'
|
101
|
+
expect(@train.actual_arriving_time).to eq '20:23'
|
102
|
+
expect(@train.scheduled_stop_time('DESENZANO')).to eq '19:50 [DONE]'
|
103
|
+
expect(@train.actual_stop_time('DESENZANO')).to eq '19:57 [DONE]'
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
describe 'Running train (delay positive)' do
|
108
|
+
before do
|
109
|
+
VCR.use_cassette('Running train (delay positive)') do
|
110
|
+
@train = Train.new('2655')
|
111
|
+
@train.update_details
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
it do
|
116
|
+
expect(@train.to_s).to eq \
|
117
|
+
'2655 REG 2655: Il treno viaggia con 1 minuti di ritardo state: '\
|
118
|
+
'TRAVELING, delay: 1, last_update: MILANO LAMBRATE alle ore 14:28'
|
119
|
+
expect(@train.state).to eq TrainState::TRAVELING
|
120
|
+
expect(@train.delay).to eq 1
|
121
|
+
expect(@train.train_number).to eq '2655'
|
122
|
+
expect(@train.train_name).to eq 'REG 2655'
|
123
|
+
expect(@train.status).to eq 'Il treno viaggia con 1 minuti di ritardo'
|
124
|
+
expect(@train.last_update).to eq 'MILANO LAMBRATE alle ore 14:28'
|
125
|
+
expect(@train.last_stop).to eq '[X] MILANO LAMBRATE = SCHEDULED: 14:26 ACTUAL: 14:25'
|
126
|
+
expect(@train.departing_station).to eq 'MILANO CENTRALE'
|
127
|
+
expect(@train.arriving_station).to eq 'MANTOVA'
|
128
|
+
expect(@train.scheduled_departing_time).to eq '14:20'
|
129
|
+
expect(@train.actual_departing_time).to eq '14:21'
|
130
|
+
expect(@train.scheduled_arriving_time).to eq '16:10'
|
131
|
+
expect(@train.actual_arriving_time).to eq '16:11'
|
132
|
+
expect(@train.scheduled_stop_time('PIADENA')).to eq '15:46 [TO_BE_DONE]'
|
133
|
+
expect(@train.actual_stop_time('PIADENA')).to eq '15:47 [TO_BE_DONE]'
|
134
|
+
expect(@train.train_stops.map(&:to_s)).to eq \
|
135
|
+
[
|
136
|
+
'[X] MILANO CENTRALE = SCHEDULED: 14:20 ACTUAL: 14:21',
|
137
|
+
'[X] MILANO LAMBRATE = SCHEDULED: 14:26 ACTUAL: 14:25',
|
138
|
+
'[ ] MILANO ROGOREDO = SCHEDULED: 14:31 EXPECTED: 14:32',
|
139
|
+
'[ ] LODI = SCHEDULED: 14:46 EXPECTED: 14:47',
|
140
|
+
'[ ] CODOGNO = SCHEDULED: 15:01 EXPECTED: 15:02',
|
141
|
+
'[ ] PONTE D`ADDA = SCHEDULED: 15:11 EXPECTED: 15:12',
|
142
|
+
'[ ] CREMONA = SCHEDULED: 15:28 EXPECTED: 15:29',
|
143
|
+
'[ ] PIADENA = SCHEDULED: 15:46 EXPECTED: 15:47',
|
144
|
+
'[ ] MANTOVA = SCHEDULED: 16:10 EXPECTED: 16:11'
|
145
|
+
]
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
149
|
+
describe 'Not departed train' do
|
150
|
+
before do
|
151
|
+
VCR.use_cassette('Not departed train') do
|
152
|
+
@train = Train.new('2657')
|
153
|
+
@train.update_details
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
it do
|
158
|
+
expect(@train.to_s).to eq \
|
159
|
+
'2657 REG 2657: Il treno non e\' ancora partito state: '\
|
160
|
+
'NOT DEPARTED, delay: , last_update: '
|
161
|
+
expect(@train.state).to eq TrainState::NOT_DEPARTED
|
162
|
+
expect(@train.delay).to eq nil
|
163
|
+
expect(@train.train_number).to eq '2657'
|
164
|
+
expect(@train.train_name).to eq 'REG 2657'
|
165
|
+
expect(@train.status).to eq 'Il treno non e\' ancora partito'
|
166
|
+
expect(@train.last_update).to eq nil
|
167
|
+
expect(@train.last_stop).to eq nil
|
168
|
+
expect(@train.departing_station).to eq 'MILANO CENTRALE'
|
169
|
+
expect(@train.arriving_station).to eq 'MANTOVA'
|
170
|
+
expect(@train.scheduled_departing_time).to eq '16:20'
|
171
|
+
expect(@train.actual_departing_time).to eq ''
|
172
|
+
expect(@train.scheduled_arriving_time).to eq '18:10'
|
173
|
+
expect(@train.actual_arriving_time).to eq '18:10'
|
174
|
+
expect(@train.scheduled_stop_time('LODI')).to eq '16:46 [TO_BE_DONE]'
|
175
|
+
expect(@train.actual_stop_time('LODI')).to eq '16:46 [TO_BE_DONE]'
|
176
|
+
expect(@train.train_stops.map(&:to_s)).to eq \
|
177
|
+
[
|
178
|
+
'[ ] MILANO CENTRALE = SCHEDULED: 16:20 EXPECTED: ',
|
179
|
+
'[ ] MILANO LAMBRATE = SCHEDULED: 16:26 EXPECTED: 16:26',
|
180
|
+
'[ ] MILANO ROGOREDO = SCHEDULED: 16:31 EXPECTED: 16:31',
|
181
|
+
'[ ] LODI = SCHEDULED: 16:46 EXPECTED: 16:46',
|
182
|
+
'[ ] CODOGNO = SCHEDULED: 17:01 EXPECTED: 17:01',
|
183
|
+
'[ ] PONTE D`ADDA = SCHEDULED: 17:11 EXPECTED: 17:11',
|
184
|
+
'[ ] CREMONA = SCHEDULED: 17:28 EXPECTED: 17:28',
|
185
|
+
'[ ] PIADENA = SCHEDULED: 17:46 EXPECTED: 17:46',
|
186
|
+
'[ ] MANTOVA = SCHEDULED: 18:10 EXPECTED: 18:10'
|
187
|
+
]
|
188
|
+
end
|
189
|
+
end
|
190
|
+
end
|
data/viaggiatreno.gemspec
CHANGED
@@ -4,17 +4,18 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
4
|
require 'viaggiatreno/version'
|
5
5
|
|
6
6
|
Gem::Specification.new do |gem|
|
7
|
-
gem.name =
|
7
|
+
gem.name = 'viaggiatreno'
|
8
8
|
gem.version = Viaggiatreno::VERSION
|
9
|
-
gem.authors = [
|
10
|
-
gem.email = [
|
11
|
-
gem.description =
|
12
|
-
gem.summary =
|
13
|
-
gem.homepage =
|
9
|
+
gem.authors = ['Michele Bologna']
|
10
|
+
gem.email = ['michele.bologna@gmail.com']
|
11
|
+
gem.description = 'A web scraper to fetch real time information on train riding the Italian railway system (viaggiatreno/trenitalia)'
|
12
|
+
gem.summary = 'A scraper for real time information on Italian railway system (viaggiatreno)'
|
13
|
+
gem.homepage = ''
|
14
14
|
|
15
|
-
gem.files = `git ls-files`.split(
|
16
|
-
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
15
|
+
gem.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map { |f| File.basename(f) }
|
17
17
|
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
18
|
gem.require_paths = ["lib"]
|
19
19
|
gem.add_dependency "nokogiri"
|
20
|
+
gem.add_development_dependency 'rake'
|
20
21
|
end
|
metadata
CHANGED
@@ -1,30 +1,41 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: viaggiatreno
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
5
|
-
prerelease:
|
4
|
+
version: 1.0.5
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Michele Bologna
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
11
|
+
date: 2016-01-20 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: nokogiri
|
16
15
|
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
16
|
requirements:
|
19
|
-
- -
|
17
|
+
- - ">="
|
20
18
|
- !ruby/object:Gem::Version
|
21
19
|
version: '0'
|
22
20
|
type: :runtime
|
23
21
|
prerelease: false
|
24
22
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
23
|
requirements:
|
27
|
-
- -
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
28
39
|
- !ruby/object:Gem::Version
|
29
40
|
version: '0'
|
30
41
|
description: A web scraper to fetch real time information on train riding the Italian
|
@@ -35,45 +46,66 @@ executables: []
|
|
35
46
|
extensions: []
|
36
47
|
extra_rdoc_files: []
|
37
48
|
files:
|
38
|
-
- .
|
49
|
+
- ".codeclimate.yml"
|
50
|
+
- ".gitignore"
|
51
|
+
- ".rubocop.yml"
|
52
|
+
- ".travis.yml"
|
39
53
|
- Gemfile
|
40
|
-
-
|
54
|
+
- Guardfile
|
55
|
+
- LICENSE
|
41
56
|
- README.md
|
42
57
|
- Rakefile
|
58
|
+
- lib/viaggia_treno.rb
|
43
59
|
- lib/viaggiatreno.rb
|
44
|
-
- lib/viaggiatreno/
|
45
|
-
- lib/viaggiatreno/
|
46
|
-
- lib/viaggiatreno/
|
47
|
-
- lib/viaggiatreno/
|
48
|
-
- lib/viaggiatreno/
|
49
|
-
- lib/viaggiatreno/
|
50
|
-
- lib/viaggiatreno/
|
51
|
-
- lib/viaggiatreno/ViaggiatrenoURLs.rb
|
52
|
-
- lib/viaggiatreno/XPathMatchInfo.rb
|
60
|
+
- lib/viaggiatreno/regex_match_info.rb
|
61
|
+
- lib/viaggiatreno/scraper.rb
|
62
|
+
- lib/viaggiatreno/string_utils.rb
|
63
|
+
- lib/viaggiatreno/train.rb
|
64
|
+
- lib/viaggiatreno/train_state.rb
|
65
|
+
- lib/viaggiatreno/train_stop.rb
|
66
|
+
- lib/viaggiatreno/train_stop_state.rb
|
53
67
|
- lib/viaggiatreno/version.rb
|
68
|
+
- lib/viaggiatreno/viaggiatreno_urls.rb
|
69
|
+
- lib/viaggiatreno/xpath_match_info.rb
|
70
|
+
- spec/spec_helper.rb
|
71
|
+
- spec/vcr_cassettes/Arrived_train_delay_0_.yml
|
72
|
+
- spec/vcr_cassettes/Arrived_train_delay_negative_.yml
|
73
|
+
- spec/vcr_cassettes/Arrived_train_delay_positive_.yml
|
74
|
+
- spec/vcr_cassettes/Not_departed_train.yml
|
75
|
+
- spec/vcr_cassettes/Running_train_delay_positive_.yml
|
76
|
+
- spec/viaggiatreno/train_spec.rb
|
77
|
+
- spec/viaggiatreno_spec.rb
|
54
78
|
- viaggiatreno.gemspec
|
55
79
|
homepage: ''
|
56
80
|
licenses: []
|
81
|
+
metadata: {}
|
57
82
|
post_install_message:
|
58
83
|
rdoc_options: []
|
59
84
|
require_paths:
|
60
85
|
- lib
|
61
86
|
required_ruby_version: !ruby/object:Gem::Requirement
|
62
|
-
none: false
|
63
87
|
requirements:
|
64
|
-
- -
|
88
|
+
- - ">="
|
65
89
|
- !ruby/object:Gem::Version
|
66
90
|
version: '0'
|
67
91
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
68
|
-
none: false
|
69
92
|
requirements:
|
70
|
-
- -
|
93
|
+
- - ">="
|
71
94
|
- !ruby/object:Gem::Version
|
72
95
|
version: '0'
|
73
96
|
requirements: []
|
74
97
|
rubyforge_project:
|
75
|
-
rubygems_version:
|
98
|
+
rubygems_version: 2.2.2
|
76
99
|
signing_key:
|
77
|
-
specification_version:
|
100
|
+
specification_version: 4
|
78
101
|
summary: A scraper for real time information on Italian railway system (viaggiatreno)
|
79
|
-
test_files:
|
102
|
+
test_files:
|
103
|
+
- spec/spec_helper.rb
|
104
|
+
- spec/vcr_cassettes/Arrived_train_delay_0_.yml
|
105
|
+
- spec/vcr_cassettes/Arrived_train_delay_negative_.yml
|
106
|
+
- spec/vcr_cassettes/Arrived_train_delay_positive_.yml
|
107
|
+
- spec/vcr_cassettes/Not_departed_train.yml
|
108
|
+
- spec/vcr_cassettes/Running_train_delay_positive_.yml
|
109
|
+
- spec/viaggiatreno/train_spec.rb
|
110
|
+
- spec/viaggiatreno_spec.rb
|
111
|
+
has_rdoc:
|
data/LICENSE.txt
DELETED
@@ -1,22 +0,0 @@
|
|
1
|
-
Copyright (c) 2013 Michele Bologna
|
2
|
-
|
3
|
-
MIT License
|
4
|
-
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
-
a copy of this software and associated documentation files (the
|
7
|
-
"Software"), to deal in the Software without restriction, including
|
8
|
-
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
-
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
-
permit persons to whom the Software is furnished to do so, subject to
|
11
|
-
the following conditions:
|
12
|
-
|
13
|
-
The above copyright notice and this permission notice shall be
|
14
|
-
included in all copies or substantial portions of the Software.
|
15
|
-
|
16
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
-
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
-
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
-
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
-
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
-
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
-
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
@@ -1,23 +0,0 @@
|
|
1
|
-
class RegExpMatchInfo
|
2
|
-
|
3
|
-
# regex to match train status (string)
|
4
|
-
@@REGEXP_STATE_RUNNING = /(Il treno viaggia.*)(Ultimo rilevamento a)(.*)/
|
5
|
-
@@REGEXP_STATE_NOT_STARTED = /Il treno non e' ancora partito/
|
6
|
-
@@REGEXP_STATE_FINISHED = /Il treno e' arrivato.*/
|
7
|
-
@@REGEXP_DELAY_STR = /con (\d+) minuti di ([anticipo|ritardo]+)/
|
8
|
-
@@REGEXP_NODELAY_STR = /Il treno .* in orario.*/
|
9
|
-
@@REGEXP_STOP_ALREADY_DONE = /giaeffettuate/
|
10
|
-
@@STR_DELAY_STR = "ritardo"
|
11
|
-
@@STR_TRAIN_NUMBER_URL_REPLACE = "TRAINNUMBER"
|
12
|
-
|
13
|
-
# attr_reader for class variables
|
14
|
-
def self.REGEXP_STATE_FINISHED() @@REGEXPSTATE_FINISHED end
|
15
|
-
def self.REGEXP_STATE_RUNNING() @@REGEXP_STATE_RUNNING end
|
16
|
-
def self.REGEXP_STATE_NOT_STARTED() @@REGEXP_STATE_NOT_STARTED end
|
17
|
-
def self.REGEXP_STATE_FINISHED() @@REGEXP_STATE_FINISHED end
|
18
|
-
def self.STR_DELAY_STR() @@STR_DELAY_STR end
|
19
|
-
def self.REGEXP_DELAY_STR() @@REGEXP_DELAY_STR end
|
20
|
-
def self.REGEXP_NODELAY_STR() @@REGEXP_NODELAY_STR end
|
21
|
-
def self.REGEXP_STOP_ALREADY_DONE() @@REGEXP_STOP_ALREADY_DONE end
|
22
|
-
def self.STR_TRAIN_NUMBER_URL_REPLACE() @@STR_TRAIN_NUMBER_URL_REPLACE end
|
23
|
-
end
|
data/lib/viaggiatreno/Scraper.rb
DELETED
@@ -1,87 +0,0 @@
|
|
1
|
-
require 'open-uri'
|
2
|
-
require 'nokogiri'
|
3
|
-
require_relative 'TrainStop'
|
4
|
-
require_relative 'TrainState'
|
5
|
-
require_relative 'StringUtils'
|
6
|
-
require_relative 'RegExpMatchInfo'
|
7
|
-
require_relative 'StopState'
|
8
|
-
require_relative 'XPathMatchInfo'
|
9
|
-
require_relative 'ViaggiatrenoURLs'
|
10
|
-
|
11
|
-
class Scraper
|
12
|
-
|
13
|
-
def initialize(trainNumber, train)
|
14
|
-
@site_info_main = ViaggiatrenoURLs.SITE_INFO_MAIN.gsub(
|
15
|
-
RegExpMatchInfo.STR_TRAIN_NUMBER_URL_REPLACE, trainNumber)
|
16
|
-
@site_info_details = ViaggiatrenoURLs.SITE_INFO_DETAILS.gsub(
|
17
|
-
RegExpMatchInfo.STR_TRAIN_NUMBER_URL_REPLACE, trainNumber)
|
18
|
-
@train = train
|
19
|
-
end
|
20
|
-
|
21
|
-
# fetch and parse basic train information (status, trainName, details)
|
22
|
-
def updateTrain()
|
23
|
-
doc = Nokogiri::HTML(open(@site_info_main))
|
24
|
-
doc.xpath(XPathMatchInfo.XPATH_STATUS).each do |x|
|
25
|
-
@status = StringUtils.remove_newlines_tabs_and_spaces(x)
|
26
|
-
end
|
27
|
-
doc.xpath(XPathMatchInfo.XPATH_TRAIN_NAME).each do |x|
|
28
|
-
@trainName = x.content
|
29
|
-
end
|
30
|
-
if @status =~ RegExpMatchInfo.REGEXP_STATE_NOT_STARTED
|
31
|
-
@train.state = TrainState.NOT_STARTED
|
32
|
-
elsif @status =~ RegExpMatchInfo.REGEXP_STATE_RUNNING or \
|
33
|
-
RegExpMatchInfo.REGEXP_STATE_FINISHED
|
34
|
-
if @status =~ RegExpMatchInfo.REGEXP_NODELAY_STR
|
35
|
-
@train.delay = 0
|
36
|
-
else
|
37
|
-
@train.delay = @status.match(RegExpMatchInfo.REGEXP_DELAY_STR)[1].to_i
|
38
|
-
if @status.match(RegExpMatchInfo.REGEXP_DELAY_STR)[2] \
|
39
|
-
!= RegExpMatchInfo.STR_DELAY_STR
|
40
|
-
# train is ahead of time, delay is negative
|
41
|
-
@train.delay *= -1
|
42
|
-
end
|
43
|
-
end
|
44
|
-
if @status =~ RegExpMatchInfo.REGEXP_STATE_RUNNING
|
45
|
-
@train.state = TrainState.RUNNING
|
46
|
-
@train.lastUpdate = @status.match(
|
47
|
-
RegExpMatchInfo.REGEXP_STATE_RUNNING)[3].strip
|
48
|
-
@status = @status.match(RegExpMatchInfo.REGEXP_STATE_RUNNING)[1].rstrip
|
49
|
-
else
|
50
|
-
@train.state = TrainState.FINISHED
|
51
|
-
end
|
52
|
-
end
|
53
|
-
|
54
|
-
@train.status = @status
|
55
|
-
@train.trainName = @trainName
|
56
|
-
end
|
57
|
-
|
58
|
-
# fetch and parse train details (departing and arriving station,
|
59
|
-
# intermediate stops)
|
60
|
-
def updateTrainDetails()
|
61
|
-
doc = Nokogiri::HTML(open(@site_info_details))
|
62
|
-
doc.xpath(XPathMatchInfo.XPATH_DETAILS_GENERIC).each do |x|
|
63
|
-
x.xpath(XPathMatchInfo.XPATH_DETAILS_STATION_NAME).each do |stationName|
|
64
|
-
@stationName = stationName.to_s
|
65
|
-
end
|
66
|
-
x.xpath(XPathMatchInfo.XPATH_DETAILS_SCHEDULED_STOP_TIME).each do \
|
67
|
-
|scheduledArrivalTime|
|
68
|
-
@scheduledArrivalTime = StringUtils.remove_newlines_tabs_and_spaces(
|
69
|
-
scheduledArrivalTime).to_s
|
70
|
-
end
|
71
|
-
x.xpath(XPathMatchInfo.XPATH_DETAILS_ACTUAL_STOP_TIME).each do \
|
72
|
-
|actualArrivalTime|
|
73
|
-
@actualArrivalTime = StringUtils.remove_newlines_tabs_and_spaces(
|
74
|
-
actualArrivalTime).to_s
|
75
|
-
end
|
76
|
-
if x.attributes()['class'].to_s =~ RegExpMatchInfo.REGEXP_STOP_ALREADY_DONE
|
77
|
-
t = TrainStop.new(@stationName, @scheduledArrivalTime,
|
78
|
-
@actualArrivalTime, StopState.DONE)
|
79
|
-
else
|
80
|
-
t = TrainStop.new(@stationName, @scheduledArrivalTime,
|
81
|
-
@actualArrivalTime, StopState.TODO)
|
82
|
-
end
|
83
|
-
@train.addStop(t)
|
84
|
-
end
|
85
|
-
end
|
86
|
-
end
|
87
|
-
|