Skip to content

Fetch CO2 emission information #3182

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Oct 11, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Fetch CO2 emission information
  • Loading branch information
silvaan committed Oct 11, 2020
commit 6383fbde1ae7e16e33e8999734a8a8cfa4718853
26 changes: 26 additions & 0 deletions web_programming/co2_emission.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"""
Get CO2 emission data from CarbonIntensity API
"""
import requests
import datetime

BASE_URL = 'https://api.carbonintensity.org.uk/intensity'
DATE_FORMAT = '%Y-%m-%d'

# Emission in the last half hour
def fetch_last_half_hour():
data = requests.get(BASE_URL).json()
actual_intensity = data['data'][0]['intensity']['actual']
return actual_intensity

# Emissions in a specific date range
def fetch_from_to(start, end):
data = requests.get(f'{BASE_URL}/{start}/{end}').json()
for entry in data['data']:
print('from ' + entry['from'] + ' to ' + entry['to'] + ': ' + str(entry['intensity']['actual']) + '\n')

if __name__ == '__main__':
start = datetime.date(2020, 10, 1)
end = datetime.date(2020, 10, 3)

fetch_from_to(start, end)