From 83892c465f9199a6fec89d9472e364c5c635d349 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Y=C3=BBki=20Vachot?= Date: Mon, 18 Oct 2021 08:38:41 +0200 Subject: [PATCH] Update --- app.py | 65 ++++++++++++++++++++++++++++++++++++++++++------------- config.py | 10 ++++++--- 2 files changed, 57 insertions(+), 18 deletions(-) diff --git a/app.py b/app.py index 9a3c92a..9af2ef2 100644 --- a/app.py +++ b/app.py @@ -27,30 +27,65 @@ def hello_world(): # put application's code here return 'Hello World!' -# TODO Recherche d'une ville +# Recherche d'une ville @app.route('/searchCity', methods=['POST', 'GET']) def searchCity(): # put application's code here if request.method == 'POST': - city = request.form['search'] + search = request.form['search'] else: - city = request.args.get('search') - with closing(urlopen(API_LOCATION_CITIES + API_TOKEN + METEOCONCEPT_TOKEN + API_SEARCH + city)) as f: - cities = json.loads(f.read())['cities'] - return json.dumps(cities, indent=INDENT, sort_keys=True) + search = request.args.get('search') + + if METEOCONCEPT_TOKEN == -1: + log('Env variable METEOCONCEPT_TOKEN not passed') + return 'Env variable METEOCONCEPT_TOKEN not passed' + else: + log('Env variable METEOCONCEPT_TOKEN passed') + with closing(urlopen(API_LOCATION_CITIES + API_TOKEN + METEOCONCEPT_TOKEN + API_SEARCH + search)) as f: + cities = json.loads(f.read())['cities'] + return json.dumps(cities, indent=INDENT, sort_keys=True) + + +# Informations sur la Ville +@app.route('/city', methods=['POST', 'GET']) +def getCity(): # put application's code here + if request.method == 'POST': + insee = request.form['insee'] + else: + insee = request.args.get('insee') + + if METEOCONCEPT_TOKEN == -1: + log('Env variable METEOCONCEPT_TOKEN not passed') + return 'Env variable METEOCONCEPT_TOKEN not passed' + else: + log('Env variable METEOCONCEPT_TOKEN passed') + with closing(urlopen(API_LOCATION_CITY + API_TOKEN + METEOCONCEPT_TOKEN + API_INSEE + insee)) as f: + city = json.loads(f.read())['city'] + return json.dumps(city, indent=INDENT, sort_keys=True) + + +# TODO Information sur la ville et Ephéméride +@app.route('/ephemeride', methods=['POST', 'GET']) +def ephemeride(): # put application's code here + if request.method == 'POST': + insee = request.form['insee'] + else: + insee = request.args.get('insee') + + if METEOCONCEPT_TOKEN == -1: + log('Env variable METEOCONCEPT_TOKEN not passed') + return 'Env variable METEOCONCEPT_TOKEN not passed' + else: + log('Env variable METEOCONCEPT_TOKEN passed') + with closing(urlopen(API_EPHEMERIDE + API_TOKEN + METEOCONCEPT_TOKEN + API_INSEE + insee)) as f: + cityEph = json.loads(f.read()) + return json.dumps(cityEph, indent=INDENT, sort_keys=True) + # TODO Informations sur la Ville - -# TODO Information sur la ville et Ephéméride - if __name__ == '__main__': PORT = int(os.environ.get('PORT', 33507)) # On Linux or MAC 'export METEOCONCEPT_TOKEN=...' (check shell echo $METEOCONCEPT_TOKEN) # On Windows 'set METEOCONCEPT_TOKEN=...' (check on Powershell echo $Env:METEOCONCEPT_TOKEN) METEOCONCEPT_TOKEN = int(os.environ.get('METEOCONCEPT_TOKEN', -1)) - if METEOCONCEPT_TOKEN == -1: - log('Env variable METEOCONCEPT_TOKEN not passed') - sys.exit(0) - else: - log('Env variable METEOCONCEPT_TOKEN passed') - app.run(host='0.0.0.0', port=PORT, debug=True) + app.run(host='0.0.0.0', port=PORT, debug=True) \ No newline at end of file diff --git a/config.py b/config.py index d953ff3..adcfa1a 100644 --- a/config.py +++ b/config.py @@ -2,12 +2,16 @@ INDENT = 4 # API URL +BASE_API_URL = 'https://api.meteo-concept.com/api/' + API_TOKEN = '?token=' API_SEARCH = '&search=' +API_INSEE = '&insee=' -API_LOCATION_CITIES = 'https://api.meteo-concept.com/api/location/cities' -API_LOCATION_CITY = 'https://api.meteo-concept.com/api/location/city' -API_EPHEMERIDE = 'https://api.meteo-concept.com/api/ephemeride/1' +API_LOCATION_CITIES = BASE_API_URL + 'location/cities' +API_LOCATION_CITY = BASE_API_URL + 'location/city' +API_EPHEMERIDE = BASE_API_URL + 'ephemeride/1' +API_OBSERVATIONS_AROUND = BASE_API_URL + 'observations/around' # STATIC VARIABLES WINDDIRS = [