-
-
Notifications
You must be signed in to change notification settings - Fork 703
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
51 additions
and
45 deletions.
There are no files selected for viewing
85 changes: 41 additions & 44 deletions
85
...onents/waste_collection_schedule/waste_collection_schedule/source/cheshire_east_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 |
---|---|---|
@@ -1,82 +1,79 @@ | ||
import logging | ||
|
||
from datetime import datetime | ||
from bs4 import BeautifulSoup | ||
|
||
import requests | ||
import urllib.parse | ||
from bs4 import BeautifulSoup | ||
from waste_collection_schedule import Collection | ||
|
||
TITLE = "cheshireeast.gov.uk" | ||
DESCRIPTION = ( | ||
"Source for cheshireeast.gov.uk services for Cheshire East" | ||
) | ||
DESCRIPTION = "Source for cheshireeast.gov.uk services for Cheshire East" | ||
URL = "cheshireeast.gov.uk" | ||
TEST_CASES = { | ||
"houseUPRN" : {"uprn": "100010132071"}, | ||
"houseAddress": {"postcode":"WA16 0AY", "name_number": "1"} | ||
} | ||
|
||
API_URLS = { | ||
"address_search": "https://online.cheshireeast.gov.uk/MyCollectionDay/SearchByAjax/Search", | ||
"collection": "https://online.cheshireeast.gov.uk/MyCollectionDay/SearchByAjax/GetBartecJobList?uprn={}", | ||
|
||
TEST_CASES = { | ||
"houseUPRN": {"uprn": "100010132071"}, | ||
"houseAddress": {"postcode": "WA16 0AY", "name_number": "1"}, | ||
} | ||
|
||
ICONS = { | ||
ICON_MAP = { | ||
"General Waste": "mdi:trash-can", | ||
"Mixed Recycling": "mdi:recycle", | ||
"Garden Waste": "mdi:leaf", | ||
} | ||
|
||
_LOGGER = logging.getLogger(__name__) | ||
|
||
|
||
class Source: | ||
def __init__(self, uprn=None, postcode=None, name_number=None): | ||
self._uprn = uprn | ||
self._postcode = postcode | ||
self.name_number = name_number | ||
self._name_number = name_number | ||
|
||
def fetch(self): | ||
session = requests.Session() | ||
responseContent=None | ||
|
||
if(self._postcode and self.name_number): | ||
if self._postcode and self._name_number: | ||
# Lookup postcode and number to get UPRN | ||
r = session.get(f"https://online.cheshireeast.gov.uk/MyCollectionDay/SearchByAjax/Search?postcode={urllib.parse.quote(self._postcode.encode('utf8'))}&propertyname={self.name_number}") | ||
params = { | ||
"postcode": self._postcode, | ||
"propertyname": self._name_number, | ||
} | ||
r = session.get( | ||
"https://online.cheshireeast.gov.uk/MyCollectionDay/SearchByAjax/Search", | ||
params=params, | ||
) | ||
r.raise_for_status() | ||
soup = BeautifulSoup(r.text, features="html.parser") | ||
s = soup.find("a", attrs={"class": "get-job-details"}) | ||
self._uprn = s['data-uprn'] | ||
print(s) | ||
if s is None: | ||
raise Exception("address not found") | ||
self._uprn = s["data-uprn"] | ||
|
||
if self._uprn is None: | ||
raise Exception("uprn not set") | ||
|
||
params = {"uprn": self._uprn} | ||
r = session.get( | ||
"https://online.cheshireeast.gov.uk/MyCollectionDay/SearchByAjax/GetBartecJobList", | ||
params=params, | ||
) | ||
r.raise_for_status() | ||
|
||
if self._uprn: | ||
r = session.get(f"https://online.cheshireeast.gov.uk/MyCollectionDay/SearchByAjax/GetBartecJobList?uprn={self._uprn}") | ||
responseContent = r.text | ||
|
||
soup = BeautifulSoup(responseContent, features="html.parser") | ||
soup = BeautifulSoup(r.text, features="html.parser") | ||
s = soup.find_all("td", attrs={"class": "visible-cell"}) | ||
|
||
entries = [] | ||
|
||
for cell in s: | ||
labels = cell.find_all("label") | ||
date = None | ||
type = None | ||
if labels: | ||
for i,label in enumerate(labels): | ||
if(i == 1): | ||
date = datetime.strptime(label.text, "%d/%m/%Y") | ||
if(i == 2): | ||
for round_type in ICONS: | ||
if round_type.upper() in label.text.upper(): | ||
type = round_type | ||
date = datetime.strptime(labels[1].text, "%d/%m/%Y").date() | ||
type = labels[2].text.removeprefix("Empty Standard ") | ||
entries.append( | ||
Collection( | ||
date = date.date(), | ||
t = type, | ||
icon = ICONS.get(type), | ||
) | ||
Collection( | ||
date=date, | ||
t=type, | ||
icon=ICON_MAP.get(type), | ||
) | ||
) | ||
|
||
return entries | ||
|
||
|
||
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