Add source nyc_gov (#2649)

* Add New York City

* reformatting + remove urllib.parse in favor of params argument

---------

Co-authored-by: 5ila5 <5ila5@users.noreply.github.com>
This commit is contained in:
Adam Jones
2024-09-10 18:50:40 +02:00
committed by GitHub
parent ec22c9252f
commit 997fac17c2
5 changed files with 104 additions and 1 deletions

View File

@@ -1816,6 +1816,7 @@ If your service provider is not listed, feel free to open a [source request issu
- [City of Oklahoma City (unofficial)](/doc/source/okc_gov.md) / okc.gov
- [City of Pittsburgh](/doc/source/pgh_st.md) / pgh.st
- [Louisville, Kentucky, USA](/doc/source/recyclecoach_com.md) / recyclecoach.com/cities/usa-ky-city-of-louisville
- [New York City](/doc/source/nyc_gov.md) / nyc.gov
- [Newark, Delaware, USA](/doc/source/recyclecoach_com.md) / recyclecoach.com/cities/usa-de-city-of-newark
- [Olympia, Washington, USA](/doc/source/recyclecoach_com.md) / recyclecoach.com/cities/usa-wa-city-of-olympia
- [ReCollect](/doc/ics/recollect.md) / recollect.net

View File

@@ -9850,6 +9850,11 @@
"module": "recyclecoach_com",
"default_params": {}
},
{
"title": "New York City",
"module": "nyc_gov",
"default_params": {}
},
{
"title": "Newark, Delaware, USA",
"module": "recyclecoach_com",

View File

@@ -0,0 +1,65 @@
import requests
from dateutil.parser import parse
from waste_collection_schedule import Collection # type: ignore[attr-defined]
TITLE = "New York City"
DESCRIPTION = "Source for New York City, US."
URL = "https://www.nyc.gov"
COUNTRY = "us"
TEST_CASES = {
"Test_001": {"address": "120-55 Queens Blvd, Kew Gardens, NY 11424"},
"Test_002": {"address": "1 W 72nd St, New York, NY 10023"},
}
ICON_MAP = {
"Trash": "mdi:trash-can",
"Recycling": "mdi:recycle",
"Composting": "mdi:food",
"Large items": "mdi:sofa",
}
class Source:
def __init__(self, address):
self._address = str(address)
self._session = requests.Session()
def fetch(self) -> list[Collection]:
url = "https://dsnypublic.nyc.gov/dsny/api/geocoder/DSNYCollection"
response = self._session.get(url, params={"address": self._address})
response.raise_for_status()
data: dict = response.json()
entries = []
entries.extend(
self.extract_collections(data["RegularCollectionSchedule"], "Trash")
)
entries.extend(
self.extract_collections(data["RecyclingCollectionSchedule"], "Recycling")
)
entries.extend(
self.extract_collections(data["OrganicsCollectionSchedule"], "Composting")
)
entries.extend(
self.extract_collections(
data["BulkPickupCollectionSchedule"], "Large items"
)
)
return entries
def extract_collections(
self, csvDays: str | None, waste_type: str
) -> list[Collection]:
"""Given a string of days e.g. "Monday,Wednesday" return a list of Collection objects set to the given waste type."""
if not csvDays:
return []
return [
Collection(
date=parse(day).date(),
t=waste_type,
icon=ICON_MAP.get(waste_type),
)
for day in csvDays.split(",")
]

32
doc/source/nyc_gov.md Normal file
View File

@@ -0,0 +1,32 @@
# New York City
Support for schedules provided by [New York City](https://www.nyc.gov/assets/dsny/forms/collection-schedule), serving the city of New York, NY, USA.
## Configuration via configuration.yaml
```yaml
waste_collection_schedule:
sources:
- name: nyc_gov
args:
address: ADDRESS
```
### Configuration Variables
**address**
*(string) (required)*
## Example
```yaml
waste_collection_schedule:
sources:
- name: nyc_gov
args:
address: Queens Borough Hall, 120-55 Queens Blvd, Jamaica, NY 11424, USA
```
## How to get the source argument
The `address` argument is simply the house mailing address. You can what formats of your address are accepted at <https://www.nyc.gov/assets/dsny/forms/collection-schedule>.

File diff suppressed because one or more lines are too long