forked from TheAlgorithms/Python
- Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathco2_emission.py
26 lines (17 loc) · 741 Bytes
/
co2_emission.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
"""
Get CO2 emission data from the UK CarbonIntensity API
"""
fromdatetimeimportdate
importrequests
BASE_URL="https://api.carbonintensity.org.uk/intensity"
# Emission in the last half hour
deffetch_last_half_hour() ->str:
last_half_hour=requests.get(BASE_URL, timeout=10).json()["data"][0]
returnlast_half_hour["intensity"]["actual"]
# Emissions in a specific date range
deffetch_from_to(start, end) ->list:
returnrequests.get(f"{BASE_URL}/{start}/{end}", timeout=10).json()["data"]
if__name__=="__main__":
forentryinfetch_from_to(start=date(2020, 10, 1), end=date(2020, 10, 3)):
print("from {from} to {to}: {intensity[actual]}".format(**entry))
print(f"{fetch_last_half_hour() =}")