Added Docker configuration
This commit is contained in:
188
main.py
188
main.py
@@ -1,94 +1,94 @@
|
||||
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']:
|
||||
result = str(mp['ean']) + ';' + \
|
||||
str(mp['product']) + ';' + \
|
||||
str(mp['address']['street']) + ';' + \
|
||||
str(mp['address']['streetNumber']) + ';' + \
|
||||
str(mp['address']['streetNumberAddition']) + ';' + \
|
||||
str(mp['address']['city']) + ';' + \
|
||||
('Nee' if mp['specialMeteringPoint'] == 'False' else 'Ja') + ';' + \
|
||||
str(mp['organisation']) + ';' + \
|
||||
str(mp['gridOperatorEan'])
|
||||
flash(result, 'results')
|
||||
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)
|
||||
|
||||
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_results(results)
|
||||
results = search_mp('GAS', postalcode, houseno, houseno_ext)
|
||||
flash_results(results)
|
||||
|
||||
if form.validate():
|
||||
# Save the comment here.
|
||||
flash('Op zoek naar ' + postalcode + ' ' + houseno + ' ' + houseno_ext, 'info')
|
||||
else:
|
||||
flash('Postcode + huisnummer zijn verplicht.', 'info')
|
||||
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=False, host='0.0.0.0')
|
||||
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['ean']) + ';' + \
|
||||
str(mp['product']) + ';' + \
|
||||
str(mp['address']['street']) + ';' + \
|
||||
str(mp['address']['streetNumber']) + ';' + \
|
||||
str(mp['address']['streetNumberAddition'] if 'streetNumberAddition' in mp['address'] else '') + ';' + \
|
||||
str(mp['address']['city']) + ';' + \
|
||||
('Nee' if mp['specialMeteringPoint'] is False else 'Ja') + ';' + \
|
||||
str(mp['organisation']) + ';' + \
|
||||
str(mp['gridOperatorEan'])
|
||||
flash(result, 'results')
|
||||
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)
|
||||
|
||||
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_results(results)
|
||||
results = search_mp('GAS', postalcode, houseno, houseno_ext)
|
||||
flash_results(results)
|
||||
|
||||
if form.validate():
|
||||
# Save the comment here.
|
||||
flash('Op zoek naar ' + postalcode + ' ' + houseno + ' ' + houseno_ext, 'info')
|
||||
else:
|
||||
flash('Postcode + huisnummer zijn verplicht.', 'info')
|
||||
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')
|
||||
|
||||
Reference in New Issue
Block a user