This repository has been archived by the owner on Nov 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
187 lines (143 loc) · 5.37 KB
/
app.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#!/usr/bin/env python3
from flask import Flask, render_template, request, redirect, url_for, g, send_file, Response
from datetime import datetime, timezone
from os.path import isfile, join
from dotenv import load_dotenv
from pathlib import Path
from os import listdir
import requests
import sqlite3
import hashlib
load_dotenv()
app = Flask(__name__)
script_path = Path(__file__).parent.absolute()
static_folder = script_path.joinpath("./static/")
icon_folder = static_folder.joinpath("./images/icons")
icon_data = [f.replace(".avif", "") for f in listdir(icon_folder) if isfile(join(icon_folder, f))]
def get_station_count() -> int:
tmp_db = sqlite3.connect("./data/aral.db")
tmp_cursor = tmp_db.cursor()
tmp_station_count = tmp_cursor.execute("SELECT COUNT(*) FROM stations;").fetchone()[0]
tmp_cursor.close()
tmp_db.close()
return tmp_station_count
station_count = get_station_count()
def get_db():
db = getattr(g, "_database", None)
if db is None:
db = g._database = sqlite3.connect("./data/aral.db")
return db
@app.teardown_appcontext
def close_connection(exception):
db = getattr(g, "_database", None)
if db is not None:
db.close()
@app.template_filter("euro")
def euro(value):
value = float(value) / 100
return "{:,.2f} €".format(value)
@app.template_filter("strftime")
def _jinja2_filter_datetime(date):
# Parse the input date string into a datetime object
date = datetime.strptime(date, "%Y-%m-%d %H:%M:%S")
date = date.replace(tzinfo=timezone.utc).astimezone(tz=None)
# Format the datetime as a string
return date.strftime("%H:%M")
@app.route("/sitemap.xml", methods=["GET"])
def sitemap():
return send_file("./data/sitemap.xml", max_age=43200, etag=True)
@app.route("/robots.txt", methods=["GET"])
def robots():
return send_file("./static/robots.txt", max_age=86400, etag=True)
@app.route("/", methods=["GET"])
def index():
return render_template("index.html", station_count=station_count)
def is_it_true(value):
return value.lower() == "true"
@app.route("/map", methods=["GET"])
def display_map():
lat = request.args.get("lat", default=51, type=float)
lng = request.args.get("lng", default=11, type=float)
zoom = request.args.get("zoom", default=6, type=int)
disable_control = request.args.get(
"disable_control", default=False, type=is_it_true
)
tmp_station_id = request.args.get("station", type=int)
cursor = get_db().cursor()
if tmp_station_id:
tmp_stations = cursor.execute(
"SELECT * FROM stations WHERE id = ?;", (tmp_station_id,)
).fetchall()
bounds = cursor.execute(
"SELECT MIN(lat)-1, MIN(lng)-1, MAX(lat)+1, MAX(lng)+1 FROM stations WHERE id = ?;",
(tmp_station_id,),
).fetchone()
else:
tmp_stations = cursor.execute("SELECT * FROM stations;").fetchall()
bounds = cursor.execute(
"SELECT MIN(lat)-1, MIN(lng)-1, MAX(lat)+1, MAX(lng)+1 FROM stations;"
).fetchone()
tmp_response = Response(response=render_template(
"map.html",
tmp_stations=tmp_stations,
bounds=bounds,
lat=lat,
lng=lng,
zoom=zoom,
disable_control=disable_control,
))
tmp_response.set_etag(hashlib.sha1(f"{lat}{lng}{zoom}{disable_control}{tmp_station_id}".encode("utf-8")).hexdigest())
return tmp_response
@app.route("/raw/search", methods=["GET"])
def search():
search_term = request.args.get("search")
if not search_term:
return ""
if len(search_term) < 4:
return ""
search_term = search_term.lower()
cursor = get_db().cursor()
return_stations = cursor.execute(
"SELECT * FROM stations WHERE LOWER(name) LIKE ? OR LOWER(city) LIKE ? OR LOWER(postcode) LIKE ?",
(
f"%{search_term}%",
f"%{search_term}%",
f"%{search_term}%",
),
).fetchall()
tmp_response = Response(response=render_template("raw_search.html", stations=return_stations))
tmp_response.set_etag(hashlib.sha1(f"{search_term}".encode("utf-8")).hexdigest())
return tmp_response
@app.route("/station/<int:station_id>", methods=["GET"])
def station(station_id):
if not station_id:
return redirect(url_for("index"))
cursor = get_db().cursor()
local_station_data = cursor.execute(
"SELECT postcode, city, name, lat, lng FROM stations WHERE id = ?;",
(station_id,),
).fetchone()
if not local_station_data:
return redirect(url_for("index"))
try:
station_data = requests.get(
f"https://api.tankstelle.aral.de/api/v2/stations/{station_id}/prices"
).json()
except TimeoutError:
print("api/we are offline, returning empty data")
station_data = []
tmp_response = Response(response=render_template(
"station.html",
local_station_data=local_station_data,
station_data=station_data,
station_id=station_id,
icon_data=icon_data,
))
local_e_tag = "".join(map(str, local_station_data))
station_e_tag = "".join(map(str, station_data))
tmp_response.set_etag(hashlib.sha1(f"{local_e_tag}{station_e_tag}{station_id}".encode("utf-8")).hexdigest())
return tmp_response
if __name__ == "__main__":
# during deploy waitress is used like this:
# waitress-serve --host 127.0.0.1 --port 5000 app:app
app.run(debug=True)