-
Notifications
You must be signed in to change notification settings - Fork 95
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 #992 from m26dvd/master
fix: Council Pack 15
- Loading branch information
Showing
10 changed files
with
902 additions
and
182 deletions.
There are no files selected for viewing
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
67 changes: 67 additions & 0 deletions
67
uk_bin_collection/uk_bin_collection/councils/ArgyllandButeCouncil.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,67 @@ | ||
import time | ||
|
||
import requests | ||
from bs4 import BeautifulSoup | ||
|
||
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: | ||
|
||
user_uprn = kwargs.get("uprn") | ||
check_uprn(user_uprn) | ||
user_uprn = user_uprn.zfill(12) | ||
bindata = {"bins": []} | ||
|
||
URI = "https://www.argyll-bute.gov.uk/rubbish-and-recycling/household-waste/bin-collection" | ||
|
||
data = {"addressSelect": user_uprn} | ||
|
||
s = requests.session() | ||
r = s.post(URI, data=data) | ||
r.raise_for_status() | ||
|
||
soup = BeautifulSoup(r.content, features="html.parser") | ||
soup.prettify() | ||
|
||
# Find the table and extract the rows with bin schedule information | ||
table = soup.find("table", class_="table table-bordered") | ||
rows = table.find_all("tr")[1:] # Skip the header row | ||
|
||
current_year = datetime.now().year | ||
# Loop through each row and extract the bin type and collection date | ||
for row in rows: | ||
cells = row.find_all("td") | ||
bin_type = cells[0].get_text(strip=True) | ||
collection_date = cells[1].get_text(strip=True) | ||
|
||
collection_date = datetime.strptime( | ||
collection_date, | ||
"%A %d %B", | ||
) | ||
|
||
if collection_date.month == 1: | ||
collection_date = collection_date.replace(year=current_year + 1) | ||
else: | ||
collection_date = collection_date.replace(year=current_year) | ||
|
||
dict_data = { | ||
"type": bin_type, | ||
"collectionDate": collection_date.strftime(date_format), | ||
} | ||
bindata["bins"].append(dict_data) | ||
|
||
bindata["bins"].sort( | ||
key=lambda x: datetime.strptime(x.get("collectionDate"), date_format) | ||
) | ||
|
||
return bindata |
105 changes: 105 additions & 0 deletions
105
uk_bin_collection/uk_bin_collection/councils/AshfieldDistrictCouncil.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,105 @@ | ||
import time | ||
from datetime import datetime | ||
|
||
from bs4 import BeautifulSoup | ||
from selenium import webdriver | ||
from selenium.webdriver.common.by import By | ||
from selenium.webdriver.support import expected_conditions as EC | ||
from selenium.webdriver.support.wait import WebDriverWait | ||
|
||
from uk_bin_collection.uk_bin_collection.common import * | ||
from uk_bin_collection.uk_bin_collection.get_bin_data import AbstractGetBinDataClass | ||
|
||
|
||
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: | ||
# Get and check UPRN | ||
user_postcode = kwargs.get("postcode") | ||
user_paon = kwargs.get("paon") | ||
check_paon(user_paon) | ||
check_postcode(user_postcode) | ||
web_driver = kwargs.get("web_driver") | ||
headless = kwargs.get("headless") | ||
bindata = {"bins": []} | ||
|
||
API_URL = "https://portal.digital.ashfield.gov.uk/w/webpage/raise-case?service=bin_calendar" | ||
|
||
# Create Selenium webdriver | ||
driver = create_webdriver(web_driver, headless, None, __name__) | ||
driver.get(API_URL) | ||
|
||
title = WebDriverWait(driver, 10).until( | ||
EC.presence_of_element_located((By.ID, "sub_page_title")) | ||
) | ||
|
||
# Wait for the postcode field to appear then populate it | ||
WebDriverWait(driver, 10).until( | ||
EC.presence_of_element_located( | ||
(By.CSS_SELECTOR, "input.relation_path_type_ahead_search") | ||
) | ||
) | ||
|
||
inputElement_postcode = WebDriverWait(driver, 10).until( | ||
EC.presence_of_element_located( | ||
(By.CSS_SELECTOR, "input.relation_path_type_ahead_search") | ||
) | ||
) | ||
inputElement_postcode.clear() | ||
inputElement_postcode.send_keys(user_postcode) | ||
|
||
# Wait for the 'Select your property' dropdown to appear and select the first result | ||
dropdown = WebDriverWait(driver, 10).until( | ||
EC.element_to_be_clickable( | ||
( | ||
By.CLASS_NAME, | ||
"result_list ", | ||
) | ||
) | ||
) | ||
|
||
address_element = ( | ||
WebDriverWait(driver, 10) | ||
.until( | ||
EC.element_to_be_clickable( | ||
(By.XPATH, f"//li[starts-with(@aria-label, '{user_paon}')]") | ||
) | ||
) | ||
.click() | ||
) | ||
|
||
search_button = WebDriverWait(driver, 10).until( | ||
EC.element_to_be_clickable( | ||
(By.XPATH, "//input[@type='submit' and @value='Search']") | ||
) | ||
) | ||
search_button.click() | ||
|
||
time.sleep(10) | ||
|
||
soup = BeautifulSoup(driver.page_source, features="html.parser") | ||
soup.prettify() | ||
|
||
# Find the table by class name | ||
table = soup.find("table", {"class": "table listing table-striped"}) | ||
|
||
# Iterate over each row in the tbody of the table | ||
for row in table.find("tbody").find_all("tr"): | ||
# Extract the service, day, and date for each row | ||
service = row.find_all("td")[0].get_text(strip=True) | ||
date = row.find_all("td")[2].get_text(strip=True) | ||
|
||
dict_data = { | ||
"type": service, | ||
"collectionDate": datetime.strptime(date, "%a, %d %b %Y").strftime( | ||
date_format | ||
), | ||
} | ||
bindata["bins"].append(dict_data) | ||
|
||
return bindata |
Oops, something went wrong.