-
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 #964 from m26dvd/master
feat: Council Pack 13
- Loading branch information
Showing
6 changed files
with
294 additions
and
4 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
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
75 changes: 75 additions & 0 deletions
75
uk_bin_collection/uk_bin_collection/councils/LondonBoroughSutton.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,75 @@ | ||
from time import sleep | ||
|
||
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) | ||
bindata = {"bins": []} | ||
|
||
URI = f"https://waste-services.sutton.gov.uk/waste/{user_uprn}" | ||
|
||
s = requests.Session() | ||
|
||
r = s.get(URI) | ||
while "Loading your bin days..." in r.text: | ||
sleep(2) | ||
r = s.get(URI) | ||
r.raise_for_status() | ||
|
||
soup = BeautifulSoup(r.content, "html.parser") | ||
|
||
current_year = datetime.now().year | ||
next_year = current_year + 1 | ||
|
||
services = soup.find_all("h3", class_="govuk-heading-m waste-service-name") | ||
|
||
for service in services: | ||
bin_type = service.get_text( | ||
strip=True | ||
) # Bin type name (e.g., 'Food waste', 'Mixed recycling') | ||
if bin_type == "Bulky waste": | ||
continue | ||
service_details = service.find_next("div", class_="govuk-grid-row") | ||
|
||
next_collection = ( | ||
service_details.find("dt", string="Next collection") | ||
.find_next_sibling("dd") | ||
.get_text(strip=True) | ||
) | ||
|
||
next_collection = datetime.strptime( | ||
remove_ordinal_indicator_from_date_string(next_collection), | ||
"%A, %d %B", | ||
) | ||
|
||
if next_collection.month == 1: | ||
next_collection = next_collection.replace(year=next_year) | ||
else: | ||
next_collection = next_collection.replace(year=current_year) | ||
|
||
dict_data = { | ||
"type": bin_type, | ||
"collectionDate": next_collection.strftime("%d/%m/%Y"), | ||
} | ||
bindata["bins"].append(dict_data) | ||
|
||
bindata["bins"].sort( | ||
key=lambda x: datetime.strptime(x.get("collectionDate"), "%d/%m/%Y") | ||
) | ||
|
||
return bindata |
93 changes: 93 additions & 0 deletions
93
uk_bin_collection/uk_bin_collection/councils/MidDevonCouncil.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,93 @@ | ||
import time | ||
|
||
import requests | ||
|
||
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) | ||
bindata = {"bins": []} | ||
|
||
SESSION_URL = "https://my.middevon.gov.uk/authapi/isauthenticated?uri=https%253A%252F%252Fmy.middevon.gov.uk%252Fen%252FAchieveForms%252F%253Fform_uri%253Dsandbox-publish%253A%252F%252FAF-Process-2289dd06-9a12-4202-ba09-857fe756f6bd%252FAF-Stage-eb382015-001c-415d-beda-84f796dbb167%252Fdefinition.json%2526redirectlink%253D%25252Fen%2526cancelRedirectLink%253D%25252Fen%2526consentMessage%253Dyes&hostname=my.middevon.gov.uk&withCredentials=true" | ||
|
||
API_URL = "https://my.middevon.gov.uk/apibroker/runLookup" | ||
|
||
payload = { | ||
"formValues": { | ||
"Your Address": { | ||
"listAddress": {"value": user_uprn}, | ||
}, | ||
}, | ||
} | ||
|
||
headers = { | ||
"Content-Type": "application/json", | ||
"Accept": "application/json", | ||
"User-Agent": "Mozilla/5.0", | ||
"X-Requested-With": "XMLHttpRequest", | ||
"Referer": "https://my.middevon.gov.uk/fillform/?iframe_id=fillform-frame-1&db_id=", | ||
} | ||
|
||
ids = [ | ||
"6423144f50ec0", | ||
"641c7ae9b4c96", | ||
"645e13a01dba1", | ||
"642315aacb919", | ||
"64231699483cf", | ||
"642421bab7478", | ||
"6424229605d13", | ||
"645e14020c9cc", | ||
] | ||
|
||
rows_data = [] | ||
|
||
for id in ids: | ||
s = requests.session() | ||
r = s.get(SESSION_URL) | ||
r.raise_for_status() | ||
session_data = r.json() | ||
sid = session_data["auth-session"] | ||
|
||
params = { | ||
"id": id, | ||
"repeat_against": "", | ||
"noRetry": "false", | ||
"getOnlyTokens": "undefined", | ||
"log_id": "", | ||
"app_name": "AF-Renderer::Self", | ||
# unix_timestamp | ||
"_": str(int(time.time() * 1000)), | ||
"sid": sid, | ||
} | ||
r = s.post(API_URL, json=payload, headers=headers, params=params) | ||
r.raise_for_status() | ||
data = r.json() | ||
rows_data = data["integration"]["transformed"]["rows_data"] | ||
if isinstance(rows_data, dict): | ||
date = datetime.strptime(rows_data["0"]["display"], "%d-%b-%y") | ||
bin_types = (rows_data["0"]["CollectionItems"]).split(" and ") | ||
|
||
for bin_type in bin_types: | ||
dict_data = { | ||
"type": bin_type, | ||
"collectionDate": date.strftime(date_format), | ||
} | ||
bindata["bins"].append(dict_data) | ||
|
||
bindata["bins"].sort( | ||
key=lambda x: datetime.strptime(x.get("collectionDate"), "%d/%m/%Y") | ||
) | ||
|
||
return bindata |
63 changes: 63 additions & 0 deletions
63
uk_bin_collection/uk_bin_collection/councils/OxfordCityCouncil.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,63 @@ | ||
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") | ||
user_postcode = kwargs.get("postcode") | ||
check_uprn(user_uprn) | ||
check_postcode(user_postcode) | ||
bindata = {"bins": []} | ||
|
||
session_uri = "https://www.oxford.gov.uk/mybinday" | ||
URI = "https://www.oxford.gov.uk/xfp/form/142" | ||
|
||
session = requests.Session() | ||
token_response = session.get(session_uri) | ||
soup = BeautifulSoup(token_response.text, "html.parser") | ||
token = soup.find("input", {"name": "__token"}).attrs["value"] | ||
|
||
form_data = { | ||
"__token": token, | ||
"page": "12", | ||
"locale": "en_GB", | ||
"q6ad4e3bf432c83230a0347a6eea6c805c672efeb_0_0": user_postcode, | ||
"q6ad4e3bf432c83230a0347a6eea6c805c672efeb_1_0": user_uprn, | ||
"next": "Next", | ||
} | ||
|
||
collection_response = session.post(URI, data=form_data) | ||
|
||
collection_soup = BeautifulSoup(collection_response.text, "html.parser") | ||
for paragraph in collection_soup.find("div", class_="editor").find_all("p"): | ||
matches = re.match(r"^(\w+) Next Collection: (.*)", paragraph.text) | ||
if matches: | ||
collection_type, date_string = matches.groups() | ||
try: | ||
date = datetime.strptime(date_string, "%A %d %B %Y").date() | ||
except ValueError: | ||
date = datetime.strptime(date_string, "%A %d %b %Y").date() | ||
|
||
dict_data = { | ||
"type": collection_type, | ||
"collectionDate": date.strftime("%d/%m/%Y"), | ||
} | ||
bindata["bins"].append(dict_data) | ||
|
||
bindata["bins"].sort( | ||
key=lambda x: datetime.strptime(x.get("collectionDate"), "%d/%m/%Y") | ||
) | ||
|
||
return bindata |
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