From 94d3d9ffaaceed09d2efa59fb89c247f73f11875 Mon Sep 17 00:00:00 2001 From: Alex Schofield Date: Sun, 3 May 2026 18:18:29 +0100 Subject: init new uv project with new structure --- fnme/data.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 fnme/data.py (limited to 'fnme/data.py') diff --git a/fnme/data.py b/fnme/data.py new file mode 100644 index 0000000..e69de29 -- cgit v1.2.3 From d0e905285691d73f2cf7e45a88dbadf631620f0a Mon Sep 17 00:00:00 2001 From: Alex Schofield Date: Sun, 3 May 2026 18:31:26 +0100 Subject: move all logic into fnme folder --- constants.py | 12 ------- fnme/cli.py | 59 +++++++++++++++++++++++++++++++++ fnme/constants.py | 12 +++++++ fnme/data.py | 12 +++++++ fnme/geo.py | 10 ++++++ fnme/station.py | 59 +++++++++++++++++++++++++++++++++ helpers.py | 98 ------------------------------------------------------- main.py | 42 ------------------------ 8 files changed, 152 insertions(+), 152 deletions(-) delete mode 100644 constants.py create mode 100644 fnme/constants.py delete mode 100644 helpers.py delete mode 100644 main.py (limited to 'fnme/data.py') diff --git a/constants.py b/constants.py deleted file mode 100644 index bb57cb8..0000000 --- a/constants.py +++ /dev/null @@ -1,12 +0,0 @@ -ENDPOINT = "https://www.fuel-finder.service.gov.uk/internal/v1.0.2/csv/get-latest-fuel-prices-csv" - -SORT_KV = { - "e10": "e10_price", - "e5": "e5_price", - "b7s": "diesel_price", - "distance": "distance", -} - -HEADERS = { - "User-Agent": "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_4; en-US) AppleWebKit/533.45 (KHTML, like Gecko) Chrome/48.0.2094.221 Safari/602" -} diff --git a/fnme/cli.py b/fnme/cli.py index e69de29..d93d9f4 100644 --- a/fnme/cli.py +++ b/fnme/cli.py @@ -0,0 +1,59 @@ +import argparse +import sys +from typing import Any, Dict, List + +from constants import SORT_KV +from data import get_latest_data +from geo import get_location +from station import filter_df, sort_stations +from tabulate import tabulate + + +def parse_args() -> argparse.Namespace: + parser = argparse.ArgumentParser() + parser.add_argument("-a", "--address", type=str, required=True) + parser.add_argument("-r", "--radius", type=int, default=5) + parser.add_argument("-s", "--sort", type=str, default="e10", choices=SORT_KV.keys()) + return parser.parse_args() + + +def output_stations(stations: List[Dict[str, Any]]) -> None: + if not stations: + print("[*] No stations found.") + return + print( + tabulate( + stations, + headers={ + "station_name": "Station Name", + "distance": "Distance (miles)", + "e5_price": "E5 (£/L)", + "e10_price": "E10 (£/L)", + "diesel_price": "B7S (£/L)", + }, + floatfmt=".2f", + ) + ) + + +def main(): + args = parse_args() + + try: + location = get_location(args.address) + except ValueError as e: + print(f"[*] {e}") + sys.exit(1) + df, last_modified = get_latest_data() + + print(f"Last updated: {last_modified}") + + df_filtered = filter_df(df, args, location) + + sorted_stations_list = sort_stations(df_filtered, args.sort) + + output_stations(sorted_stations_list) + + +if __name__ == "__main__": + main() diff --git a/fnme/constants.py b/fnme/constants.py new file mode 100644 index 0000000..bb57cb8 --- /dev/null +++ b/fnme/constants.py @@ -0,0 +1,12 @@ +ENDPOINT = "https://www.fuel-finder.service.gov.uk/internal/v1.0.2/csv/get-latest-fuel-prices-csv" + +SORT_KV = { + "e10": "e10_price", + "e5": "e5_price", + "b7s": "diesel_price", + "distance": "distance", +} + +HEADERS = { + "User-Agent": "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_4; en-US) AppleWebKit/533.45 (KHTML, like Gecko) Chrome/48.0.2094.221 Safari/602" +} diff --git a/fnme/data.py b/fnme/data.py index e69de29..369d80f 100644 --- a/fnme/data.py +++ b/fnme/data.py @@ -0,0 +1,12 @@ +from io import StringIO +from typing import Optional + +import pandas as pd +import requests +from constants import ENDPOINT, HEADERS + + +def get_latest_data() -> tuple[pd.DataFrame, Optional[str]]: + response = requests.get(ENDPOINT, headers=HEADERS, timeout=10) + response.raise_for_status() + return pd.read_csv(StringIO(response.text)), response.headers.get("Last-Modified") diff --git a/fnme/geo.py b/fnme/geo.py index e69de29..dba68fe 100644 --- a/fnme/geo.py +++ b/fnme/geo.py @@ -0,0 +1,10 @@ +from geopy.geocoders import Nominatim +from geopy.location import Location + + +def get_location(address: str) -> tuple[float, float]: + geolocator = Nominatim(user_agent="FuelNearMe") + result = geolocator.geocode(address) + if not isinstance(result, Location): + raise ValueError(f"Failed to get location from address: '{address}'") + return (result.latitude, result.longitude) diff --git a/fnme/station.py b/fnme/station.py index e69de29..e5112c3 100644 --- a/fnme/station.py +++ b/fnme/station.py @@ -0,0 +1,59 @@ +import argparse +import math +from typing import Any, Dict, List, Tuple + +import numpy as np +import pandas as pd +from constants import SORT_KV + + +def filter_df( + dframe: pd.DataFrame, arguments: argparse.Namespace, loc: Tuple[float, float] +) -> List[Dict[str, Any]]: + + def bounding_box() -> pd.DataFrame: + lat, lon = loc + deg_lat = arguments.radius / 69.0 + deg_lon = arguments.radius / (69.0 * math.cos(math.radians(lat))) + return dframe[ + dframe["forecourts.location.latitude"].between(lat - deg_lat, lat + deg_lat) + & dframe["forecourts.location.longitude"].between( + lon - deg_lon, lon + deg_lon + ) + ] + + def haversine_miles(lat2: np.ndarray, lon2: np.ndarray) -> np.ndarray: + R = 3958.8 + lat1, lon1 = np.radians(loc[0]), np.radians(loc[1]) + lat2, lon2 = np.radians(lat2), np.radians(lon2) + dlat = lat2 - lat1 + dlon = lon2 - lon1 + a = np.sin(dlat / 2) ** 2 + np.cos(lat1) * np.cos(lat2) * np.sin(dlon / 2) ** 2 + return R * 2 * np.arcsin(np.sqrt(a)) + + def pence_to_pounds(col: pd.Series) -> pd.Series: + return (col / 100).round(2).where(col.notna(), other="N/A") + + df = bounding_box().copy() + + df["distance"] = haversine_miles( + df["forecourts.location.latitude"].to_numpy(), + df["forecourts.location.longitude"].to_numpy(), + ).round(1) + + df = df[df["distance"] < arguments.radius] + + df = df.assign( + e5_price=pence_to_pounds(df["forecourts.fuel_price.E5"]), + e10_price=pence_to_pounds(df["forecourts.fuel_price.E10"]), + diesel_price=pence_to_pounds(df["forecourts.fuel_price.B7S"]), + ) + + return df.rename(columns={"forecourts.trading_name": "station_name"})[ + ["station_name", "distance", "e5_price", "e10_price", "diesel_price"] + ].to_dict(orient="records") + + +def sort_stations(stations: list[dict], sort: str) -> list[dict]: + sort_key = SORT_KV[sort] + return sorted(stations, key=lambda d: d[sort_key] if d[sort_key] != "N/A" else 999) diff --git a/helpers.py b/helpers.py deleted file mode 100644 index 64a3b0a..0000000 --- a/helpers.py +++ /dev/null @@ -1,98 +0,0 @@ -import argparse -import math -from io import StringIO -from typing import Any, Dict, List, Optional, Tuple - -import numpy as np -import pandas as pd -import requests -from geopy.geocoders import Nominatim -from geopy.location import Location -from tabulate import tabulate - -from constants import ENDPOINT, HEADERS, SORT_KV - - -def get_location(address: str) -> tuple[float, float]: - geolocator = Nominatim(user_agent="FuelNearMe") - result = geolocator.geocode(address) - if not isinstance(result, Location): - raise ValueError(f"Failed to get location from address: '{address}'") - return (result.latitude, result.longitude) - - -def get_latest_data() -> tuple[pd.DataFrame, Optional[str]]: - response = requests.get(ENDPOINT, headers=HEADERS, timeout=10) - response.raise_for_status() - return pd.read_csv(StringIO(response.text)), response.headers.get("Last-Modified") - - -def filter_df( - dframe: pd.DataFrame, arguments: argparse.Namespace, loc: Tuple[float, float] -) -> List[Dict[str, Any]]: - - def bounding_box() -> pd.DataFrame: - lat, lon = loc - deg_lat = arguments.radius / 69.0 - deg_lon = arguments.radius / (69.0 * math.cos(math.radians(lat))) - return dframe[ - dframe["forecourts.location.latitude"].between(lat - deg_lat, lat + deg_lat) - & dframe["forecourts.location.longitude"].between( - lon - deg_lon, lon + deg_lon - ) - ] - - def haversine_miles(lat2: np.ndarray, lon2: np.ndarray) -> np.ndarray: - R = 3958.8 - lat1, lon1 = np.radians(loc[0]), np.radians(loc[1]) - lat2, lon2 = np.radians(lat2), np.radians(lon2) - dlat = lat2 - lat1 - dlon = lon2 - lon1 - a = np.sin(dlat / 2) ** 2 + np.cos(lat1) * np.cos(lat2) * np.sin(dlon / 2) ** 2 - return R * 2 * np.arcsin(np.sqrt(a)) - - def pence_to_pounds(col: pd.Series) -> pd.Series: - return (col / 100).round(2).where(col.notna(), other="N/A") - - df = bounding_box().copy() - - df["distance"] = haversine_miles( - df["forecourts.location.latitude"].to_numpy(), - df["forecourts.location.longitude"].to_numpy(), - ).round(1) - - df = df[df["distance"] < arguments.radius] - - df = df.assign( - e5_price=pence_to_pounds(df["forecourts.fuel_price.E5"]), - e10_price=pence_to_pounds(df["forecourts.fuel_price.E10"]), - diesel_price=pence_to_pounds(df["forecourts.fuel_price.B7S"]), - ) - - return df.rename(columns={"forecourts.trading_name": "station_name"})[ - ["station_name", "distance", "e5_price", "e10_price", "diesel_price"] - ].to_dict(orient="records") - - -def sort_stations(stations: list[dict], sort: str) -> list[dict]: - sort_key = SORT_KV[sort] - return sorted(stations, key=lambda d: d[sort_key] if d[sort_key] != "N/A" else 999) - - -def output_stations(stations: List[Dict[str, Any]]) -> None: - if not stations: - print("[*] No stations found.") - return - print( - tabulate( - stations, - headers={ - "station_name": "Station Name", - "distance": "Distance (miles)", - "e5_price": "E5 (£/L)", - "e10_price": "E10 (£/L)", - "diesel_price": "B7S (£/L)", - }, - floatfmt=".2f", - ) - ) diff --git a/main.py b/main.py deleted file mode 100644 index 0c81d9a..0000000 --- a/main.py +++ /dev/null @@ -1,42 +0,0 @@ -import argparse -import sys - -from constants import SORT_KV -from helpers import ( - filter_df, - get_latest_data, - get_location, - output_stations, - sort_stations, -) - - -def parse_args() -> argparse.Namespace: - parser = argparse.ArgumentParser() - parser.add_argument("-a", "--address", type=str, required=True) - parser.add_argument("-r", "--radius", type=int, default=5) - parser.add_argument("-s", "--sort", type=str, default="e10", choices=SORT_KV.keys()) - return parser.parse_args() - - -def main(): - args = parse_args() - - try: - location = get_location(args.address) - except ValueError as e: - print(f"[*] {e}") - sys.exit(1) - df, last_modified = get_latest_data() - - print(f"Last updated: {last_modified}") - - df_filtered = filter_df(df, args, location) - - sorted_stations_list = sort_stations(df_filtered, args.sort) - - output_stations(sorted_stations_list) - - -if __name__ == "__main__": - main() -- cgit v1.2.3 From 4264ced9fa14b689acc18169b2c1cae72d3ce667 Mon Sep 17 00:00:00 2001 From: Alex Schofield Date: Sun, 3 May 2026 19:55:31 +0100 Subject: fix import paths & update pyproject.toml --- fnme/cli.py | 9 +++++---- fnme/data.py | 3 ++- fnme/station.py | 3 ++- pyproject.toml | 2 +- 4 files changed, 10 insertions(+), 7 deletions(-) (limited to 'fnme/data.py') diff --git a/fnme/cli.py b/fnme/cli.py index d93d9f4..d9ef508 100644 --- a/fnme/cli.py +++ b/fnme/cli.py @@ -2,12 +2,13 @@ import argparse import sys from typing import Any, Dict, List -from constants import SORT_KV -from data import get_latest_data -from geo import get_location -from station import filter_df, sort_stations from tabulate import tabulate +from fnme.constants import SORT_KV +from fnme.data import get_latest_data +from fnme.geo import get_location +from fnme.station import filter_df, sort_stations + def parse_args() -> argparse.Namespace: parser = argparse.ArgumentParser() diff --git a/fnme/data.py b/fnme/data.py index 369d80f..bdec6df 100644 --- a/fnme/data.py +++ b/fnme/data.py @@ -3,7 +3,8 @@ from typing import Optional import pandas as pd import requests -from constants import ENDPOINT, HEADERS + +from fnme.constants import ENDPOINT, HEADERS def get_latest_data() -> tuple[pd.DataFrame, Optional[str]]: diff --git a/fnme/station.py b/fnme/station.py index e5112c3..c971173 100644 --- a/fnme/station.py +++ b/fnme/station.py @@ -4,7 +4,8 @@ from typing import Any, Dict, List, Tuple import numpy as np import pandas as pd -from constants import SORT_KV + +from fnme.constants import SORT_KV def filter_df( diff --git a/pyproject.toml b/pyproject.toml index 721d4ee..fcf426d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -13,7 +13,7 @@ dependencies = [ ] [project.scripts] -fnme = "fuelnearme.cli:main" +fnme = "fnme.cli:main" [tool.setuptools.packages.find] where = ["."] -- cgit v1.2.3