-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.py
More file actions
64 lines (56 loc) · 2.08 KB
/
functions.py
File metadata and controls
64 lines (56 loc) · 2.08 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
def filter_by_outward_code(stations, outward_code):
if not outward_code:
return stations
filtered = []
for s in stations:
postcode = s.get("postcode", "")
if postcode.upper().startswith(outward_code.upper()):
filtered.append(s)
return filtered
def parse_stations(retailer, data, outward_code):
result = []
stations = data.get("stations") or data.get("data") or data
if not isinstance(stations, list):
return result
for station in stations:
postcode = station.get("postcode", "").upper()
if postcode.startswith(outward_code.upper()):
address = (
station.get("address") or
station.get("addr") or
station.get("location") or
""
).strip()
if address:
if retailer == "bp":
retailer = retailer.upper()
name = f"{retailer} {address}"
else:
name = f"{retailer} Station "
prices = station.get("prices") or {
"E10": station.get("E10"),
"E5": station.get("E5"),
"B7": station.get("B7"),
"Diesel": station.get("diesel"),
"Unleaded": station.get("unleaded")
}
prices = {k: v for k, v in prices.items() if v is not None}
if prices:
avg_price = sum(prices.values()) / len(prices)
else:
avg_price = None
loc = station.get("location") or {}
latitude = loc.get("latitude") or loc.get("lat")
longitude = loc.get("longitude") or loc.get("lng") or loc.get("lat")
result.append({
"name": name,
"postcode": postcode,
"prices": prices,
"average_price": avg_price,
"location": {
"latitude": latitude,
"longitude": longitude
}
})
print("[INFO] Completed Station: ", name)
return result