generated from projeto-de-algoritmos/RepositorioTemplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_populate_database.py
114 lines (87 loc) · 3.5 KB
/
create_populate_database.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
import os
import requests
origin_cities = [("15", "Samambaia"), ("16", "Recanto das Emas")]
destiny_city = ("25", "Plano Piloto")
base_url_bus_routes_id = "https://www.sistemas.dftrans.df.gov.br/linha/ra/"
bus_routes_id = {}
base_url_schedules_bus_routes = (
"https://www.sistemas.dftrans.df.gov.br/horario/linha/numero/"
)
def populate_schedules_bus_routes():
for path in bus_routes_id:
data_archive_path = "./database/schedules_bus_routes/" + path + '/'
os.mkdir(data_archive_path)
for bus_route_id in bus_routes_id[path]:
response = requests.get(
base_url_schedules_bus_routes + bus_route_id
).json()
data_archive = open(data_archive_path + bus_route_id + ".txt", 'w')
for schedules_data in response:
if (
schedules_data["duracaoMedia"] and
schedules_data["horarios"] and (
schedules_data["sentido"] == "CIRCULAR" or
schedules_data["sentido"] == "IDA"
)
):
data_archive.writelines(
str(schedules_data["duracaoMedia"])
)
data_archive.close()
data = ""
for schedule_data in schedules_data["horarios"]:
if (
schedule_data["diasLabel"] == "SEG_SEX" and (
schedule_data["sentido"] == "CIRCULAR" or
schedule_data["sentido"] == "IDA"
)
):
data_archive = open(
data_archive_path + bus_route_id + ".txt", 'r'
)
data = data_archive.readlines()
data.append(',' + str(schedule_data["horario"]))
data_archive = open(
data_archive_path + bus_route_id + ".txt", 'w'
)
data_archive.writelines(data)
data_archive.close()
def populate_bus_routes_id():
for origin_city in origin_cities:
response = requests.get(
base_url_bus_routes_id + origin_city[0] + "/ra/" + destiny_city[0]
).json()
data = ""
path = origin_city[0] + '_' + destiny_city[0]
i = 0
for bus_route in response:
if (
bus_route["numero"] and (
bus_route["sentido"] == "CIRCULAR" or
bus_route["sentido"] == "IDA"
)
):
bus_route_id = bus_route["numero"]
if i == 0:
i = 1
data = bus_route_id
bus_routes_id[path] = [bus_route_id]
else:
data = data + ',' + bus_route_id
bus_routes_id[path].append(bus_route_id)
data_archive = open("./database/bus_routes_id/" + path + ".txt", 'w')
data_archive.writelines(data)
data_archive.close()
def popula_database():
populate_bus_routes_id()
populate_schedules_bus_routes()
def create_database():
os.mkdir("./database/")
os.mkdir("./database/bus_routes_id/")
os.mkdir("./database/schedules_bus_routes/")
def main():
create_database()
popula_database()
if __name__ == "__main__":
main()
exit()