|
| 1 | +"""Carrier fuel surcharges: LTL and parcel (#101). |
| 2 | +
|
| 3 | +Usage: |
| 4 | + OILPRICEAPI_KEY=... python examples/fuel_surcharge.py |
| 5 | +
|
| 6 | +Prints the latest LTL surcharge per carrier, one carrier's recent weekly |
| 7 | +history, and the latest parcel surcharge per service level. Every row shows the |
| 8 | +carrier's effective date and where the value was retrieved from. |
| 9 | +""" |
| 10 | + |
| 11 | +import os |
| 12 | + |
| 13 | +from oilpriceapi import OilPriceAPI |
| 14 | +from oilpriceapi.exceptions import DataNotFoundError |
| 15 | + |
| 16 | + |
| 17 | +def main() -> None: |
| 18 | + with OilPriceAPI(api_key=os.environ["OILPRICEAPI_KEY"]) as client: |
| 19 | + print("LTL carriers") |
| 20 | + rates = client.fuel_surcharge.list() |
| 21 | + for rate in rates: |
| 22 | + print( |
| 23 | + f" {rate.carrier:<22} {rate.surcharge_percent:>6.2f}% " |
| 24 | + f"effective {rate.effective_date} retrieved {rate.retrieved_at:%Y-%m-%d}" |
| 25 | + ) |
| 26 | + |
| 27 | + if rates: |
| 28 | + carrier = rates[0].carrier |
| 29 | + page = client.fuel_surcharge.history(carrier, per_page=4) |
| 30 | + print(f"\n{carrier} history ({page.meta.total_count} weeks on record)") |
| 31 | + for row in page.history: |
| 32 | + diesel = "n/a" if row.doe_diesel_price is None else f"${row.doe_diesel_price:.3f}" |
| 33 | + print(f" {row.effective_date} {row.surcharge_percent:.2f}% DOE diesel {diesel}") |
| 34 | + print(f" source: {page.history[0].source}" if page.history else " no rows") |
| 35 | + |
| 36 | + print("\nParcel carriers") |
| 37 | + for parcel in client.fuel_surcharge.parcel_list(): |
| 38 | + for rate in parcel.service_levels: |
| 39 | + print( |
| 40 | + f" {parcel.carrier:<6} {rate.service_level:<26} " |
| 41 | + f"{rate.surcharge_percent:>6.2f}% effective {rate.effective_date}" |
| 42 | + ) |
| 43 | + |
| 44 | + try: |
| 45 | + client.fuel_surcharge.latest("fedex-freight") |
| 46 | + except DataNotFoundError as error: |
| 47 | + print(f"\nNot covered: {error.message}") |
| 48 | + print(f"Covered carriers: {', '.join(error.suggestions)}") |
| 49 | + |
| 50 | + |
| 51 | +if __name__ == "__main__": |
| 52 | + main() |
0 commit comments