Skip to content

Commit

Permalink
refactor chesire_east_gov_uk
Browse files Browse the repository at this point in the history
  • Loading branch information
mampfes committed Jun 4, 2022
1 parent 744fa24 commit e3c6798
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 45 deletions.
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
11 changes: 10 additions & 1 deletion doc/source/cheshire_east_gov_uk.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ waste_collection_schedule:
- name: cheshire_east_gov_uk
args:
uprn: UPRN
```
```yaml
waste_collection_schedule:
sources:
- name: cheshire_east_gov_uk
args:
postcode: POSTCODE
Expand Down Expand Up @@ -37,9 +42,13 @@ waste_collection_schedule:
- name: cheshire_east_gov_uk
args:
uprn: 100010132071
```
```yaml
waste_collection_schedule:
sources:
- name: cheshire_east_gov_uk
args:
postcode: WA16 0AY
name_number: 1
```

0 comments on commit e3c6798

Please sign in to comment.