101 lines
3.3 KiB
Python
Executable File
101 lines
3.3 KiB
Python
Executable File
from flask import Flask, render_template, flash, request
|
|
import requests
|
|
from requests import HTTPError
|
|
from wtforms import Form, StringField, validators
|
|
import json
|
|
|
|
app = Flask(__name__)
|
|
app.config['SECRET_KEY'] = '255771f2872f27567d441f2b62425'
|
|
|
|
|
|
def search_mp(product, postalcode, houseno, houseno_ext=''):
|
|
|
|
headers = {
|
|
'accept': 'application/json',
|
|
'X-API-Key': config['api_key'],
|
|
}
|
|
|
|
params = (
|
|
('product', product),
|
|
('streetNumber', houseno),
|
|
('streetNumberAddition', houseno_ext),
|
|
('postalCode', postalcode),
|
|
('limit', '50'),
|
|
('offset', '0'),
|
|
)
|
|
|
|
try:
|
|
response = requests.get(config['endpoint'], headers=headers, params=params)
|
|
response.raise_for_status()
|
|
except HTTPError as http_err:
|
|
print(f'HTTP error occurred: {http_err}')
|
|
print(response.status_code)
|
|
print(response.request.headers)
|
|
print(response.headers)
|
|
print(response.text)
|
|
except Exception as err:
|
|
print(f'Other error occurred: {err}')
|
|
print(response.text)
|
|
else:
|
|
print('Successfull GET Request')
|
|
return response.json()
|
|
|
|
def flash_results(results):
|
|
for mp in results['meteringPoints']:
|
|
print(mp['specialMeteringPoint'])
|
|
result = str(mp['address']['streetNumber']) + ';' + \
|
|
str(mp['address']['streetNumberAddition'] if 'streetNumberAddition' in mp['address'] else '') + ';' + \
|
|
str(mp['product']) + ';' + \
|
|
str(mp['ean']) + ';' + \
|
|
('Nee' if mp['specialMeteringPoint'] is False else 'Ja') + ';' + \
|
|
str(mp['organisation']) + ';'
|
|
flash(result, 'results')
|
|
return
|
|
|
|
def flash_address(results):
|
|
for mp in results['meteringPoints']:
|
|
print(mp['specialMeteringPoint'])
|
|
result = 'Gevonden resultaten voor: ' + \
|
|
str(mp['address']['postalCode']) + ' ' + \
|
|
str(mp['address']['street']) + ' ' + \
|
|
str(mp['address']['streetNumber']) + ' ' + \
|
|
str(mp['address']['city'])
|
|
flash(result, 'info')
|
|
break
|
|
return
|
|
|
|
class ReusableForm(Form):
|
|
postalcode = StringField('Postcode:', validators=[validators.InputRequired(), validators.Length(min=6, max=6)])
|
|
houseno = StringField('Huis nummer:', validators=[validators.InputRequired()])
|
|
houseno_ext = StringField('Toevoeging:', validators=[])
|
|
|
|
@app.route('/', methods=['GET', 'POST'])
|
|
def home():
|
|
form = ReusableForm(request.form)
|
|
|
|
if form.validate():
|
|
print(form.errors)
|
|
|
|
if request.method == 'POST':
|
|
postalcode = request.form['postalcode']
|
|
houseno = request.form['houseno']
|
|
houseno_ext = request.form['houseno_ext']
|
|
results = search_mp('ELK', postalcode, houseno, houseno_ext)
|
|
flash_address(results)
|
|
flash_results(results)
|
|
results = search_mp('GAS', postalcode, houseno, houseno_ext)
|
|
flash_results(results)
|
|
|
|
return render_template('index.html', form=form)
|
|
|
|
|
|
@app.route('/about/')
|
|
def about():
|
|
return render_template('about.html')
|
|
|
|
|
|
if __name__ == '__main__':
|
|
with open('config.json') as json_data:
|
|
config = json.load(json_data)
|
|
app.run(debug=True, host='0.0.0.0')
|