diff --git a/api_fonction.py b/api_fonction.py index 44711f3..c12c198 100644 --- a/api_fonction.py +++ b/api_fonction.py @@ -2,15 +2,21 @@ from config import * from contextlib import closing from urllib.request import urlopen import json +import sys -def getSearch(query): +def log(*args): + print(args[0] % (len(args) > 1 and args[1:] or [])) + sys.stdout.flush() + + +def getSearch(query, METEOCONCEPT_TOKEN, WEATHERSTACK_TOKEN): if METEOCONCEPT_TOKEN is None and WEATHERSTACK_TOKEN is None: log('Env variable METEOCONCEPT_TOKEN and WEATHERSTACK_TOKEN not passed') return 'Env variable METEOCONCEPT_TOKEN and WEATHERSTACK_TOKEN not passed' elif query is None: - log('GET/POST search variable not passed') - return 'GET/POST search variable not passed' + log('GET/POST query variable not passed') + return 'GET/POST query variable not passed' else: log('Env variable METEOCONCEPT_TOKEN or/and WEATHERSTACK_TOKEN passed') with closing(urlopen( @@ -34,7 +40,7 @@ def getSearch(query): } -def getCurrent(query): +def getCurrent(query, METEOCONCEPT_TOKEN, WEATHERSTACK_TOKEN): if METEOCONCEPT_TOKEN is None and WEATHERSTACK_TOKEN is None: log('Env variable METEOCONCEPT_TOKEN and WEATHERSTACK_TOKEN not passed') return 'Env variable METEOCONCEPT_TOKEN and WEATHERSTACK_TOKEN not passed' @@ -43,23 +49,23 @@ def getCurrent(query): return 'GET/POST query variable not passed' else: log('Env variable METEOCONCEPT_TOKEN or/and WEATHERSTACK_TOKEN passed') - try: - query = int(query) + if query.isnumeric(): + insee = int(query) with closing(urlopen( API_OBSERVATIONS_AROUND_METEOCONCEPT + API_TOKEN_METEOCONCEPT + METEOCONCEPT_TOKEN + API_INSEE_METEOCONCEPT + - str(query) + + str(insee) + API_RADIUS_METEOCONCEPT + '0')) as f: - around_METEOCONCEPT = json.loads(f.read()) - return {"query": query, "around": { + around_METEOCONCEPT = json.loads(f.read())[0] + return {"query": insee, "current": { "METEOCONCEPT": around_METEOCONCEPT, "WEATHERSTACK": None - } } - except: + } + else: with closing(urlopen( API_OBSERVATIONS_AROUND_WEATHERSTACK + API_TOKEN_WEATHERSTACK + @@ -67,38 +73,47 @@ def getCurrent(query): API_SEARCH_WEATHERSTACK + query + ",France")) as f: around_WEATHERSTACK = json.loads(f.read()) - return {"query": query, "around": { + return {"query": query, "current": { "METEOCONCEPT": None, "WEATHERSTACK": around_WEATHERSTACK - } } + } +def getCity(query, METEOCONCEPT_TOKEN, WEATHERSTACK_TOKEN): + if METEOCONCEPT_TOKEN is None and WEATHERSTACK_TOKEN is None: + log('Env variable METEOCONCEPT_TOKEN and WEATHERSTACK_TOKEN not passed') + return 'Env variable METEOCONCEPT_TOKEN and WEATHERSTACK_TOKEN not passed' + elif query is None: + log('GET/POST query variable not passed') + return 'GET/POST query variable not passed' + else: + log('Env variable METEOCONCEPT_TOKEN or/and WEATHERSTACK_TOKEN passed') + if query.isnumeric(): + insee = int(query) + with closing(urlopen( + API_LOCATION_CITY_METEOCONCEPT + + API_TOKEN_METEOCONCEPT + + METEOCONCEPT_TOKEN + + API_INSEE_METEOCONCEPT + + str(insee))) as f: + city_METEOCONCEPT = json.loads(f.read())['city'] -# def getEphemeride(insee): -# if METEOCONCEPT_TOKEN is None: -# log('Env variable METEOCONCEPT_TOKEN not passed') -# return 'Env variable METEOCONCEPT_TOKEN not passed' -# elif insee is None: -# log('GET/POST insee variable not passed') -# return 'GET/POST insee variable 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 [insee, cityEph] -# -# -# def getCity(insee): -# if METEOCONCEPT_TOKEN is None and WEATHERSTACK_TOKEN is None: -# log('Env variable METEOCONCEPT_TOKEN and WEATHERSTACK_TOKEN not passed') -# return 'Env variable METEOCONCEPT_TOKEN and WEATHERSTACK_TOKEN not passed' -# elif search is None: -# log('GET/POST search variable not passed') -# return 'GET/POST search variable not passed' -# else: -# log('Env variable METEOCONCEPT_TOKEN or/and WEATHERSTACK_TOKEN passed') -# -# with closing(urlopen(API_LOCATION_CITY + API_TOKEN + METEOCONCEPT_TOKEN + API_INSEE + insee)) as f: -# city = json.loads(f.read())['city'] -# return [insee, city] + return {"query": insee, "city": { + "METEOCONCEPT": city_METEOCONCEPT, + "WEATHERSTACK": None + } + } + else: + with closing(urlopen( + API_OBSERVATIONS_AROUND_WEATHERSTACK + + API_TOKEN_WEATHERSTACK + + WEATHERSTACK_TOKEN + + API_SEARCH_WEATHERSTACK + + query + ",France")) as f: + city_WEATHERSTACK = json.loads(f.read())['location'] + return {"query": query, "city": { + "METEOCONCEPT": None, + "WEATHERSTACK": city_WEATHERSTACK + } + } \ No newline at end of file diff --git a/app.py b/app.py index facf9e8..031e06a 100644 --- a/app.py +++ b/app.py @@ -1,12 +1,15 @@ import os -from config import * from flask_route import * if __name__ == '__main__': PORT = int(os.environ.get('PORT', 33507)) + + METEOCONCEPT_TOKEN = None if app.config['ENV'] == 'development' else "e93ee091ba7cd6ce1882fb55f9c030bbbafa0defaa0d8dd4f780b00897b989e1" + WEATHERSTACK_TOKEN = None if app.config['ENV'] == 'development' else "e43f819bbbf50da109d7076840cd3f11" + # 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 = os.environ.get('METEOCONCEPT_TOKEN', METEOCONCEPT_TOKEN) - WEATHERSTACK_TOKEN = os.environ.get('WEATHERSTACK_TOKEN', WEATHERSTACK_TOKEN) + app.config['METEOCONCEPT_TOKEN'] = os.environ.get('METEOCONCEPT_TOKEN', METEOCONCEPT_TOKEN) + app.config['WEATHERSTACK_TOKEN'] = os.environ.get('WEATHERSTACK_TOKEN', WEATHERSTACK_TOKEN) app.config['SECRET_KEY'] = 'secret_key' 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 61e4c0a..c84aa8e 100644 --- a/config.py +++ b/config.py @@ -1,15 +1,9 @@ -import sys -def log(*args): - print(args[0] % (len(args) > 1 and args[1:] or [])) - sys.stdout.flush() - # JSON DUMPS JSON_PRETTYFIER = True INDENT = 4 # METEOCONCEPT API URL BASE_API_URL_METEOCONCEPT = 'https://api.meteo-concept.com/api/' -METEOCONCEPT_TOKEN = "e93ee091ba7cd6ce1882fb55f9c030bbbafa0defaa0d8dd4f780b00897b989e1" API_TOKEN_METEOCONCEPT = '?token=' API_SEARCH_METEOCONCEPT = '&search=' API_INSEE_METEOCONCEPT = '&insee=' @@ -131,7 +125,6 @@ WEATHER_METEOCONCEPT = { # WEATHERSTACK API URL BASE_API_URL_WEATHERSTACK = 'http://api.weatherstack.com/' -WEATHERSTACK_TOKEN = "e43f819bbbf50da109d7076840cd3f11" API_TOKEN_WEATHERSTACK = '?access_key=' API_SEARCH_WEATHERSTACK = '&query=' API_LOCATION_CITIES_WEATHERSTACK = BASE_API_URL_WEATHERSTACK + 'autocomplete' diff --git a/flask_route.py b/flask_route.py index f05947e..df6da70 100644 --- a/flask_route.py +++ b/flask_route.py @@ -1,10 +1,9 @@ -from flask import Flask, request, render_template -from config import * -from forms import * +from flask import Flask, request from api_fonction import * app = Flask(__name__) + def app_response(results): if JSON_PRETTYFIER: json_results = json.dumps(results, indent=INDENT, sort_keys=True) @@ -18,6 +17,7 @@ def app_response(results): response.headers['Access-Control-Allow-Origin'] = '*' return response + @app.route('/config') def config(): # put application's code here return app_response({}) @@ -30,7 +30,7 @@ def search(): # put application's code here query = request.form['query'] else: query = request.args.get('query') - return app_response(getSearch(query)) + return app_response(getSearch(query, app.config['METEOCONCEPT_TOKEN'], app.config['WEATHERSTACK_TOKEN'])) # Information sur les alentours d'une ville @@ -40,26 +40,17 @@ def current(): # put application's code here query = request.form['query'] else: query = request.args.get('query') - return app_response(getCurrent(query)) + return app_response(getCurrent(query, app.config['METEOCONCEPT_TOKEN'], app.config['WEATHERSTACK_TOKEN'])) -# # 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') -# return app_response(json.dumps(getCity(insee), indent=INDENT, sort_keys=True)) -# -# -# # 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') -# return app_response(json.dumps(getEphemeride(insee), indent=INDENT, sort_keys=True)) + +# Informations sur la Ville +@app.route('/city', methods=['POST', 'GET']) +def city(): # put application's code here + if request.method == 'POST': + query = request.form['query'] + else: + query = request.args.get('query') + return app_response(getCity(query, app.config['METEOCONCEPT_TOKEN'], app.config['WEATHERSTACK_TOKEN'])) # @app.route('/', methods=['POST', 'GET'])