Skip to content

Commit

Permalink
Merge pull request mampfes#622 from mampfes/refactor_stockport
Browse files Browse the repository at this point in the history
Refactor stockport
  • Loading branch information
mampfes authored Jan 18, 2023
2 parents 2bc3c6b + b1602ee commit 0cbf4bf
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,7 @@ Waste collection schedules in the following formats and countries are supported.
- [South Norfolk and Broadland Council](/doc/source/south_norfolk_and_broadland_gov_uk.md) / area.southnorfolkandbroadland.gov.uk
- [Southampton City Council](/doc/source/southampton_gov_uk.md) / southampton.gov.uk
- [Stevenage Borough Council](/doc/source/stevenage_gov_uk.md) / stevenage.gov.uk
- [Stockport Council](/doc/source/stockport_gov_uk.md) / stockport.gov.uk
- [Telford and Wrekin Council](/doc/source/telford_gov_uk.md) / telford.gov.uk
- [Tewkesbury Borough Council](/doc/source/tewkesbury_gov_uk.md) / tewkesbury.gov.uk
- [The Royal Borough of Kingston Council](/doc/source/kingston_gov_uk.md) / kingston.gov.uk
Expand Down
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
49 changes: 49 additions & 0 deletions doc/source/stockport_gov_uk.md
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
2 changes: 1 addition & 1 deletion info.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Waste collection schedules from service provider web sites are updated daily, de
| Poland | Ecoharmonogram, Warsaw |
| Sweden | Landskrona - Svalövs Renhållning, Lerum Vatten och Avlopp, Ronneby Miljöteknik, SRV Återvinning, SSAM, Sysav Sophämntning, 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 | Ashfield District Council, Bracknell Forest Council, Bradford Metropolitan District Council, Braintree District Council, Breckland Council, Cambridge City Council, Canterbury City Council, Central Bedfordshire Council, Cheshire East Council, Chesterfield Borough Council, City of York Council, Colchester City Council, Cornwall Council, Derby City Council, East Cambridgeshire District Council, East Herts Council, Eastbourne Borough Council, Elmbridge Borough Council, Environment First, FCC Environment, Guildford Borough Council, Harborough District Council, Harlow Council, Horsham District Council, Huntingdonshire District Council, Lewes District Council, London Borough of Lewisham, Manchester City Council, Middlesbrough Council, Newcastle City Council, Newcastle Under Lyme Borough Council, Newport City Council, North Somerset Council, Nottingham City Council, Peterborough City Council, Richmondshire District Council, Rushmoor Borough Council, Salford City Council, Sheffield City Council, South Cambridgeshire District Council, South Hams District Council, South Norfolk and Broadland Council, Southampton City Council, Stevenage Borough Council, Telford and Wrekin Council, Tewkesbury Borough Council, The Royal Borough of Kingston Council, Walsall Council, Waverley Borough Council, West Berkshire Council, West Devon Borough Council, Wiltshire Council, Wyre Forest District Council |
| United Kingdom | Ashfield District Council, Bracknell Forest Council, Bradford Metropolitan District Council, Braintree District Council, Breckland Council, Cambridge City Council, Canterbury City Council, Central Bedfordshire Council, Cheshire East Council, Chesterfield Borough Council, City of York Council, Colchester City Council, Cornwall Council, Derby City Council, East Cambridgeshire District Council, East Herts Council, Eastbourne Borough Council, Elmbridge Borough Council, Environment First, FCC Environment, Guildford Borough Council, Harborough District Council, Harlow Council, Horsham District Council, Huntingdonshire District Council, Lewes District Council, London Borough of Lewisham, Manchester City Council, Middlesbrough Council, Newcastle City Council, Newcastle Under Lyme Borough Council, Newport City Council, North Somerset Council, Nottingham City Council, Peterborough City Council, Richmondshire District Council, Rushmoor Borough Council, Salford City Council, Sheffield City Council, South Cambridgeshire District Council, South Hams District Council, South Norfolk and Broadland Council, Southampton City Council, Stevenage Borough Council, Stockport Council, Telford and Wrekin Council, Tewkesbury Borough Council, The Royal Borough of Kingston Council, Walsall Council, Waverley Borough Council, West Berkshire Council, West Devon Borough Council, Wiltshire Council, Wyre Forest District Council |
| United States of America | City of Pittsburgh, Republic Services, Seattle Public Utilities |
<!--End of country section-->

Expand Down

0 comments on commit 0cbf4bf

Please sign in to comment.