-
Notifications
You must be signed in to change notification settings - Fork 0
/
flask_interface.py
88 lines (69 loc) · 2.8 KB
/
flask_interface.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/python3.5
# -*- coding: utf-8 -*-
from flask import Flask, request, jsonify, render_template, send_file
import regiojet
import json
from datetime import datetime
from threading import Thread
from time import sleep
app = Flask(__name__)
app.config['PROPAGATE_EXCEPTIONS'] = True
config_data = {}
def __config_loader(file_name):
global config_data
while True:
with open(file_name) as data_file:
config_data = json.load(data_file)
sleep(5)
def __save_data(data):
import psycopg2
import psycopg2.extras as pg2
conn = psycopg2.connect(**config_data['db_config'])
cur = conn.cursor(cursor_factory=pg2.DictCursor)
for data_row in data:
cur.execute(
'INSERT INTO connections_tomas_prochazka(departure,arrival,src,dst,free_seats,price) VALUES(\'{departure}\',\'{arrival}\',\'{src}\',\'{dst}\',{free_seats},{price})'
.format(departure=data_row['departure'], arrival=data_row['arrival'], src=data_row['from'],
dst=data_row['to'], free_seats=data_row['free_seats'], price=data_row['price']))
conn.commit()
@app.route('/<filename>.png', methods=['GET'])
def get_image(filename):
return send_file(filename + '.png', mimetype='image/png')
@app.route('/search', methods=['GET'])
def search():
'''
Vyhledávání spojení, vrací stránku
'''
search_result = []
try:
from_ = request.args.get('from')
to_ = request.args.get('to')
if {from_, to_} & set(config_data['ban_list']):
raise Exception('Město {} je na banlistu'.format(set([from_, to_]) & set(config_data['ban_list'])))
date_from = datetime.strptime(request.args.get('date_from'), '%Y-%m-%d').date()
date_to = datetime.strptime(request.args.get('date_to'), '%Y-%m-%d').date()
search_result = regiojet.search(from_, to_, date_from, date_to)
except TypeError:
raise
print('Missing variables, return page with empty list')
except Exception as ex:
return render_template('error_template.jinja2', error_mesage=str(ex))
__save_data(search_result)
return render_template('template.jinja2', result_list=search_result)
@app.route('/search-json', methods=['GET'])
def search_json():
'''
Vyhledávání spojení, vrací JSON
'''
from_ = request.args.get('from')
to_ = request.args.get('to')
date_from = datetime.strptime(request.args.get('date_from'), '%Y-%m-%d').date()
date_to = datetime.strptime(request.args.get('date_to'), '%Y-%m-%d').date()
search_result = regiojet.search(from_, to_, date_from, date_to)
if search_result is None:
return 'Nenalezeno'
return jsonify(search_result)
if __name__ == '__main__':
t = Thread(target=__config_loader, args=('config.json',))
t.start()
app.run(debug=True)