This repository has been archived by the owner on Aug 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EPTCBus.rb
159 lines (143 loc) · 3.93 KB
/
EPTCBus.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# frozen_string_literal: true
require 'rubygems'
require 'nokogiri'
require 'open-uri'
require './models'
# Bus class that will parse the html content and save into sqlite
class EPTCBus
def initialize(id, name, url)
@id = id.to_s.strip
@url = url.to_s.strip
@code = name.match(/^\w+/).to_s.to_sym
@name = name.match(/(?!^\w+)(?!\s{1,}\-)\s.+(\n|)/m).to_s.strip
@is_summer_time = false # :(
@current_day_type = nil
@current_direction = nil
@direction_types = {
unknown: 0, circular: 1,
bairro_centro: 2, centro_bairro: 3,
bairro_terminal: 4, terminal_bairro: 5,
norte_sul: 6, sul_norte: 7,
norte_leste: 8, leste_norte: 9,
leste_sul: 10, sul_leste: 11
}
@day_types = { unknown: 0, mon_fri: 1, saturday: 2, sunday: 3 }
end
# blacklist
def valid?(all_buses = {})
return false if %w[395-79 394-27].include?(@id) || all_buses[@code.to_s]
end
def create_bus
Bus.find_or_create_by!(
identifier: @id,
code: @code,
name: @name,
url: @url
)
end
def create_schedule(schedule_data, is_handicap, bus)
@current_direction ||= :unknown
@current_day_type ||= :unknown
# week_day values based on the date of: 03/01/2020,
# so it's even possible to query the stop_datetime based on the week day.
week_day = { mon_fri: 3, saturday: 4, sunday: 5 }
day = week_day[@current_day_type]
hour = schedule_data[0]
minute = schedule_data[1]
time_date_time = DateTime.new(2020, 1, day, hour, minute, 0, 0, 0)
Schedule.find_or_create_by!(
direction: @direction_types[@current_direction],
dayType: @day_types[@current_day_type],
hour: hour,
minute: minute,
time: schedule_data[2],
timeDateTime: time_date_time,
isHandicap: is_handicap,
isSummerTime: @is_summer_time,
bus_id: bus.id
)
end
def build(options = { sleep: 1 })
sleep options[:sleep]
page = Nokogiri::HTML(open(@url))
if page.text.match(/Nenhum registro encontrado/)
error_message = "Nenhum horario encontrado em #{@code} - #{@name}"
raise StandardError, error_message
end
page.xpath('//b').each do |row|
# Collect schedule info here
text = row.text.to_s.strip
@is_summer_time = summer_time?(text)
if (direction = which_direction(text))
@current_direction = direction
next
elsif (day_type = which_day_type(text))
@current_day_type = day_type
next
elsif (schedule_data = schedule_info(row))
# INSERT new schedule here
bus = create_bus
create_schedule schedule_data, handicap?(row), bus
next
end
end
end
def which_direction(text)
case text
when 'BAIRRO/CENTRO'
:bairro_centro
when 'CENTRO/BAIRRO'
:centro_bairro
when 'BAIR/CENT/BAIR', 'CENT/BAIR/CENT', 'TERMINAL/BAIRRO/TERMINAL'
:circular
when 'BAIRRO/TERMINAL'
:bairro_terminal
when 'TERMINAL/BAIRRO'
:terminal_bairro
when 'NORTE/SUL'
:norte_sul
when 'SUL/NORTE'
:sul_norte
when 'NORTE/LESTE'
:norte_leste
when 'LESTE/NORTE'
:leste_norte
when 'LESTE/SUL'
:leste_sul
when 'SUL/LESTE'
:sul_leste
end
end
def which_day_type(text)
case text
when 'Dias Úteis'
:mon_fri
when 'Sábados'
:saturday
when 'Domingos'
:sunday
end
end
def handicap?(row)
if row.children.length > 1
time_row_text = row.children[1].attributes['src'].value.to_s
time_row_text.match(/APD\.gif/) ? true : false
else
false
end
end
def summer_time?(text)
if text.match(/\sVERAO$/)
true # :D
elsif text.match(/\sOFICIAL$/)
false # :(
else
@is_summer_time
end
end
def schedule_info(row)
schedule_time = row.text.match(/(\d{2})[:](\d{2})/)
return false if schedule_time.nil?
[schedule_time[1].to_i, schedule_time[2].to_i, schedule_time[0]]
end
end