-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFuelPriceAggregator.py
More file actions
63 lines (57 loc) · 2.43 KB
/
Copy pathFuelPriceAggregator.py
File metadata and controls
63 lines (57 loc) · 2.43 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
import requests
class FuelPriceAggregator:
PROVIDERS = {
"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": "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",
"motorfuelgroup": "https://fuel.motorfuelgroup.com/fuel_prices_data.json",
"rontec": "https://www.rontec-servicestations.co.uk/fuel-prices/data/fuel_prices_data.json",
"sainsburys": "https://api.sainsburys.co.uk/v1/exports/latest/fuel_prices_data.json",
"sgn": "https://www.sgnretail.uk/files/data/SGN_daily_fuel_prices.json",
"shell": "https://www.shell.co.uk/fuel-prices-data.html",
"tesco": "https://www.tesco.com/fuel_prices/fuel_prices_data.json",
}
async def fetch_all(self):
results = []
for provider, url in self.PROVIDERS.items():
try:
r = requests.get(url)
r.raise_for_status()
data = r.json()
normalize_func = getattr(self, f"normalize_{provider}", self.default_normalize)
normalized = normalize_func(data)
results.extend(normalized)
except Exception as e:
print(f"Error fetching {provider}: {e}")
return results
def default_normalize(self, data):
# Fallback if no specific normalization is implemented
return []
# Normalization for Asda data
def normalize_asda(self, data):
stations = data.get("stations", [])
result = []
for s in stations:
result.append({
"site_id": s.get("site_id"),
"brand": "Asda",
"address": s.get("address"),
"postcode": s.get("postcode"),
"location": {
"longitude": s.get("longitude"),
"latitude": s.get("latitude"),
},
"prices": {
"E10": s.get("E10"),
"E5": s.get("E5"),
"B7": s.get("B7"),
},
})
return result
# Add similar normalization methods for other providers...
# Example usage:
# aggregator = FuelPriceAggregator()
# all_prices = aggregator.fetch_all()
# print(all_prices)