-
-
Notifications
You must be signed in to change notification settings - Fork 695
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #342 from thinkl33t/thinkl33t/manchester-city-council
Add source for manchester city council.
- Loading branch information
Showing
4 changed files
with
113 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
...om_components/waste_collection_schedule/waste_collection_schedule/source/manchester_uk.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
from datetime import datetime | ||
|
||
import requests | ||
from waste_collection_schedule import Collection # type: ignore[attr-defined] | ||
|
||
from bs4 import BeautifulSoup | ||
from urllib.parse import urlsplit, parse_qs | ||
import logging | ||
|
||
TITLE = "manchester.gov.uk" | ||
DESCRIPTION = "Source for bin collection services for Manchester City Council, UK." | ||
URL = "https://www.manchester.gov.uk/bincollections/" | ||
TEST_CASES = { | ||
"domestic": {'uprn': '000077065560'}, | ||
} | ||
|
||
ICONS = { | ||
"Black / Grey Bin": "mdi:trash-can", | ||
"Blue Bin": "mdi:recycle", | ||
"Brown Bin": "mdi:glass-fragile", | ||
"Green Bin": "mdi:leaf", | ||
} | ||
|
||
_LOGGER = logging.getLogger(__name__) | ||
|
||
|
||
class Source: | ||
def __init__( | ||
self, uprn: int = None | ||
): | ||
self._uprn = uprn | ||
if not self._uprn: | ||
_LOGGER.error( | ||
"uprn must be provided in config" | ||
) | ||
self._session = requests.Session() | ||
|
||
def fetch(self): | ||
entries = [] | ||
|
||
r = requests.post( | ||
URL, | ||
data={ | ||
"mcc_bin_dates_uprn": self._uprn, | ||
"mcc_bin_dates_submit": "Go" | ||
}, | ||
) | ||
|
||
soup = BeautifulSoup(r.text, features="html.parser") | ||
results = soup.find_all("div", {"class": "collection"}) | ||
|
||
for result in results: | ||
date = result.find("p", {"class": "caption"}) | ||
dates = [] | ||
dates.append(str(date.text).replace("Next collection ", "", 1)) | ||
for date in result.find_all('li'): | ||
dates.append(date.text) | ||
img_tag = result.find("img") | ||
collection_type = img_tag["alt"] | ||
for current_date in dates: | ||
try: | ||
date = datetime.strptime(current_date, "%A %d %b %Y").date() | ||
entries.append( | ||
Collection( | ||
date=date, | ||
t=collection_type, | ||
icon=ICONS[collection_type], | ||
) | ||
) | ||
except ValueError: | ||
_LOGGER.error(f"Skipped {current_date} as it does not match time format") | ||
|
||
return entries |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# Manchester City Council | ||
|
||
Support for schedules provided by [Manchester City | ||
Council](https://www.manchester.gov.uk/bincollections/), serving the | ||
city of Manchester, UK. | ||
|
||
## Configuration via configuration.yaml | ||
|
||
```yaml | ||
waste_collection_schedule: | ||
sources: | ||
- name: manchester_uk | ||
args: | ||
uprn: UPRN_CODE | ||
``` | ||
### Configuration Variables | ||
**uprn**<br> | ||
*(string) (required)* | ||
## Example | ||
```yaml | ||
waste_collection_schedule: | ||
sources: | ||
- name: manchester_uk | ||
args: | ||
uprn: "100031540175" | ||
``` | ||
## How to get the source argument | ||
The UPRN code can be found in the page by entering your postcode on the | ||
[Manchester City Council Bin Collections page | ||
](https://www.manchester.gov.uk/bincollections/). When on the address list, | ||
View the source code for the page, and look for your address, the uprn will be | ||
shown as the value. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters