-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
67 lines (53 loc) · 2.34 KB
/
main.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
import csv
from lib import get_data
from locations import get_location
def init():
results = []
csv_header = ['company_name','contract_number', 'location_name', 'pickup_dt', 'dropoff_dt', 'car_type', 'total_price']
locations_fetched = {}
with open('input.csv', newline='') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
company_name = row['company_name']
contract_number = row['contract_number']
location_name = row['location_name']
pickup_dt = row['pickup_dt']
dropoff_dt = row['dropoff_dt']
print("Fetching - "+company_name+" at "+location_name+" from "+pickup_dt+
" - "+dropoff_dt)
if location_name in locations_fetched:
print("Getting location data from cache")
location = locations_fetched[location_name]
else:
print("Getting location data from API")
location = get_location(location_name)
locations_fetched[location_name] = location
if len(location['airports']) == 0:
print("Failed fetching location data")
else:
data = get_data(row, location['airports'][0])
if 'gbo' in data and \
'reservation' in data['gbo'] and \
'car_classes' in data['gbo']['reservation']:
print("Processing data for "+company_name)
car_classes = data['gbo']['reservation']['car_classes']
for cls in car_classes:
results.append([
company_name,
contract_number,
location_name,
pickup_dt,
dropoff_dt,
cls['name'],
cls['charges']['PAYLATER']['total_price_payment']['amount']
])
else:
print("Fetching failed for "+company_name)
with open('results.csv', 'w', encoding='UTF8', newline='') as f:
writer = csv.writer(f)
# write the header
writer.writerow(csv_header)
# write multiple rows
writer.writerows(results)
if __name__ == '__main__':
init()