Skip to content
This repository was archived by the owner on Dec 22, 2023. It is now read-only.

Commit 1582694

Browse files
committed
CO2 emission
1 parent ee13c82 commit 1582694

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# CO2 Emission
2+
3+
Scrape data from the CarbonIntensity API between two dates.
4+
5+
## Pre-requisites
6+
- Python 3
7+
- Requests library
8+
9+
## How to run
10+
Execute in your terminal:
11+
```
12+
python co2_emission.py
13+
```
14+
15+
### Author
16+
[Jr Miranda](https://github.com/jrmiranda)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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))
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
requests==2.24.0

0 commit comments

Comments
 (0)