From e665b335d8b84938432e51d82979f0097347a45e Mon Sep 17 00:00:00 2001 From: nemesis Date: Tue, 25 Oct 2022 16:31:19 +0200 Subject: [PATCH] correction token RTE --- asmodee.py | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/asmodee.py b/asmodee.py index a9b04b6..4739784 100755 --- a/asmodee.py +++ b/asmodee.py @@ -25,7 +25,9 @@ import json bot = None PREFIX = '!' USERNAME = "" -oauth = None #Appels RTE +RTE_ID = None # Bot's RTE ID +RTE_SECRET = None # Bot's RTE SECRET + #ecowatt_url = 'https://digital.iservices.rte-france.com/open_api/ecowatt/v4/sandbox/signals' ecowatt_url = 'https://digital.iservices.rte-france.com/open_api/ecowatt/v4/signals' commune_url = 'https://geo.api.gouv.fr/communes?=&fields=nom,code,codesPostaux,population&format=json' @@ -1634,6 +1636,7 @@ async def ecowatt(room, message): global wattobj if (wattobj.jour < jn or (wattobj.jour == jn and wattobj.heure + 1 < hn)): try: + oauth = rte_token() result = oauth.get(ecowatt_url) watt = json.loads(result.text) except Exception as e: @@ -1694,6 +1697,7 @@ async def commune(room, message): cette_commune = commune_url.replace("", ville).replace("", "codePostal") else: cette_commune = commune_url.replace("", ville).replace("", "nom") + oauth = rte_token() result = oauth.get(cette_commune) villes = json.loads(result.text) if (len(villes) > 0): @@ -1730,6 +1734,7 @@ async def eau(room, message): cette_commune = eau_url.replace("", ville).replace("", "code_commune") else: cette_commune = eau_url.replace("", ville).replace("", "nom_commune") + oauth = rte_token() result = oauth.get(cette_commune) eaux = json.loads(result.text) if (len(eaux["data"]) > 0): @@ -2009,6 +2014,13 @@ async def callStats(room, message): if match.is_not_from_this_bot(): statistiques(room, message) +def rte_token(): + auth = HTTPBasicAuth(RTE_ID, RTE_SECRET) + client = BackendApplicationClient(client_id=RTE_ID) + oauth = OAuth2Session(client=client) + token = oauth.fetch_token(token_url='https://digital.iservices.rte-france.com/token/oauth/', auth=auth) + return(oauth) + def main(): config = configparser.ConfigParser() config.read('asmodee.ini') @@ -2025,20 +2037,16 @@ def main(): else: print("Probleme de lecture de configuration asmodee.ini (AUTH)") - RTE_ID = None # Bot's RTE ID - RTE_SECRET = None # Bot's RTE SECRET if ('RTE' in config): - global oauth + global RTE_ID + global RTE_SECRET global wattobj RTE_ID = config['RTE']['client_id'] # Bot's username RTE_SECRET = config['RTE']['client_secret'] # Bot's password - auth = HTTPBasicAuth(RTE_ID, RTE_SECRET) - client = BackendApplicationClient(client_id=RTE_ID) - oauth = OAuth2Session(client=client) - token = oauth.fetch_token(token_url='https://digital.iservices.rte-france.com/token/oauth/', auth=auth) wattobj = Wattobject() - else: + RTE_ID = None + RTE_SECRET = None print("Probleme de lecture de configuration asmodee.ini (RTE)") global liste_mod