From 125167d2a992c170aebdae8b1d8c7bcae2f9365b Mon Sep 17 00:00:00 2001 From: dt215git <86194065+dt215git@users.noreply.github.com> Date: Mon, 10 Apr 2023 13:36:08 +0100 Subject: [PATCH] New Source: City of Oklahoma City (#856) * Exception handling updated as per comment thread * tidy-up * minor reformatting --- README.md | 1 + .../source/okc_gov.py | 74 +++++++++++++++++++ doc/source/okc_gov.md | 35 +++++++++ info.md | 2 +- 4 files changed, 111 insertions(+), 1 deletion(-) create mode 100644 custom_components/waste_collection_schedule/waste_collection_schedule/source/okc_gov.py create mode 100644 doc/source/okc_gov.md diff --git a/README.md b/README.md index 2677498a..c985df47 100644 --- a/README.md +++ b/README.md @@ -566,6 +566,7 @@ Waste collection schedules in the following formats and countries are supported.
United States of America +- [City of Oklahoma City](/doc/source/okc_gov.md) / okc.gov - [City of Pittsburgh](/doc/source/pgh_st.md) / pgh.st - [Republic Services](/doc/source/republicservices_com.md) / republicservices.com - [Seattle Public Utilities](/doc/source/seattle_gov.md) / myutilities.seattle.gov diff --git a/custom_components/waste_collection_schedule/waste_collection_schedule/source/okc_gov.py b/custom_components/waste_collection_schedule/waste_collection_schedule/source/okc_gov.py new file mode 100644 index 00000000..09f13c59 --- /dev/null +++ b/custom_components/waste_collection_schedule/waste_collection_schedule/source/okc_gov.py @@ -0,0 +1,74 @@ +import json +import requests + +from datetime import datetime, timedelta +from waste_collection_schedule import Collection + +TITLE = "City of Oklahoma City" +DESCRIPTION = ( + "Source for okc.gov services for City of Oklahoma City" +) +URL = "https://www.okc.gov" +COUNTRY = "us" +TEST_CASES = { + "Test_001": {"objectID": "1781151"}, + "Test_002": {"objectID": "2002902"}, + "Test_003": {"objectID": 1935340}, +} +HEADERS = { + "content-type": "text/json", + "user-agent": "Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/111.0", +} +ICON_MAP = { + "TRASH": "mdi:trash-can", + "RECYCLE": "mdi:recycle", + "BULKY": "mdi:sofa", +} + + +class Source: + def __init__(self, objectID): + self._recordID = str(objectID) + + def fetch(self): + + s = requests.Session() + r = s.get( + f"https://data.okc.gov/services/portal/api/data/records/Address%20Trash%20Services", + params={"recordID": self._recordID}, + headers=HEADERS, + ) + + try: + json_data = json.loads(r.text) + except Exception as e: + raise Exception("Invalid response returned from data.okc.gov") from e + else: + waste_types = [] + # Build list of collection categories + for item in json_data["Fields"][3:-1]: # limit to those entries containing collection info + waste_types.append(item["FieldName"].replace("Next_", "").split("_")[0]) + # Build list of collection days/dates + waste_dates = [] + action_day = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0) + for item in json_data["Records"][0][3:-1]:# limit to those entries containing collection info + if item != "Not Available": # ignore missing collections + if "day" in item: # convert day of week into next collection date + while action_day.strftime("%A") != item: + action_day += timedelta(days=+1) + waste_dates.append(action_day.date()) + else: + waste_dates.append(datetime.strptime(item, "%b %d, %Y").date()) + schedule = list(zip(waste_types, waste_dates)) + + entries = [] + for waste in schedule: + entries.append( + Collection( + date=waste[1], + t=waste[0], + icon=ICON_MAP.get(waste[0].upper()), + ) + ) + + return entries diff --git a/doc/source/okc_gov.md b/doc/source/okc_gov.md new file mode 100644 index 00000000..cf77c077 --- /dev/null +++ b/doc/source/okc_gov.md @@ -0,0 +1,35 @@ +# City of Oklahoma City + +Support for schedules provided by [City of Oklahoma City](https://www.okc.gov/), serving City of Oklahoma City. + +## Configuration via configuration.yaml + +```yaml +waste_collection_schedule: + sources: + - name: okc.gov + args: + objectID: UNIQUE_PROPERTY_IDENTIFIER +``` + +### Configuration Variables + +**objectID** +*(string) (required)* + +## Example + +```yaml +waste_collection_schedule: + sources: + - name: okc.gov + args: + objectID: "1781151" +``` + +## How to find your `objectID` + +Using a browser, go to [data.okc.gov](https://data.okc.gov/portal/page/viewer?datasetName=Address%20Trash%20Services). +Click on the `Map` tab, search for your address, then click on your house. Your schedule will be displayed. +Click on the `Table` tab, then click on the `Filter By Map` menu item, and click `Apply` to reduce the number of items being displayed. Note: In the previous step, the more you zoom in on your house, the better this filter works. +Find your address in the filtered list and make a note of the `Object ID` number in the first column. This is the number you need to use. diff --git a/info.md b/info.md index 0be5dfb6..b77ca46a 100644 --- a/info.md +++ b/info.md @@ -31,7 +31,7 @@ Waste collection schedules from service provider web sites are updated daily, de | Sweden | Affärsverken, Jönköping - June Avfall & Miljö, Landskrona - Svalövs Renhållning, Lerum Vatten och Avlopp, Linköping - Tekniska Verken, Region Gotland, Ronneby Miljöteknik, Samverkan Återvinning Miljö (SÅM), SRV Återvinning, SSAM, Sysav Sophämntning, Uppsala Vatten och Avfall AB, VA Syd Sophämntning | | Switzerland | A-Region, Andwil, Appenzell, Berg, Bühler, Eggersriet, Gais, Gaiserwald, Goldach, Grosswangen, Grub, Heiden, Herisau, Horn, Hundwil, Häggenschwil, Lindau, Lutzenberg, Muolen, Mörschwil, Münchenstein, Rehetobel, Rorschach, Rorschacherberg, Schwellbrunn, Schönengrund, Speicher, Stein, Steinach, Teufen, Thal, Trogen, Tübach, Untereggen, Urnäsch, Wald, Waldkirch, Waldstatt, Wittenbach, Wolfhalden | | United Kingdom | Amber Valley Borough Council, Ashfield District Council, Basingstoke and Deane Borough Council, Binzone, Blackburn with Darwen Borough Council, Bracknell Forest Council, Bradford Metropolitan District Council, Braintree District Council, Breckland Council, Bristol City Council, Buckinghamshire Waste Collection - Former Chiltern, South Bucks or Wycombe areas, Cambridge City Council, Canterbury City Council, Central Bedfordshire Council, Cheshire East Council, Chesterfield Borough Council, Chichester District Council, City of Doncaster Council, City of York Council, Colchester City Council, Cornwall Council, Derby City Council, East Cambridgeshire District Council, East Herts Council, East Northamptonshire and Wellingborough, Eastbourne Borough Council, Elmbridge Borough Council, Environment First, FCC Environment, Fenland District Council, Fife Council, Gateshead Council, Glasgow City Council, Guildford Borough Council, Harborough District Council, Harlow Council, Herefordshire City Council, Horsham District Council, Huntingdonshire District Council, Leicester City Council, Lewes District Council, Liverpool City Council, London Borough of Bromley, London Borough of Lewisham, London Borough of Merton, Maldon District Council, Manchester City Council, Mid-Sussex District Council, Middlesbrough Council, Newcastle City Council, Newcastle Under Lyme Borough Council, Newport City Council, North Herts Council, North Lincolnshire Council, North Somerset Council, Nottingham City Council, Oxford City Council, Peterborough City Council, Richmondshire District Council, Rotherham Metropolitan Borough Council, Runnymede Borough Council, Rushmoor Borough Council, Salford City Council, Sheffield City Council, South Cambridgeshire District Council, South Derbyshire District Council, South Hams District Council, South Norfolk and Broadland Council, South Oxfordshire District Council, Southampton City Council, Stevenage Borough Council, Stockport Council, Stockton-on-Tees Borough Council, Swindon Borough Council, Telford and Wrekin Council, Tewkesbury Borough Council, The Royal Borough of Kingston Council, Tonbridge and Malling Borough Council, Uttlesford District Council, Vale of White Horse District Council, Walsall Council, Waverley Borough Council, West Berkshire Council, West Devon Borough Council, West Dunbartonshire Council, Wigan Council, Wiltshire Council, Wyre Forest District Council | -| United States of America | City of Pittsburgh, Republic Services, Seattle Public Utilities | +| United States of America | City of Oklahoma City, City of Pittsburgh, Republic Services, Seattle Public Utilities | For full details on supported service providers, and more project details, visit us on [GitHub](https://github.com/mampfes/hacs_waste_collection_schedule).