Update for Datalore Partie 2

This commit is contained in:
Yûki VACHOT 2021-11-23 21:50:01 +01:00
parent 7621e940ef
commit ef26c987a2
4 changed files with 75 additions and 73 deletions

View file

@ -2,15 +2,21 @@ from config import *
from contextlib import closing from contextlib import closing
from urllib.request import urlopen from urllib.request import urlopen
import json 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: if METEOCONCEPT_TOKEN is None and WEATHERSTACK_TOKEN is None:
log('Env variable METEOCONCEPT_TOKEN and WEATHERSTACK_TOKEN not passed') log('Env variable METEOCONCEPT_TOKEN and WEATHERSTACK_TOKEN not passed')
return 'Env variable METEOCONCEPT_TOKEN and WEATHERSTACK_TOKEN not passed' return 'Env variable METEOCONCEPT_TOKEN and WEATHERSTACK_TOKEN not passed'
elif query is None: elif query is None:
log('GET/POST search variable not passed') log('GET/POST query variable not passed')
return 'GET/POST search variable not passed' return 'GET/POST query variable not passed'
else: else:
log('Env variable METEOCONCEPT_TOKEN or/and WEATHERSTACK_TOKEN passed') log('Env variable METEOCONCEPT_TOKEN or/and WEATHERSTACK_TOKEN passed')
with closing(urlopen( 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: if METEOCONCEPT_TOKEN is None and WEATHERSTACK_TOKEN is None:
log('Env variable METEOCONCEPT_TOKEN and WEATHERSTACK_TOKEN not passed') log('Env variable METEOCONCEPT_TOKEN and WEATHERSTACK_TOKEN not passed')
return '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' return 'GET/POST query variable not passed'
else: else:
log('Env variable METEOCONCEPT_TOKEN or/and WEATHERSTACK_TOKEN passed') log('Env variable METEOCONCEPT_TOKEN or/and WEATHERSTACK_TOKEN passed')
try: if query.isnumeric():
query = int(query) insee = int(query)
with closing(urlopen( with closing(urlopen(
API_OBSERVATIONS_AROUND_METEOCONCEPT + API_OBSERVATIONS_AROUND_METEOCONCEPT +
API_TOKEN_METEOCONCEPT + API_TOKEN_METEOCONCEPT +
METEOCONCEPT_TOKEN + METEOCONCEPT_TOKEN +
API_INSEE_METEOCONCEPT + API_INSEE_METEOCONCEPT +
str(query) + str(insee) +
API_RADIUS_METEOCONCEPT + API_RADIUS_METEOCONCEPT +
'0')) as f: '0')) as f:
around_METEOCONCEPT = json.loads(f.read()) around_METEOCONCEPT = json.loads(f.read())[0]
return {"query": query, "around": { return {"query": insee, "current": {
"METEOCONCEPT": around_METEOCONCEPT, "METEOCONCEPT": around_METEOCONCEPT,
"WEATHERSTACK": None "WEATHERSTACK": None
}
} }
except: }
else:
with closing(urlopen( with closing(urlopen(
API_OBSERVATIONS_AROUND_WEATHERSTACK + API_OBSERVATIONS_AROUND_WEATHERSTACK +
API_TOKEN_WEATHERSTACK + API_TOKEN_WEATHERSTACK +
@ -67,38 +73,47 @@ def getCurrent(query):
API_SEARCH_WEATHERSTACK + API_SEARCH_WEATHERSTACK +
query + ",France")) as f: query + ",France")) as f:
around_WEATHERSTACK = json.loads(f.read()) around_WEATHERSTACK = json.loads(f.read())
return {"query": query, "around": { return {"query": query, "current": {
"METEOCONCEPT": None, "METEOCONCEPT": None,
"WEATHERSTACK": around_WEATHERSTACK "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): return {"query": insee, "city": {
# if METEOCONCEPT_TOKEN is None: "METEOCONCEPT": city_METEOCONCEPT,
# log('Env variable METEOCONCEPT_TOKEN not passed') "WEATHERSTACK": None
# return 'Env variable METEOCONCEPT_TOKEN not passed' }
# elif insee is None: }
# log('GET/POST insee variable not passed') else:
# return 'GET/POST insee variable not passed' with closing(urlopen(
# else: API_OBSERVATIONS_AROUND_WEATHERSTACK +
# log('Env variable METEOCONCEPT_TOKEN passed') API_TOKEN_WEATHERSTACK +
# with closing(urlopen(API_EPHEMERIDE + API_TOKEN + METEOCONCEPT_TOKEN + API_INSEE + insee)) as f: WEATHERSTACK_TOKEN +
# cityEph = json.loads(f.read()) API_SEARCH_WEATHERSTACK +
# return [insee, cityEph] query + ",France")) as f:
# city_WEATHERSTACK = json.loads(f.read())['location']
# return {"query": query, "city": {
# def getCity(insee): "METEOCONCEPT": None,
# if METEOCONCEPT_TOKEN is None and WEATHERSTACK_TOKEN is None: "WEATHERSTACK": city_WEATHERSTACK
# 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]

9
app.py
View file

@ -1,12 +1,15 @@
import os import os
from config import *
from flask_route import * from flask_route import *
if __name__ == '__main__': if __name__ == '__main__':
PORT = int(os.environ.get('PORT', 33507)) 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 Linux or MAC 'export METEOCONCEPT_TOKEN=...' (check shell echo $METEOCONCEPT_TOKEN)
# On Windows 'set METEOCONCEPT_TOKEN=...' (check on Powershell echo $Env:METEOCONCEPT_TOKEN) # On Windows 'set METEOCONCEPT_TOKEN=...' (check on Powershell echo $Env:METEOCONCEPT_TOKEN)
METEOCONCEPT_TOKEN = os.environ.get('METEOCONCEPT_TOKEN', METEOCONCEPT_TOKEN) app.config['METEOCONCEPT_TOKEN'] = os.environ.get('METEOCONCEPT_TOKEN', METEOCONCEPT_TOKEN)
WEATHERSTACK_TOKEN = os.environ.get('WEATHERSTACK_TOKEN', WEATHERSTACK_TOKEN) app.config['WEATHERSTACK_TOKEN'] = os.environ.get('WEATHERSTACK_TOKEN', WEATHERSTACK_TOKEN)
app.config['SECRET_KEY'] = 'secret_key' app.config['SECRET_KEY'] = 'secret_key'
app.run(host='0.0.0.0', port=PORT, debug=True) app.run(host='0.0.0.0', port=PORT, debug=True)

View file

@ -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 DUMPS
JSON_PRETTYFIER = True JSON_PRETTYFIER = True
INDENT = 4 INDENT = 4
# METEOCONCEPT API URL # METEOCONCEPT API URL
BASE_API_URL_METEOCONCEPT = 'https://api.meteo-concept.com/api/' BASE_API_URL_METEOCONCEPT = 'https://api.meteo-concept.com/api/'
METEOCONCEPT_TOKEN = "e93ee091ba7cd6ce1882fb55f9c030bbbafa0defaa0d8dd4f780b00897b989e1"
API_TOKEN_METEOCONCEPT = '?token=' API_TOKEN_METEOCONCEPT = '?token='
API_SEARCH_METEOCONCEPT = '&search=' API_SEARCH_METEOCONCEPT = '&search='
API_INSEE_METEOCONCEPT = '&insee=' API_INSEE_METEOCONCEPT = '&insee='
@ -131,7 +125,6 @@ WEATHER_METEOCONCEPT = {
# WEATHERSTACK API URL # WEATHERSTACK API URL
BASE_API_URL_WEATHERSTACK = 'http://api.weatherstack.com/' BASE_API_URL_WEATHERSTACK = 'http://api.weatherstack.com/'
WEATHERSTACK_TOKEN = "e43f819bbbf50da109d7076840cd3f11"
API_TOKEN_WEATHERSTACK = '?access_key=' API_TOKEN_WEATHERSTACK = '?access_key='
API_SEARCH_WEATHERSTACK = '&query=' API_SEARCH_WEATHERSTACK = '&query='
API_LOCATION_CITIES_WEATHERSTACK = BASE_API_URL_WEATHERSTACK + 'autocomplete' API_LOCATION_CITIES_WEATHERSTACK = BASE_API_URL_WEATHERSTACK + 'autocomplete'

View file

@ -1,10 +1,9 @@
from flask import Flask, request, render_template from flask import Flask, request
from config import *
from forms import *
from api_fonction import * from api_fonction import *
app = Flask(__name__) app = Flask(__name__)
def app_response(results): def app_response(results):
if JSON_PRETTYFIER: if JSON_PRETTYFIER:
json_results = json.dumps(results, indent=INDENT, sort_keys=True) json_results = json.dumps(results, indent=INDENT, sort_keys=True)
@ -18,6 +17,7 @@ def app_response(results):
response.headers['Access-Control-Allow-Origin'] = '*' response.headers['Access-Control-Allow-Origin'] = '*'
return response return response
@app.route('/config') @app.route('/config')
def config(): # put application's code here def config(): # put application's code here
return app_response({}) return app_response({})
@ -30,7 +30,7 @@ def search(): # put application's code here
query = request.form['query'] query = request.form['query']
else: else:
query = request.args.get('query') 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 # Information sur les alentours d'une ville
@ -40,26 +40,17 @@ def current(): # put application's code here
query = request.form['query'] query = request.form['query']
else: else:
query = request.args.get('query') 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']) # Informations sur la Ville
# def getCity(): # put application's code here @app.route('/city', methods=['POST', 'GET'])
# if request.method == 'POST': def city(): # put application's code here
# insee = request.form['insee'] if request.method == 'POST':
# else: query = request.form['query']
# insee = request.args.get('insee') else:
# return app_response(json.dumps(getCity(insee), indent=INDENT, sort_keys=True)) query = request.args.get('query')
# return app_response(getCity(query, app.config['METEOCONCEPT_TOKEN'], app.config['WEATHERSTACK_TOKEN']))
#
# # 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))
# @app.route('/', methods=['POST', 'GET']) # @app.route('/', methods=['POST', 'GET'])