-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommon_functions.py
138 lines (110 loc) · 4.45 KB
/
common_functions.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
import json
from pathlib import Path
from urllib.request import urlopen, Request
import mysql.connector
"""
Following header is necessary for requesting golemio api.
code copied from https://golemioapi.docs.apiary.io/#reference/public-transport/vehicle-positions/get-all-vehicle-positions
access token generated by https://api.golemio.cz/api-keys/auth/sign-in
"""
headers = {
'Content-Type': 'application/json; charset=utf-8',
'x-access-token': 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6ImNpem1hcmZpbGlwQGdtYWlsLmNvbSIsImlkIjo3NiwibmFtZSI6bnVsbCwic3VybmFtZSI6bnVsbCwiaWF0IjoxNTcwNTQ2MTU2LCJleHAiOjExNTcwNTQ2MTU2LCJpc3MiOiJnb2xlbWlvIiwianRpIjoiMzAxYWNhNDUtNGRlNC00ZDRmLWI4NzAtMzQwMDQ5OTM1MzBhIn0.4rCELzCNY8XOSvjqQA7cKocPGJ8D2ezhXiWUkIRUNjg'
}
"""
This function downloads file of given url request and returns decoded json object
"""
class allFilesURL:
prefix = Path("..")
data = Path("data")
trips_shapes = prefix / data / "trips"
vehicles_positions = prefix / data / "veh_act_pos"
log_server = prefix / "server.log"
def downloadURL(url: str, header: dict = None) -> dict:
if header is None:
header = headers
request = Request(url, headers = header)
webURL = urlopen(request)
response_body = webURL.read()
encoding = webURL.info().get_content_charset('utf-8')
return json.loads(response_body.decode(encoding))
class GI:
type = "type"
trip = "trip"
shapes = "shapes"
feature = "feature"
features = "features"
geometry = "geometry"
properties = "properties"
lineString = "LineString"
coordinates = "coordinates"
gtfs_trip_id = "gtfs_trip_id"
featureCollection = "FeatureCollection"
shape_dist_traveled = "shape_dist_traveled"
gtfs_route_short_name = "gtfs_route_short_name"
class SQL_queries:
S_id_trip_F_trips = 'SELECT id_trip FROM trips WHERE trip_id = %s LIMIT 1'
S_id_trip_F_coor = 'SELECT `time` FROM trip_coordinates WHERE id_trip = %s ORDER BY `time` DESC LIMIT 1'
insert_headsign = 'SELECT insert_headsign_if_exists_and_return_id(%s)'
insert_trip = 'SELECT insert_trip_and_return_id (%s, %s, %s, %s, %s)'
S_id_stop_F_stops = 'SELECT id_stop FROM stops WHERE stop_id = %s'
insert_stop = 'SELECT insert_stop_and_return_id (%s, %s, %s, %s, NULL)'
insert_ride = 'INSERT INTO rides (id_trip, id_stop, arrival_time, departure_time, shape_dist_traveled) VALUES (%s, %s, %s, %s, %s)'
up_trip = 'UPDATE trips SET current_delay = %s, shape_traveled = %s WHERE id_trip = %s'
up_trip_coo = 'INSERT INTO trip_coordinates (id_trip, lat, lon, time, delay, shape_traveled) VALUES (%s, %s, %s, %s, %s, %s)'
@staticmethod
def sql_get_result(cursor, sql_query, params=()):
try:
cursor.execute(sql_query, params)
return cursor.fetchall()
except Exception as e:
print(e)
print("Query failed", sql_query, "params:", params)
@staticmethod
def sql_run_transaction(connection, cursor, sql_query, params=()):
try:
cursor.executemany(sql_query, params)
connection.commit()
except mysql.connector.errors.IntegrityError as e:
print(e)
print("Query failed", sql_query, "params:", params)
except Exception as e:
print(e)
print("Query failed", sql_query, "params:", params)
@staticmethod
def sql_run_transaction_and_fetch(connection, cursor, sql_query, params=()):
try:
cursor.execute_fetchall(sql_query, params)
ret = cursor.fetchall()
connection.commit()
return ret
except mysql.connector.errors.IntegrityError as e:
print(e)
print("Query failed", sql_query, "params:", params)
except Exception as e:
print(e)
print("Query failed", sql_query, "params:", params)
class URLs:
vehicles_positions = 'https://api.golemio.cz/v1/vehiclepositions' #TODO limit
# stops = 'https://api.golemio.cz/v1/gtfs/stops'
trips = 'https://api.golemio.cz/v1/gtfs/trips'
@staticmethod
def stops(limit: int = 10000, offset: int = 0):
return 'https://api.golemio.cz/v1/gtfs/stops' + '?limit=' + str(limit) + '&offset=' + str(offset)
@staticmethod
def trip_by_id(trip_id: str) -> str:
return 'https://api.golemio.cz/v1/gtfs/trips/' + trip_id + '?includeShapes=true&includeStopTimes=true&includeStops=true'
class Log:
start = "Program has started."
net_err = "Network error: "
sleep = "Sleep failed, "
io_err = "Write to file failed: "
vpf_up = "Vehicle positions file updated"
stops_up = "Stops are updating"
stop_nf = "Stop was not found in stops file"
@staticmethod
def new_trip(trip_id):
return "New trip " + trip_id + " found"
@staticmethod
def new_shape(trip_id):
return "New shape of trip " + trip_id + " file exported"