-
Notifications
You must be signed in to change notification settings - Fork 100
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 #350 from OliverCullimore/157-reigate-banstead
- Loading branch information
Showing
5 changed files
with
168 additions
and
0 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
uk_bin_collection/tests/council_schemas/ReigateAndBansteadBoroughCouncil.schema
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,39 @@ | ||
{ | ||
"$schema": "http://json-schema.org/draft-06/schema#", | ||
"$ref": "#/definitions/Welcome5", | ||
"definitions": { | ||
"Welcome5": { | ||
"type": "object", | ||
"additionalProperties": false, | ||
"properties": { | ||
"bins": { | ||
"type": "array", | ||
"items": { | ||
"$ref": "#/definitions/Bin" | ||
} | ||
} | ||
}, | ||
"required": [ | ||
"bins" | ||
], | ||
"title": "Welcome5" | ||
}, | ||
"Bin": { | ||
"type": "object", | ||
"additionalProperties": false, | ||
"properties": { | ||
"type": { | ||
"type": "string" | ||
}, | ||
"collectionDate": { | ||
"type": "string" | ||
} | ||
}, | ||
"required": [ | ||
"collectionDate", | ||
"type" | ||
], | ||
"title": "Bin" | ||
} | ||
} | ||
} |
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
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
52 changes: 52 additions & 0 deletions
52
uk_bin_collection/tests/outputs/ReigateAndBansteadBoroughCouncil.json
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,52 @@ | ||
{ | ||
"bins": [ | ||
{ | ||
"type": "Food waste", | ||
"collectionDate": "03/10/2023" | ||
}, | ||
{ | ||
"type": "Paper and cardboard", | ||
"collectionDate": "03/10/2023" | ||
}, | ||
{ | ||
"type": "Food waste", | ||
"collectionDate": "10/10/2023" | ||
}, | ||
{ | ||
"type": "Paper and cardboard", | ||
"collectionDate": "10/10/2023" | ||
}, | ||
{ | ||
"type": "Mixed recycling", | ||
"collectionDate": "10/10/2023" | ||
}, | ||
{ | ||
"type": "Refuse", | ||
"collectionDate": "10/10/2023" | ||
}, | ||
{ | ||
"type": "Food waste", | ||
"collectionDate": "17/10/2023" | ||
}, | ||
{ | ||
"type": "Paper and cardboard", | ||
"collectionDate": "17/10/2023" | ||
}, | ||
{ | ||
"type": "Food waste", | ||
"collectionDate": "24/10/2023" | ||
}, | ||
{ | ||
"type": "Paper and cardboard", | ||
"collectionDate": "24/10/2023" | ||
}, | ||
{ | ||
"type": "Mixed recycling", | ||
"collectionDate": "24/10/2023" | ||
}, | ||
{ | ||
"type": "Refuse", | ||
"collectionDate": "24/10/2023" | ||
} | ||
] | ||
} |
69 changes: 69 additions & 0 deletions
69
uk_bin_collection/uk_bin_collection/councils/ReigateAndBansteadBoroughCouncil.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,69 @@ | ||
import time | ||
from bs4 import BeautifulSoup | ||
from selenium import webdriver | ||
from selenium.webdriver.support.ui import WebDriverWait | ||
from selenium.webdriver.common.by import By | ||
from selenium.webdriver.support import expected_conditions as EC | ||
from uk_bin_collection.uk_bin_collection.common import * | ||
from uk_bin_collection.uk_bin_collection.get_bin_data import \ | ||
AbstractGetBinDataClass | ||
|
||
|
||
# import the wonderful Beautiful Soup and the URL grabber | ||
class CouncilClass(AbstractGetBinDataClass): | ||
""" | ||
Concrete classes have to implement all abstract operations of the base | ||
class. They can also override some operations with a default | ||
implementation. | ||
""" | ||
|
||
def parse_data(self, page: str, **kwargs) -> dict: | ||
# Set up Selenium to run 'headless' | ||
options = webdriver.ChromeOptions() | ||
options.add_argument("--headless") | ||
options.add_argument("--no-sandbox") | ||
options.add_argument("--disable-gpu") | ||
options.add_argument("--disable-dev-shm-usage") | ||
options.add_experimental_option("excludeSwitches", ["enable-logging"]) | ||
|
||
user_uprn = kwargs.get("uprn") | ||
check_uprn(user_uprn) | ||
# Pad UPRN with 0's at the start for any that aren't 12 chars | ||
user_uprn = user_uprn.zfill(12) | ||
|
||
# Create Selenium webdriver | ||
driver = webdriver.Chrome(options=options) | ||
driver.get(f"https://my.reigate-banstead.gov.uk/en/service/Bins_and_recycling___collections_calendar?uprn={user_uprn}") | ||
|
||
# Wait for iframe to load and switch to it | ||
WebDriverWait(driver, 30).until(EC.frame_to_be_available_and_switch_to_it((By.ID, 'fillform-frame-1'))) | ||
|
||
# Wait for form | ||
WebDriverWait(driver, 30).until(EC.presence_of_element_located((By.CSS_SELECTOR, 'span[data-name="html2"] > div'))) | ||
|
||
# Make a BS4 object | ||
soup = BeautifulSoup(driver.page_source, features="html.parser") | ||
soup.prettify() | ||
|
||
data = {"bins": []} | ||
section = soup.find("span", {"data-name": "html2"}) | ||
dates = section.find_all("div") | ||
for d in dates: | ||
date = d.find("h3") | ||
collections = d.find_all("li") | ||
if date and collections: | ||
collection_date = datetime.strptime(date.get_text(strip=True), "%A %d %B %Y").strftime(date_format) | ||
for c in collections: | ||
collection_type = c.get_text(strip=True) | ||
if c.get_text(strip=True): | ||
dict_data = { | ||
"type": collection_type, | ||
"collectionDate": collection_date, | ||
} | ||
data["bins"].append(dict_data) | ||
|
||
data["bins"].sort( | ||
key=lambda x: datetime.strptime(x.get("collectionDate"), date_format) | ||
) | ||
|
||
return data |