This commit is contained in:
Abel Fokkinga 2021-12-31 15:33:40 +01:00
commit f5ea5c533e
Signed by: abel
GPG Key ID: B84FADF9AF5B8D90
5 changed files with 110 additions and 99 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
venv

View File

@ -2,15 +2,17 @@ FROM ubuntu:20.04
MAINTAINER Abel Fokkinga "abel@fokkinga.com"
RUN apt-get update -y && \
apt-get install -y python-pip python-dev
RUN apt-get update && apt-get upgrade -y && apt-get autoremove && apt-get autoclean
RUN apt install git python3 python3-pip -y --no-install-recommends
RUN apt install locales -y --no-install-recommends
RUN locale-gen en_US.UTF-8
WORKDIR /app
RUN pip install flask, requests, wtforms, json
RUN pip install flask requests wtforms
COPY . /app
ENTRYPOINT [ "python" ]
ENTRYPOINT [ "python3" ]
CMD [ "main.py" ]

View File

@ -7,3 +7,11 @@ Go to https://gateway.edsn.nl/eancodeboek
## Update config.json
Update the API key token in the config.json file
## Docker
For building and running the app via Docker
```
docker build -t ecb:latest .
docker run -d -p 5000:5000 ecb
```

188
main.py
View File

@ -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')

View File

@ -35,7 +35,7 @@
</div>
<div class="results">
{% with messages = get_flashed_messages(with_categories=true) %}
{% if messages %}
{% if messages %}
<table class="results">
<tr><th>EAN Aansluiting</th><th>Product</th><th>Straatnaam</th><th>Huisnr.</th><th>Toev.</th><th>Woonplaats</th><th>Bijz. aansl.</th><th>Netbeheerder</th><th>EAN Netbeheerder</th></tr>
{% for category, message in messages %}