Skip to main content

A simple python module that scrapes XML data from the government of Western Australia's FuelWatch website that makes parsing a breeze.

Project description

    ______           __               __       __
   / ____/_  _____  / /      ______ _/ /______/ /_  ___  _____
  / /_  / / / / _ \/ / | /| / / __ `/ __/ ___/ __ \/ _ \/ ___/
 / __/ / /_/ /  __/ /| |/ |/ / /_/ / /_/ /__/ / / /  __/ /
/_/    \__,_/\___/_/ |__/|__/\__,_/\__/\___/_/ /_/\___/_/

Fuelwatcher

A simple python module that scrapes XML data from the government of Western Australia's FuelWatch website that makes parsing a breeze.

Fuelwatch.wa.gov.au provides information on fuel prices by fuel type, location, brand and region within Western Australia. Fuelwatcher will parse the XML from the fuelwatch.wa.gov.au RSS feed giving the developer an easy way to manipulate the information.

Installation

Requires pip to be installed or pip3 dependent on system, or environment.

pip install fuelwatcher

Usage example

Basic Usage (Recommended)

The recommended way to access fuel station data is via the typed stations property, which returns a list of FuelStation dataclass instances:

from fuelwatcher import FuelWatch

api = FuelWatch()

# Query the API
api.query(product=2, region=25, day='yesterday')

# Access stations as typed dataclass instances
for station in api.stations:
    print(f"{station.trading_name}: ${station.price}")
    print(f"  Address: {station.address}")
    print(f"  Location: {station.latitude}, {station.longitude}")

The FuelStation dataclass provides IDE autocomplete and type safety:

from fuelwatcher import FuelStation

# FuelStation fields:
# - title, description, brand, date, price
# - trading_name, location, address, phone
# - latitude, longitude, site_features

Alternative Access Methods

As list of dictionaries:

api = FuelWatch()
api.query(region=1)

# Access as list of dicts
stations = api.xml
print(stations[0]['title'])
>>> '143.9: United Boulder Kalgoorlie'

As JSON string:

api = FuelWatch()
api.query(region=1)

json_response = api.json
>>> [
>>>   {
>>>       "title": "143.9: United Boulder Kalgoorlie",
>>>       "description": "Address: Cnr Lane St & Davis St, BOULDER...",
>>>       "brand": "United",
>>>       ...
>>>   }
>>> ]

Raw XML bytes:

api = FuelWatch()
api.query(product=1)

raw_xml = api.raw
>>> b'<?xml version="1.0" encoding="UTF-8"?>...'

Query Parameters

The query method takes several keyword arguments:

def query(
    product: int = None,      # Fuel type ID
    suburb: str = None,       # WA suburb name
    region: int = None,       # Region ID
    brand: int = None,        # Brand ID
    surrounding: bool = None, # Include surrounding suburbs
    day: str = None,          # 'today', 'tomorrow', 'yesterday', or 'DD/MM/YYYY'
)

A query without any arguments returns all of today's Unleaded stations in Western Australia.

Notes:

  • If suburb is set, surrounding defaults to yes. To get only the suburb, explicitly pass surrounding=False
  • Don't mix region with suburb and surrounding together
  • The surrounding parameter accepts both bool (True/False) and str ('yes'/'no')

A list of valid suburbs, brands, regions and products (fuel types) can be found in constants.py

Error Handling

Fuelwatcher validates inputs and raises FuelWatchError for invalid parameters or failed requests:

from fuelwatcher import FuelWatch, FuelWatchError

api = FuelWatch()

try:
    api.query(product=999)  # Invalid product ID
except FuelWatchError as e:
    print(f"Error: {e}")
>>> Error: Invalid product ID: 999. Valid options: 1: Unleaded Petrol, 2: Premium Unleaded...

Backwards Compatibility

The previous get_* property names are still supported but deprecated:

# Deprecated (still works, emits DeprecationWarning)
api.get_xml    # Use api.xml instead
api.get_json   # Use api.json instead
api.get_raw    # Use api.raw instead

Migration guide:

Old (deprecated) New
api.get_xml api.xml or api.stations
api.get_json api.json
api.get_raw api.raw
AssertionError FuelWatchError
surrounding='yes' surrounding=True

Meta

Daniel Michaels – https://danielms.site

Distributed under the MIT license. See LICENSE for more information.

Contributing

All requests, ideas or improvements are welcomed!

  1. Fork it
  2. Create your feature branch (git checkout -b feature/fooBar)
  3. Commit your changes (git commit -am 'Add some fooBar')
  4. Push to the branch (git push origin feature/fooBar)
  5. Create a new Pull Request

Inspired by..

A local python meetup group idea that turned into a PyPi package for anyone to use!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fuelwatcher-1.0.0.tar.gz (33.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fuelwatcher-1.0.0-py3-none-any.whl (11.1 kB view details)

Uploaded Python 3

File details

Details for the file fuelwatcher-1.0.0.tar.gz.

File metadata

  • Download URL: fuelwatcher-1.0.0.tar.gz
  • Upload date:
  • Size: 33.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.20 {"installer":{"name":"uv","version":"0.9.20","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for fuelwatcher-1.0.0.tar.gz
Algorithm Hash digest
SHA256 db2527b994158cc0b10ca6fb558df9ea6219526025b7114f9da38ef1fb3c361e
MD5 8ccd32fbc313f8c93e958c79a2bcedf2
BLAKE2b-256 35ff77c3199bf484395086e79b5c9cd6624805b0d1e800cc942bbf24f61bd372

See more details on using hashes here.

File details

Details for the file fuelwatcher-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: fuelwatcher-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 11.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.20 {"installer":{"name":"uv","version":"0.9.20","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for fuelwatcher-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8bfbe50c88928792417612c290cf03c15c22cace1787dd7b92c6e7d9e5ec06ea
MD5 acb0b39353b6e13477fb28c14712b9b5
BLAKE2b-256 161948e1e77d484d9fb59b74a7bc21e60fdb87c653bd7ae34540679d4cdd39ff

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page