Created
April 28, 2019 08:06
-
-
Save oktal/476e56db1bafe1ad432a6810b0c8026d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding=utf-8 | |
import discord | |
import logging | |
import aiohttp | |
CO2_API = 'http://www.hqcasanova.com/co2/' | |
IPCC_REPORTS = { | |
'ar5': { | |
'wg1': { | |
'url': 'https://www.ipcc.ch/report/ar5/wg1/', | |
'title': 'Climate Change 2013: The Pysical Science Basis', | |
'description': 'The Working Group I contribution to the Fifth Assessment Report of the Intergovernmental' | |
' Panel on Climate Change (IPCC) provides a comprehensive assessment of the physical' | |
' science basis of climate change since 2007 when the Fourth Assessment Report (AR4) was' | |
' released.' | |
}, | |
'wg2': { | |
'url': 'https://www.ipcc.ch/report/ar5/wg2/', | |
'title': 'Climate Change 2014: Impacts, Adaptation, and Vulnerability', | |
'description': 'The assessment of impacts, adaptation, and vulnerability in the Working Group II' | |
' contribution to the IPCC’s Fifth Assessment Report (WGII AR5) evaluates how patterns' | |
' of risks and potential benefits are shifting due to climate change since 2007 when' | |
' the Fourth Assessment Report (AR4) was released.' | |
}, | |
'wg3': { | |
'url': 'https://www.ipcc.ch/report/ar5/wg3/', | |
'title': 'Climate Change 2014: Mitigation of Climate Change', | |
'description': 'The Working Group III contribution to the IPCC’s Fifth Assessment Report (AR5) assesses' | |
' literature on the scientific, technological, environmental, economic and social aspects' | |
' of mitigation of climate change since 2007 when the Fourth Assessment Report (AR4) was' | |
' released.' | |
} | |
}, | |
'sr15': { | |
'url': 'https://www.ipcc.ch/sr15/', | |
'title': 'Global Warming of 1.5 ºC', | |
'description': 'An IPCC special report on the impacts of global warming of 1.5 °C above pre-industrial levels' | |
' and related global greenhouse gas emission pathways, in the context of strengthening the global' | |
' response to the threat of climate change, sustainable development, and efforts to eradicate' | |
' poverty.' | |
} | |
} | |
logging.basicConfig(level=logging.DEBUG) | |
logger = logging.getLogger() | |
client = discord.Client() | |
async def fetch(session, url): | |
async with session.get(url) as response: | |
return await response.text() | |
async def retrieve_co2(): | |
async with aiohttp.ClientSession() as session: | |
return await fetch(session, CO2_API) | |
async def fetch_ipcc_report(message, report_name, report_group): | |
if report_name not in IPCC_REPORTS: | |
await message.channel.send('Rapport {} inconnu ({}).'.format(report_name, IPCC_REPORTS.keys())) | |
return | |
report = IPCC_REPORTS[report_name] | |
if report_group is None: | |
report_info = report | |
else: | |
if report_group not in report: | |
await message.channel.send('Rapport {} inconnu ({})'.format(report_group, report.keys())) | |
return | |
report_info = report[report_group] | |
url = report_info['url'] | |
title = report_info['title'] | |
description = report_info['description'] | |
embed = discord.Embed(title=title, url=url, description=description, type="rich") | |
await message.channel.send('Voici le rapport.', embed=embed) | |
@client.event | |
async def on_ready(): | |
print("Successfully logged-in as {0.user}".format(client)) | |
@client.event | |
async def on_message(message): | |
if message.author == client.user: | |
return | |
message_parts = message.content.split() | |
command = message_parts[0] | |
if command == '$co2': | |
co2_level = await retrieve_co2() | |
embed=discord.Embed(title="https://www.esrl.noaa.gov/gmd/ccgg/trends/monthly.html", | |
url="https://www.esrl.noaa.gov/gmd/ccgg/trends/monthly.html", | |
description="Mauna Loa CO2", | |
type="rich") | |
embed.set_image(url="https://www.esrl.noaa.gov/gmd/webdata/ccgg/trends/co2_data_mlo.png") | |
await message.channel.send('Le niveau actuel de CO2 dans l\'atmosphere est de {0}'.format(co2_level), | |
embed=embed) | |
elif command == '$ipcc' or command == '$giec': | |
if len(message_parts) < 2: | |
return | |
report_name = message_parts[1] | |
report_group = message_parts[2] if len(message_parts) > 2 else None | |
await fetch_ipcc_report(message, report_name, report_group) | |
client.run('') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment