Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Dec 22, 2023. It is now read-only.

Commit 8a628e6

Browse files
Merge pull request #620 from jrmiranda/master
CO2 emission
2 parents c2cc8c5 + 26f7f6e commit 8a628e6

File tree

4 files changed

+43
-0
lines changed

4 files changed

+43
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
![Screenshot](screenshot.png)
16+
17+
### Author
18+
[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
266 KB
Loading[フレーム]

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /