-
Notifications
You must be signed in to change notification settings - Fork 0
/
footprint.py
134 lines (109 loc) · 4.82 KB
/
footprint.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
import os
import requests
SECRET_API_KEY = os.environ.get('CARBON_SECRET_KEY')
def get_all_vehicle_brands():
response = requests.get(url='https://www.carboninterface.com/api/v1/vehicle_makes', headers={
'Authorization': f'Bearer {SECRET_API_KEY}',
'Content-Type': 'application/json'
})
data = response.json()
VEHICLE_BRAND_NAMES = []
for brand in data:
id = brand['data']['id']
name = brand['data']['attributes']['name']
VEHICLE_BRAND_NAMES.append((id, name))
# https://stackoverflow.com/questions/8966538/syntax-behind-sortedkey-lambda
VEHICLE_BRAND_NAMES = sorted(VEHICLE_BRAND_NAMES, key=lambda tup: tup[1])
# https://stackoverflow.com/questions/8966538/syntax-behind-sortedkey-lambda
return VEHICLE_BRAND_NAMES
units = {
"g": "carbon_g",
"kg": "carbon_kg",
"lbs": "carbon_lb",
"mt(metric tonnes)": "carbon_mt"
}
def get_vehicle_estimate(distance_value, distance_unit, vehicle_model_id, emission_unit):
url = 'https://www.carboninterface.com/api/v1/estimates'
headers = {'Authorization': f'Bearer {SECRET_API_KEY}', 'Content-Type': 'application/json'}
data = {
'type': 'vehicle',
"distance_unit": f"{distance_unit}",
"distance_value": f'{distance_value}',
"vehicle_model_id": f'{vehicle_model_id}'
}
res = requests.post(url, headers=headers, json=data)
data = res.json()
for key in units.keys():
if key == emission_unit:
carbon_unit = units[key]
return data['data']['attributes'][f'{carbon_unit}']
def get_vehicle_estimate(distance_value, distance_unit, vehicle_model_id, emission_unit):
url = 'https://www.carboninterface.com/api/v1/estimates'
headers = {'Authorization': f'Bearer {SECRET_API_KEY}', 'Content-Type': 'application/json'}
data = {
'type': 'vehicle',
"distance_unit": f"{distance_unit}",
"distance_value": f'{distance_value}',
"vehicle_model_id": f'{vehicle_model_id}'
}
res = requests.post(url, headers=headers, json=data)
data = res.json()
for key in units.keys():
if key == emission_unit:
carbon_unit = units[key]
return data['data']['attributes'][f'{carbon_unit}']
def get_shipping_estimate(weight_unit, weight_value, distance_unit, distance_value, transport_method, emission_unit):
url = 'https://www.carboninterface.com/api/v1/estimates'
headers = {'Authorization': 'Bearer {SECRET_API_KEY}', 'Content-Type': 'application/json'}
data = {
"type": "shipping",
"weight_value": f"{weight_value}",
"weight_unit": f"{weight_unit}",
"distance_value": f'{distance_value}',
"distance_unit": f"{distance_unit}",
"transport_method": f"{transport_method}"
}
res = requests.post(url, headers=headers, json=data)
data = res.json()
for key in units.keys():
if key == emission_unit:
carbon_unit = units[key]
return data['data']['attributes'][f'{carbon_unit}']
def get_flight_estimate(distance_unit, distance_value, emission_unit):
avg_lbs_per_mile = 53
km_in_mile = 1.6
kg_in_lb = .45
g_in_lb = 453.59
mt_in_lb = .0005
mi_in_km = 0.621
if distance_unit == "mi" and emission_unit == 'lbs':
return distance_value * avg_lbs_per_mile
elif distance_unit == 'mi' and emission_unit == 'kg':
return distance_value * avg_lbs_per_mile * kg_in_lb
elif distance_unit == 'mi' and emission_unit == 'g':
return distance_value * avg_lbs_per_mile * g_in_lb
elif distance_unit == 'mi' and emission_unit == 'mt(metric tonnes)':
return distance_value * avg_lbs_per_mile * mt_in_lb
elif distance_unit == "km" and emission_unit == 'lbs':
return distance_value * avg_lbs_per_mile * mi_in_km
elif distance_unit == 'km' and emission_unit == 'kg':
return distance_value * avg_lbs_per_mile * kg_in_lb * mi_in_km
elif distance_unit == 'km' and emission_unit == 'g':
return distance_value * avg_lbs_per_mile * g_in_lb * mi_in_km
else:
return distance_value * avg_lbs_per_mile * mt_in_lb * mi_in_km
def get_electricity_estimate(electricity_value, electricity_unit, country, emission_unit):
url = 'https://www.carboninterface.com/api/v1/estimates'
headers = {'Authorization': f'Bearer {SECRET_API_KEY}', 'Content-Type': 'application/json'}
data = {
"type": "electricity",
"electricity_unit": f"{electricity_unit}",
"electricity_value": f"{electricity_value}",
"country": f"{country}"
}
res = requests.post(url, headers=headers, json=data)
result = res.json()
for key in units.keys():
if key == emission_unit:
carbon_unit = units[key]
return result['data']['attributes'][f'{carbon_unit}']