Update
This commit is contained in:
parent
f7eaf8e018
commit
20f5cc55ac
3 changed files with 110 additions and 85 deletions
154
app.py
154
app.py
|
|
@ -15,40 +15,6 @@ def log(*args):
|
|||
|
||||
|
||||
def getApiCity(search):
|
||||
with closing(urlopen(API_LOCATION_CITIES + API_TOKEN + METEOCONCEPT_TOKEN + API_SEARCH + search)) as f:
|
||||
cities = json.loads(f.read())['cities']
|
||||
return cities
|
||||
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
|
||||
|
||||
@app.route('/config')
|
||||
def config(): # put application's code here
|
||||
return str(INDENT) + str(WEATHER) + str(WINDDIRS)
|
||||
|
||||
|
||||
@app.route('/', methods=['POST', 'GET'])
|
||||
def index():
|
||||
response = False
|
||||
if request.method == 'POST':
|
||||
search = request.form['search']
|
||||
if search is None:
|
||||
response = []
|
||||
else:
|
||||
response = getApiCity(search)
|
||||
return render_template('index.html', response=response)
|
||||
|
||||
|
||||
# Recherche d'une ville
|
||||
@app.route('/searchCity', methods=['POST', 'GET'])
|
||||
def searchCity(): # put application's code here
|
||||
if request.method == 'POST':
|
||||
search = request.form['search']
|
||||
else:
|
||||
search = request.args.get('search')
|
||||
|
||||
if METEOCONCEPT_TOKEN is None:
|
||||
log('Env variable METEOCONCEPT_TOKEN not passed')
|
||||
return 'Env variable METEOCONCEPT_TOKEN not passed'
|
||||
|
|
@ -57,38 +23,28 @@ def searchCity(): # put application's code here
|
|||
return 'GET/POST search variable not passed'
|
||||
else:
|
||||
log('Env variable METEOCONCEPT_TOKEN passed')
|
||||
return json.dumps(getApiCity(search), indent=INDENT, sort_keys=True)
|
||||
with closing(urlopen(API_LOCATION_CITIES + API_TOKEN + METEOCONCEPT_TOKEN + API_SEARCH + search)) as f:
|
||||
cities = json.loads(f.read())['cities']
|
||||
return [search, cities]
|
||||
|
||||
|
||||
# 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')
|
||||
|
||||
def getAround(insee, radius):
|
||||
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'
|
||||
elif insee is None or radius is None:
|
||||
log('GET/POST insee or radius variable not passed')
|
||||
return 'GET/POST insee or radiu variable 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)
|
||||
with closing(
|
||||
urlopen(
|
||||
API_OBSERVATIONS_AROUND + API_TOKEN + METEOCONCEPT_TOKEN + API_INSEE + insee + API_RADIUS + radius)) as f:
|
||||
around = json.loads(f.read())
|
||||
return [insee, radius, around]
|
||||
|
||||
|
||||
# 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')
|
||||
|
||||
def getEphemeride(insee):
|
||||
if METEOCONCEPT_TOKEN is None:
|
||||
log('Env variable METEOCONCEPT_TOKEN not passed')
|
||||
return 'Env variable METEOCONCEPT_TOKEN not passed'
|
||||
|
|
@ -99,7 +55,76 @@ def ephemeride(): # put application's code here
|
|||
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)
|
||||
return [insee, cityEph]
|
||||
|
||||
|
||||
def getCity(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_LOCATION_CITY + API_TOKEN + METEOCONCEPT_TOKEN + API_INSEE + insee)) as f:
|
||||
city = json.loads(f.read())['city']
|
||||
return [insee, city]
|
||||
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
|
||||
@app.route('/config')
|
||||
def config(): # put application's code here
|
||||
return str(INDENT) + str(WEATHER) + str(WINDDIRS)
|
||||
|
||||
|
||||
@app.route('/', methods=['POST', 'GET'])
|
||||
def index():
|
||||
response_cities = False
|
||||
response_ephemeride = False
|
||||
response_around = False
|
||||
if request.method == 'POST':
|
||||
search = request.form['search']
|
||||
response_cities = getApiCity(search)
|
||||
|
||||
# TODO Multiple form for insee ? or ajax ?
|
||||
# insee = request.form['insee']
|
||||
# response_ephemeride = getEphemeride(insee)
|
||||
# response_around = getAround(insee, 1)
|
||||
|
||||
return render_template('index.html', cities=response_cities, ephemeride=response_ephemeride, around=response_around)
|
||||
|
||||
|
||||
# Recherche d'une ville
|
||||
@app.route('/searchCity', methods=['POST', 'GET'])
|
||||
def searchCity(): # put application's code here
|
||||
if request.method == 'POST':
|
||||
search = request.form['search']
|
||||
else:
|
||||
search = request.args.get('search')
|
||||
return json.dumps(getApiCity(search), 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')
|
||||
return 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 json.dumps(getEphemeride(insee), indent=INDENT, sort_keys=True)
|
||||
|
||||
|
||||
# Information sur les alentours d'une ville
|
||||
|
|
@ -111,18 +136,7 @@ def around(): # put application's code here
|
|||
else:
|
||||
insee = request.args.get('insee')
|
||||
radius = request.args.get('radius')
|
||||
|
||||
if METEOCONCEPT_TOKEN is None:
|
||||
log('Env variable METEOCONCEPT_TOKEN not passed')
|
||||
return 'Env variable METEOCONCEPT_TOKEN not passed'
|
||||
elif insee is None or radius is None:
|
||||
log('GET/POST insee or radius variable not passed')
|
||||
return 'GET/POST insee or radiu variable not passed'
|
||||
else:
|
||||
log('Env variable METEOCONCEPT_TOKEN passed')
|
||||
with closing(urlopen(API_OBSERVATIONS_AROUND + API_TOKEN + METEOCONCEPT_TOKEN + API_INSEE + insee + API_RADIUS + radius)) as f:
|
||||
around = json.loads(f.read())
|
||||
return json.dumps(around, indent=INDENT, sort_keys=True)
|
||||
return json.dumps(getAround(insee, radius), indent=INDENT, sort_keys=True)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
|
|
|||
|
|
@ -20,6 +20,8 @@ li{
|
|||
}
|
||||
|
||||
.response{
|
||||
|
||||
}
|
||||
|
||||
.error{
|
||||
color: red;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,23 +7,32 @@
|
|||
</head>
|
||||
<body>
|
||||
<div class="meteoAPI">
|
||||
<h1>WebServices : MétéoAPI</h1>
|
||||
<h1>WebServices : MétéoAPI</h1>
|
||||
<form method="POST">
|
||||
<label>Rechercher une ville : </label><input name="search"> <input type="submit" value="Search"></p>
|
||||
<label>Rechercher une ville : </label><input name="search" value="{{cities[0]}}"> <input type="submit" value="Search"></p>
|
||||
</form>
|
||||
<div class="response">
|
||||
<br><br>
|
||||
{% if response %}
|
||||
<h2>Results</h2>
|
||||
{% if cities[1]|length == 0 %}
|
||||
<p class="error">No results for {{cities[0]}}</p>
|
||||
{% endif %}
|
||||
{% if cities and cities[1] and cities[1]|length > 0 %}
|
||||
<form method="POST">
|
||||
<h3> Select city ({{cities[1]|length}} found for {{cities[0]}}) :
|
||||
<select name="insee" method="POST">
|
||||
<label>Rechercher une ville : </label>{% if cities[1]|length > 0 %} <option value="{{cities[1][0]['name']}}" selected>{{cities[1][0]['name']}} ({{cities[1][0]['cp']}})</option> {% endif %}
|
||||
{% for city in cities[1][1:] %}
|
||||
<option value="{{city['insee']}}">{{city['name']}} ({{city['cp']}})</option>
|
||||
{% endfor %}
|
||||
</select></h3>
|
||||
</form>
|
||||
{% endif %}
|
||||
{% if ephemeride %}
|
||||
<div class="ephemeride">
|
||||
<br><br>
|
||||
<ul>
|
||||
{% for city in response %}
|
||||
<ul>
|
||||
<li>{{city}}</li>
|
||||
</ul>
|
||||
{%endfor%}
|
||||
{{ ephemeride[1] }}
|
||||
</ul>
|
||||
{% endif %}
|
||||
</div>
|
||||
</div>
|
||||
{% endif %}
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
Loading…
Add table
Add a link
Reference in a new issue