forked from mampfes/hacs_waste_collection_schedule
-
Notifications
You must be signed in to change notification settings - Fork 1
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 mampfes#622 from mampfes/refactor_stockport
Refactor stockport
- Loading branch information
Showing
4 changed files
with
116 additions
and
1 deletion.
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
65 changes: 65 additions & 0 deletions
65
...components/waste_collection_schedule/waste_collection_schedule/source/stockport_gov_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,65 @@ | ||
import logging | ||
import re | ||
from datetime import datetime | ||
|
||
import requests | ||
from bs4 import BeautifulSoup | ||
from waste_collection_schedule import Collection # type: ignore[attr-defined] | ||
|
||
TITLE = "Stockport Council" | ||
DESCRIPTION = "Source for bin collection services for Stockport Council, UK.\n Refactored with thanks from the Manchester equivalent" | ||
URL = "https://stockport.gov.uk" | ||
TEST_CASES = { | ||
"domestic": {"uprn": "100011460157"}, | ||
} | ||
|
||
ICON_MAP = { | ||
"Black 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): | ||
self._uprn = uprn | ||
|
||
def fetch(self): | ||
|
||
r = requests.get( | ||
f"https://myaccount.stockport.gov.uk/bin-collections/show/{self._uprn}" | ||
) | ||
|
||
soup = BeautifulSoup(r.text, features="html.parser") | ||
|
||
bins = soup.find_all( | ||
"div", {"class": re.compile("service-item service-item-.*")} | ||
) | ||
|
||
bins = str(bins) | ||
header_string = "<h3>" | ||
bin_name_start_position = bins.find(header_string) | ||
|
||
entries = [] | ||
while bin_name_start_position != -1: | ||
bin_name_start_position += len(header_string) | ||
bin_name_end_position = bins.find(" bin", bin_name_start_position) + 4 | ||
bin_name = bins[bin_name_start_position:bin_name_end_position] | ||
bin_date_pos = bins.find("<p>", bin_name_start_position) | ||
bin_date_exc_day_of_week_pos = bins.find(", ", bin_date_pos) + 2 | ||
bin_date_end_pos = bins.find("</p>", bin_date_exc_day_of_week_pos) | ||
bin_date_string = bins[bin_date_exc_day_of_week_pos:bin_date_end_pos] | ||
bin_date = datetime.strptime(bin_date_string, "%d %B %Y").date() | ||
entries.append( | ||
Collection( | ||
date=bin_date, | ||
t=bin_name, | ||
icon=ICON_MAP.get(bin_name), | ||
) | ||
) | ||
bin_name_start_position = bins.find(header_string, bin_date_end_pos) | ||
|
||
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,49 @@ | ||
# Stockport Metropolitan Borough Council | ||
|
||
Support for schedules provided by [Stockport Metropolitan Borough | ||
Council](https://www.manchester.gov.uk/bincollections/), serving the | ||
area of Stockport, UK. | ||
|
||
With thanks to the creator of the schedule for Manchester, UK, from | ||
whom some of the code is re-factored. | ||
|
||
## Configuration via configuration.yaml | ||
|
||
```yaml | ||
waste_collection_schedule: | ||
sources: | ||
- name: stockport_gov_uk | ||
args: | ||
uprn: UPRN_CODE | ||
``` | ||
### Configuration Variables | ||
**uprn**<br> | ||
*(string) (required)* | ||
## Example | ||
```yaml | ||
waste_collection_schedule: | ||
sources: | ||
- name: stockport_gov_uk | ||
args: | ||
uprn: "10090543805" | ||
``` | ||
## How to get the source argument | ||
The UPRN code can be found in the page by entering your postcode on the | ||
[Stockport Bin Collections page | ||
](https://www.stockport.gov.uk/find-your-collection-day/). | ||
Select your house from the drop-down, then the UPRN is the final part of | ||
the URL that you are re-directed to. | ||
For example, for post-code SK6 3AA, house number 25, you are re-directed to | ||
https://myaccount.stockport.gov.uk/bin-collections/show/10090543805 | ||
The UPRN is 10090543805 |
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