Skip to content

Commit 6f9d55d

Browse files
silvaancclauss
authored andcommitted
Fetch CO2 emission information (TheAlgorithms#3182)
* Fetch CO2 emission information * fix blank lines * fix blank lines * Add type hints for function return values * Update co2_emission.py * Update co2_emission.py * Update co2_emission.py Co-authored-by: Christian Clauss <[email protected]>
1 parent 087007e commit 6f9d55d

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

web_programming/co2_emission.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"""
2+
Get CO2 emission data from the UK CarbonIntensity API
3+
"""
4+
from datetime import date
5+
6+
import requests
7+
8+
BASE_URL = "https://api.carbonintensity.org.uk/intensity"
9+
10+
11+
# Emission in the last half hour
12+
def fetch_last_half_hour() -> str:
13+
last_half_hour = requests.get(BASE_URL).json()["data"][0]
14+
return last_half_hour["intensity"]["actual"]
15+
16+
17+
# Emissions in a specific date range
18+
def fetch_from_to(start, end) -> list:
19+
return requests.get(f"{BASE_URL}/{start}/{end}").json()["data"]
20+
21+
22+
if __name__ == "__main__":
23+
for entry in fetch_from_to(start=date(2020, 10, 1), end=date(2020, 10, 3)):
24+
print("from {from} to {to}: {intensity[actual]}".format(**entry))
25+
print(f"{fetch_last_half_hour() = }")

0 commit comments

Comments
 (0)