-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
104 lines (89 loc) · 3.49 KB
/
main.py
File metadata and controls
104 lines (89 loc) · 3.49 KB
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
from flask import Flask, render_template, request
import requests
import functions
app = Flask(__name__)
DEV_PORT = 2011
DEPLOY_PORT = 44751
HOST = "0.0.0.0"
DEV = False
if DEV == True:
print("[INFO] Starting In Development Mode")
PORT = DEV_PORT
else:
print("[INFO] Starting in Production Mode")
PORT = DEPLOY_PORT
# Define Endpoints for the different retailers
RETAILER_ENDPOINTS = {
"Applegreen UK": "https://applegreenstores.com/fuel-prices/data.json",
"Ascona Group": "https://fuelprices.asconagroup.co.uk/newfuel.json",
"Asda": "https://storelocator.asda.com/fuel_prices_data.json",
## "bp": "https://www.bp.com/en_gb/united-kingdom/home/fuelprices/fuel_prices_data.json",
"Esso Tesco Alliance": "https://fuelprices.esso.co.uk/latestdata.json",
"Morrisons": "https://www.morrisons.com/fuel-prices/fuel.json",
"Moto": "https://moto-way.com/fuel-price/fuel_prices.json",
"Motor Fuel Group": "https://fuel.motorfuelgroup.com/fuel_prices_data.json",
"Rontec": "https://www.rontec-servicestations.co.uk/fuel-prices/data/fuel_prices_data.json",
"Sainsbury’s": "https://api.sainsburys.co.uk/v1/exports/latest/fuel_prices_data.json",
"SGN": "https://www.sgnretail.uk/files/data/SGN_daily_fuel_prices.json",
}
@app.route("/")
def index():
outward_code = request.args.get('postcode', '').strip().upper()
aggregated_data = []
all_station_averages = [] # ✅ Properly initialized list
if outward_code == "":
print("[INFO] Loading Empty Page")
return render_template(
"index.html",
aggregated_data="",
outward_code="",
national_average=""
)
for retailer, url in RETAILER_ENDPOINTS.items():
try:
print(f"[INFO] Scanning Retailer: {retailer}")
response = requests.get(url, timeout=10)
response.raise_for_status()
data = response.json()
stations = functions.parse_stations(retailer, data, outward_code)
for station in stations:
avg = station.get("average_price")
if avg is not None:
all_station_averages.append(avg)
aggregated_data.append({
"retailer": retailer,
"data": stations
})
print("[INFO] Scan Complete")
except Exception as e:
aggregated_data.append({
"retailer": retailer,
"error": str(e)
})
print("[CRITICAL] Error: ",e)
national_average = None
if all_station_averages:
national_average = sum(all_station_averages) / len(all_station_averages)
return render_template(
"index.html",
aggregated_data=aggregated_data,
outward_code=outward_code,
national_average=national_average
)
@app.route("/map")
def map():
all_stations = []
for retailer, url in RETAILER_ENDPOINTS.items():
try:
response = requests.get(url, timeout=10)
response.raise_for_status()
data = response.json()
stations = functions.parse_stations(retailer, data, outward_code="")
for station in stations:
station["retailer"] = retailer
all_stations.extend(stations)
except Exception as e:
print(f"[WARN] Failed to fetch from {retailer}: {e}")
return render_template("map.html", all_stations=all_stations)
if __name__ == "__main__":
app.run(debug=True, port=PORT, host=HOST)