-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate.py
294 lines (233 loc) · 10.8 KB
/
generate.py
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
import os
import zipfile
from typing import Dict, List
import requests
from bs4 import BeautifulSoup
from requests_toolbelt.multipart.encoder import MultipartEncoder
agency = """agency_id,agency_name,agency_lang,agency_timezone,agency_url,agency_phone,agency_email,agency_fare_url
1,Kapnos Airport Shuttle,en,Asia/Nicosia,https://kapnosairportshuttle.com,+357 24 00 87 18,[email protected],https://kapnosairportshuttle.com/routes"""
trips = "route_id,service_id,trip_id"
stop_times = "trip_id,arrival_time,departure_time,stop_id,stop_sequence"
calendar = "service_id,monday,tuesday,wednesday,thursday,friday,saturday,sunday,start_date,end_date"
stops = "stop_id,stop_name,stop_lat,stop_lon,zone_id,location_type,parent_station,wheelchair_boarding"
routes = "route_id,route_short_name,route_long_name,route_type,route_url"
fare_rules = "fare_id,route_id,origin_id,destination_id"
fare_attributes = "fare_id,price,currency_type,payment_method,transfers"
__id = 0
class Period:
def __convert_date(self, date: str):
day, month, year = date.split("/")
return f"{year}{month}{day}"
def __init__(self, id, start_date, end_date):
self.id = id
self.start_date = self.__convert_date(start_date)
self.end_date = self.__convert_date(end_date)
def id():
global __id
__id += 1
return __id
def stop(stop_name, stop_lat, stop_lon):
global stops
stop_point_id = id()
stop_area_id = id()
zone_id = stop_point_id
stop_point = f"\n{stop_point_id},{stop_name},{
stop_lat},{stop_lon},{zone_id},0,{stop_area_id},0"
stop_area = f"\n{stop_area_id},{stop_name},{
stop_lat},{stop_lon},{zone_id},1,,0"
stops += (stop_point + stop_area)
return stop_point_id
def service(monday, tuesday, wednesday, thursday, friday, saturday, sunday, valid_from, valid_to):
global calendar
service_id = id()
calendar += f"\n{service_id},{monday},{tuesday},{wednesday},{
thursday},{friday},{saturday},{sunday},{valid_from},{valid_to}"
return service_id
def route(route_name, route_url):
global routes
ROUTE_SHORT_NAME = "Airport Shuttle"
route_id = id()
routes += f"\n{route_id},{ROUTE_SHORT_NAME},{route_name},3,{route_url}"
return route_id
def trip(route_id, service_id):
global trips
trip_id = id()
trips += f"\n{route_id},{service_id},{trip_id}"
return trip_id
def __append_minutes(time: str, minutes: int):
h, m, s = 0, 0, 0
if len(time) == 8:
h, m, s = time.split(":")
else:
h, m = time.split(":")
m = int(m) + minutes
h = int(h) + m // 60
m = m % 60
return f"{h:02d}:{m:02d}:{s}"
def add_trip(route_id, service_id,
start_stop_point_id, end_stop_point_id,
start_time, travel_time_minutes):
global stop_times, trips
trip_id = trip(route_id, service_id)
start_time = f"{start_time}:00"
end_time = __append_minutes(start_time, travel_time_minutes)
stop_times += f"\n{trip_id},{start_time},{start_time},{start_stop_point_id},1"
stop_times += f"\n{trip_id},{end_time},{end_time},{end_stop_point_id},2"
def add_trips(route_id, service_id,
start_stop_point_id, end_stop_point_id,
travel_time_minutes, start_times):
for start_time in start_times:
add_trip(route_id, service_id, start_stop_point_id,
end_stop_point_id, start_time, travel_time_minutes)
def add_fare_rule(route_id, origin_id, destination_id, price_eur):
global fare_rules, fare_attributes
fare_id = id()
fare_rules += f"\n{fare_id},{route_id},{origin_id},{destination_id}"
fare_attributes += f"\n{fare_id},{price_eur},EUR,1,0"
def parse_table(soup: BeautifulSoup) -> Dict[str, List[str]]:
table = soup.find("table", {"id": "route-table"})
headers = [th.text.strip() for th in table.find_all("th")]
schedule = {day: [] for day in headers}
for tr in table.find_all("tr"):
cells = tr.find_all("td")
if len(cells) == len(headers):
for i, cell in enumerate(cells):
time_text = cell.get_text(strip=True)
if time_text and time_text != "-":
schedule[headers[i]].append(time_text)
return schedule
def get_adult_price(soup: BeautifulSoup) -> float:
prices_div = soup.find("div", class_="route-tickets-col")
for price_div in prices_div.find_all("div", class_="price-div"):
person = price_div.find("p", class_="person").get_text(strip=True)
if "Adult" in person:
price_text = price_div.find(
"p", class_="price").get_text(strip=True)
return float(price_text.replace("€", "").replace(",", "."))
return 0.0
def parse_periods(soup: BeautifulSoup):
options = soup.find(
"select", {"name": "period_id"}).find_all("option")
periods = []
current_period = None
for option in options:
period = Period(option["value"], *option.text.split(" - "))
if option.has_attr("selected"):
current_period = period
else:
periods.append(period)
return periods, current_period
def add_trips_from_url(route_id,
start_stop_point_id, end_stop_point_id,
travel_time_minutes, url):
print(f"Fetching data from {url}")
session = requests.Session()
page = session.get(url)
soup = BeautifulSoup(page.content, 'html.parser')
_token = soup.find("input", {"name": "_token"})["value"]
periods, current_period = parse_periods(soup)
periods_schedule = [(current_period, parse_table(soup))]
print(f"Found period from {current_period.start_date} to {
current_period.end_date}")
for period in periods:
form_data = MultipartEncoder(
fields={
"_token": _token,
"period_id": str(period.id)
}
)
headers = {
"Content-Type": form_data.content_type
}
page = session.post(url, data=form_data, headers=headers)
soup = BeautifulSoup(page.content, 'html.parser')
periods_schedule.append((period, parse_table(soup)))
print(f"Found period from {period.start_date} to {period.end_date}")
for period, schedule in periods_schedule:
for day, start_times in schedule.items():
service_id = service(
int(day == "Monday"),
int(day == "Tuesday"),
int(day == "Wednesday"),
int(day == "Thursday"),
int(day == "Friday"),
int(day == "Saturday"),
int(day == "Sunday"),
period.start_date,
period.end_date
)
add_trips(route_id, service_id, start_stop_point_id,
end_stop_point_id, travel_time_minutes, start_times)
adult_price_eur = get_adult_price(soup)
add_fare_rule(route_id, start_stop_point_id,
end_stop_point_id, adult_price_eur)
print(f"Adult price: {adult_price_eur}€")
def generate():
"""
Create all txt files and zip it in a single file
"""
files = [
("agency.txt", agency),
("trips.txt", trips),
("stop_times.txt", stop_times),
("calendar.txt", calendar),
("stops.txt", stops),
("routes.txt", routes),
("fare_rules.txt", fare_rules),
("fare_attributes.txt", fare_attributes)
]
with zipfile.ZipFile("gtfs.zip", "w") as z:
for file_name, content in files:
with open(file_name, "w") as f:
f.write(content)
z.write(file_name)
os.remove(file_name)
if __name__ == "__main__":
larnaca_airport = stop("Kapnos - Larnaca Airport",
34.870561341994815, 33.606514350233944)
paphos_airport = stop("Kapnos - Paphos Airport",
34.71147673509841, 32.48893335140306)
nicosia = stop("Kapnos - Nicosia (Kyrenias Ave.)",
35.14871877404405, 33.37542849791872)
ayia_napa_nissi_ave_80 = stop(
"Kapnos - Ayia Napa (Nissi Ave.)", 34.989462262857344, 33.97755919456195)
protaras_ave_3 = stop("Kapnos - Protaras (Ave. 3)",
35.01772769511953, 34.04745892792459)
larnaca_nicosia_route = route(
"Larnaca - Nicosia", "https://kapnosairportshuttle.com/routes/4/en/1")
paphos_nicosia_route = route(
"Paphos - Nicosia", "https://kapnosairportshuttle.com/routes/11/en/1")
larnaca_paphos_route = route(
"Larnaca - Paphos", "https://kapnosairportshuttle.com/routes/6/en/1")
larnaca_niss_ave_route = route(
"Larnaca - Ayia Napa (Nissi Ave.)", "https://kapnosairportshuttle.com/routes/3/en/1")
larnaca_protaras = route("Larnaca - Protaras (Ave. 3)",
"https://kapnosairportshuttle.com/routes/8/en/1")
larnaca_hq_paphos = route("Larnaca (HQ) - Paphos",
"https://kapnosairportshuttle.com/routes/12/en/1")
add_trips_from_url(larnaca_nicosia_route, larnaca_airport,
nicosia, 40, "https://kapnosairportshuttle.com/routes/4/en/1")
add_trips_from_url(larnaca_nicosia_route, nicosia, larnaca_airport,
40, "https://kapnosairportshuttle.com/routes/5/en/1")
add_trips_from_url(paphos_nicosia_route, nicosia, paphos_airport,
100, "https://kapnosairportshuttle.com/routes/11/en/1")
add_trips_from_url(paphos_nicosia_route, paphos_airport, nicosia,
100, "https://kapnosairportshuttle.com/routes/10/en/1")
add_trips_from_url(larnaca_paphos_route, larnaca_airport, paphos_airport,
100, "https://kapnosairportshuttle.com/routes/6/en/1")
add_trips_from_url(larnaca_paphos_route, paphos_airport, larnaca_airport,
100, "https://kapnosairportshuttle.com/routes/7/en/1")
# TODO: vérifier si les urls affichent bien le tableau horaire ou si les routes ont été désactivées (redirection /)
# add_trips_from_url(larnaca_niss_ave_route, ayia_napa_nissi_ave_80,
# larnaca_airport, 40, "https://kapnosairportshuttle.com/routes/3/en/1")
# add_trips_from_url(larnaca_niss_ave_route, larnaca_airport, ayia_napa_nissi_ave_80,
# 40, "https://kapnosairportshuttle.com/routes/2/en/1")
# add_trips_from_url(larnaca_protaras, larnaca_airport, protaras_ave_3,
# 60, "https://kapnosairportshuttle.com/routes/8/en/1")
# add_trips_from_url(larnaca_protaras, protaras_ave_3, larnaca_airport,
# 60, "https://kapnosairportshuttle.com/routes/19/en/1")
add_trips_from_url(larnaca_hq_paphos, paphos_airport, larnaca_airport,
100, "https://kapnosairportshuttle.com/routes/12/en/1")
# TODO: https://kapnosairportshuttle.com/routes/15/en/1
# TODO: https://kapnosairportshuttle.com/routes/16/en/1
generate()