refactor stockport_gov_uk

This commit is contained in:
mampfes
2023-01-18 21:04:13 +01:00
parent 34c505b1bd
commit b1602eeca7
4 changed files with 116 additions and 124 deletions

View File

@@ -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 - [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 - [Southampton City Council](/doc/source/southampton_gov_uk.md) / southampton.gov.uk
- [Stevenage Borough Council](/doc/source/stevenage_gov_uk.md) / stevenage.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 - [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 - [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 - [The Royal Borough of Kingston Council](/doc/source/kingston_gov_uk.md) / kingston.gov.uk

View File

@@ -1,74 +1,65 @@
from datetime import datetime import logging
import re
import requests from datetime import datetime
import re
from waste_collection_schedule import Collection # type: ignore[attr-defined] import requests
from bs4 import BeautifulSoup
from bs4 import BeautifulSoup from waste_collection_schedule import Collection # type: ignore[attr-defined]
import logging TITLE = "Stockport Council"
DESCRIPTION = "Source for bin collection services for Stockport Council, UK.\n Refactored with thanks from the Manchester equivalent"
TITLE = "stockport.gov.uk" URL = "https://stockport.gov.uk"
DESCRIPTION = "Source for bin collection services for Stockport Council, UK.\n Refactored with thanks from the Manchester equivalent" TEST_CASES = {
URL = "https://myaccount.stockport.gov.uk/bin-collections/show/" "domestic": {"uprn": "100011460157"},
TEST_CASES = { }
"domestic": {'uprn': '100011460157'},
} ICON_MAP = {
"Black bin": "mdi:trash-can",
ICONS = { "Blue bin": "mdi:recycle",
"Black bin": "mdi:trash-can", "Brown bin": "mdi:glass-fragile",
"Blue bin": "mdi:recycle", "Green bin": "mdi:leaf",
"Brown bin": "mdi:glass-fragile", }
"Green bin": "mdi:leaf",
} _LOGGER = logging.getLogger(__name__)
_LOGGER = logging.getLogger(__name__)
class Source:
def __init__(self, uprn):
class Source: self._uprn = uprn
def __init__(
self, uprn: int = None def fetch(self):
):
self._uprn = uprn r = requests.get(
if not self._uprn: f"https://myaccount.stockport.gov.uk/bin-collections/show/{self._uprn}"
_LOGGER.error( )
"uprn must be provided in config"
) soup = BeautifulSoup(r.text, features="html.parser")
self._session = requests.Session()
bins = soup.find_all(
def fetch(self): "div", {"class": re.compile("service-item service-item-.*")}
entries = [] )
bin_URL = URL + self._uprn bins = str(bins)
header_string = "<h3>"
r = requests.get( bin_name_start_position = bins.find(header_string)
bin_URL,
) entries = []
while bin_name_start_position != -1:
soup = BeautifulSoup(r.text, features="html.parser") bin_name_start_position += len(header_string)
bin_name_end_position = bins.find(" bin", bin_name_start_position) + 4
results = soup.find_all("div", {"class": "collection"}) bin_name = bins[bin_name_start_position:bin_name_end_position]
bins = soup.find_all("div" ,{"class": re.compile("service-item service-item-.*")}) bin_date_pos = bins.find("<p>", bin_name_start_position)
bin_date_exc_day_of_week_pos = bins.find(", ", bin_date_pos) + 2
bins = str(bins) bin_date_end_pos = bins.find("</p>", bin_date_exc_day_of_week_pos)
header_string = "<h3>" bin_date_string = bins[bin_date_exc_day_of_week_pos:bin_date_end_pos]
bin_name_start_position = bins.find(header_string) bin_date = datetime.strptime(bin_date_string, "%d %B %Y").date()
while bin_name_start_position != -1: entries.append(
bin_name_start_position += len(header_string) Collection(
bin_name_end_position = bins.find(" bin",bin_name_start_position) + 4 date=bin_date,
bin_name = bins[bin_name_start_position:bin_name_end_position] t=bin_name,
bin_date_pos = bins.find("<p>",bin_name_start_position) icon=ICON_MAP.get(bin_name),
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_name_start_position = bins.find(header_string, bin_date_end_pos)
bin_date = datetime.strptime(bin_date_string,'%d %B %Y').date()
entries.append( return entries
Collection(
date=bin_date,
t=bin_name,
icon=ICONS[bin_name],
)
)
bin_name_start_position = bins.find(header_string,bin_date_end_pos)
return entries

View File

@@ -1,49 +1,49 @@
# Stockport Metropolitan Borough Council # Stockport Metropolitan Borough Council
Support for schedules provided by [Stockport Metropolitan Borough Support for schedules provided by [Stockport Metropolitan Borough
Council](https://www.manchester.gov.uk/bincollections/), serving the Council](https://www.manchester.gov.uk/bincollections/), serving the
area of Stockport, UK. area of Stockport, UK.
With thanks to the creator of the schedule for Manchester, UK, from With thanks to the creator of the schedule for Manchester, UK, from
whom some of the code is re-factored. whom some of the code is re-factored.
## Configuration via configuration.yaml ## Configuration via configuration.yaml
```yaml ```yaml
waste_collection_schedule: waste_collection_schedule:
sources: sources:
- name: stockport_gov_uk - name: stockport_gov_uk
args: args:
uprn: UPRN_CODE uprn: UPRN_CODE
``` ```
### Configuration Variables ### Configuration Variables
**uprn**<br> **uprn**<br>
*(string) (required)* *(string) (required)*
## Example ## Example
```yaml ```yaml
waste_collection_schedule: waste_collection_schedule:
sources: sources:
- name: stockport_gov_uk - name: stockport_gov_uk
args: args:
uprn: "10090543805" uprn: "10090543805"
``` ```
## How to get the source argument ## How to get the source argument
The UPRN code can be found in the page by entering your postcode on the The UPRN code can be found in the page by entering your postcode on the
[Stockport Bin Collections page [Stockport Bin Collections page
](https://www.stockport.gov.uk/find-your-collection-day/). ](https://www.stockport.gov.uk/find-your-collection-day/).
Select your house from the drop-down, then the UPRN is the final part of Select your house from the drop-down, then the UPRN is the final part of
the URL that you are re-directed to. the URL that you are re-directed to.
For example, for post-code SK6 3AA, house number 25, 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 https://myaccount.stockport.gov.uk/bin-collections/show/10090543805
The UPRN is 10090543805 The UPRN is 10090543805

View File

@@ -28,7 +28,7 @@ Waste collection schedules from service provider web sites are updated daily, de
| Poland | Ecoharmonogram, Warsaw | | 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 | | 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 | | 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 | | United States of America | City of Pittsburgh, Republic Services, Seattle Public Utilities |
<!--End of country section--> <!--End of country section-->