This repository has been archived by the owner on Aug 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_data.py
248 lines (177 loc) · 5.63 KB
/
get_data.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
"""
Download new data from an MDS provider and load it to db.
"""
from datetime import datetime
import pytz
import logging
import pdb
import _setpath
import argutil
from mds_provider_client import *
import requests
from pypgrest import Postgrest
from config import secrets
from config import config
def build_client_params(
cfg,
keys=[
"auth_type",
"delay",
"headers",
"url",
"timeout",
"token",
"user",
"password",
],
):
# package config elems that need to be passed to mds_provider_client
return {key: cfg[key] for key in keys if key in cfg}
def get_token(url, data):
res = requests.post(url, data=data)
res.raise_for_status()
return res.json()
def most_recent(client, provider_id, key="end_time"):
"""
Return the most recent trip record for the given provider
"""
results = client.select(
{
"select": f"{key}",
"provider_id": f"eq.{provider_id}",
"limit": 1,
"order": f"{key}.desc",
}
)
if results:
return results[0].get(key)
else:
return "2018-12-01T00:00:00"
def cli_args():
parser = argutil.get_parser(
"mds.py",
"Extract data from MDS endpoint and load to staging database.",
"provider_name",
"--start",
"--end",
"--replace",
)
args = parser.parse_args()
return args
def get_data(client, end_time, interval, paging):
data = client.get_trips(
start_time=end_time - interval, end_time=end_time, paging=paging
)
return data
def get_coords(feature):
if feature["geometry"]["coordinates"]:
return feature["geometry"]["coordinates"]
else:
# some provider data has an empty coordinates element
return [None, None]
def parse_routes(trips):
for trip in trips:
if not trip.get("route"):
# some provider data is missing a route element
trip["start_longitude"], trip["start_latitude"] = [0, 0]
trip["end_longitude"], trip["end_latitude"] = [0, 0]
continue
if not trip["route"].get("features"):
# some provider data has an empty route element
trip["start_longitude"], trip["start_latitude"] = [0, 0]
trip["end_longitude"], trip["end_latitude"] = [0, 0]
continue
trip["start_longitude"], trip["start_latitude"] = get_coords(
trip["route"]["features"][0]
)
trip["end_longitude"], trip["end_latitude"] = get_coords(
trip["route"]["features"][-1]
)
trip.pop("route")
return trips
def to_unix(iso_string, tz_string="+00:00"):
# handle format YYYY-MM-DDTHH:MM:SS+00:00 (timezone ignored)
iso_string = iso_string.replace(tz_string, "")
dt = datetime.strptime(iso_string, "%Y-%m-%dT%H:%M:%S").replace(tzinfo=pytz.UTC)
return int(dt.timestamp())
def to_iso(unix):
if unix < 4_100_264_520:
return datetime.utcfromtimestamp(int(unix)).strftime("%Y-%m-%dT%H:%M:%SZ")
else:
# try milliseconds instead
return datetime.utcfromtimestamp(int(unix / 1000)).strftime(
"%Y-%m-%dT%H:%M:%SZ"
)
def floats_to_iso(data, keys):
for row in data:
for key in keys:
row[key] = to_iso(row[key])
return data
def drop_dupes(trips, key="trip_id"):
ids = []
new_trips = []
for trip in trips:
if trip[key] not in ids:
ids.append(trip[key])
new_trips.append(trip)
else:
print(trip[key])
return new_trips
def post_data(client, data):
print("Post {} trips...".format(len(data)))
client.upsert(data)
return data
def main():
args = cli_args()
provider_name = args.provider_name
cfg = secrets.PROVIDERS[provider_name]
pgrest = Postgrest(secrets.PG["url"], auth=secrets.PG["token"])
start = args.start
end = args.end
offset = cfg["time_offset_seconds"]
if not start:
# use the most recent record as the start date (minus the offset)
start = most_recent(pgrest, cfg["provider_id"])
start = to_unix(start)
start = start - offset
if not end:
# use current time as the end date
end = int(datetime.today().timestamp())
interval = cfg["interval"]
if cfg.get("time_format") == "mills":
# mills to unix
start, end, interval = int(start * 1000), int(end * 1000), int(interval * 1000)
auth_type = cfg.get("auth_type")
if not cfg.get("token") and auth_type.lower() != "httpbasicauth":
token_res = get_token(cfg["auth_url"], cfg["auth_data"])
cfg["token"] = token_res[cfg["auth_token_res_key"]]
client_params = build_client_params(cfg)
client = ProviderClient(**client_params)
total = 0
for i in range(start, end, interval):
data = get_data(client, i, interval, cfg["paging"])
print(start)
if data:
data = parse_routes(data)
data = drop_dupes(data)
data = [
{
field["name"]: row[field["name"]]
for field in config.FIELDS
if field.get("upload_mds")
}
for row in data
]
data = floats_to_iso(
data,
[field["name"] for field in config.FIELDS if field.get("datetime")],
)
post_data(pgrest, data)
total += len(data)
else:
continue
return total
if __name__ == "__main__":
import logging
logging.basicConfig(level=logging.DEBUG)
main()