Skip to content

Commit

Permalink
fix lewisham_gov_uk
Browse files Browse the repository at this point in the history
by adding headers and modifing request data
  • Loading branch information
5ila5 committed Dec 29, 2024
1 parent 54b5035 commit 02540c6
Showing 1 changed file with 16 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,11 @@
ID_SEPERATOR = "-----------------------------{rand_id}"
PAYLOAD_SECTION_TEMPLATE = (
ID_SEPERATOR
+ """
Content-Disposition: form-data; name="{name}"
{value}
"""
+ """\r\nContent-Disposition: form-data; name="{name}"\r\n\r\n{value}\r\n"""
)

HEADERS = {"User-Agent": "Mozilla/5.0"}


class InsufficientDataError(Exception):
pass
Expand All @@ -58,7 +56,11 @@ def get_uprn(self) -> None:
if not self._uprn:
# look up the UPRN for the address
p = {"postcodeOrStreet": self._post_code}
r = requests.post(ADDRESS_SEARCH_URL, params=p)
r = requests.post(
ADDRESS_SEARCH_URL,
params=p,
headers=HEADERS,
)
r.raise_for_status()
addresses = r.json()

Expand Down Expand Up @@ -86,7 +88,8 @@ def fetch(self) -> list[Collection]:
s = requests.Session()

r = s.get(
"https://lewisham.gov.uk/myservices/recycling-and-rubbish/your-bins/collection"
"https://lewisham.gov.uk/myservices/recycling-and-rubbish/your-bins/collection",
headers=HEADERS,
)
r.raise_for_status()
soup = BeautifulSoup(r.text, "html.parser")
Expand Down Expand Up @@ -118,8 +121,9 @@ def fetch(self) -> list[Collection]:
rand_id=rand_id, name=name, value=value
)

payload += ID_SEPERATOR.format(rand_id=rand_id) + "--"
payload += ID_SEPERATOR.format(rand_id=rand_id) + "--\r\n"
headers = {
**HEADERS,
"Content-Type": f"multipart/form-data; boundary=---------------------------{rand_id}",
"Accept": "*/*",
"X-Requested-With": "XMLHttpRequest",
Expand All @@ -134,7 +138,10 @@ def fetch(self) -> list[Collection]:
if url.startswith("/"):
url = BASE_URL + url

r = s.get(url)
r = s.get(
url,
headers=HEADERS,
)
r.raise_for_status()

soup = BeautifulSoup(r.text, "html.parser")
Expand Down

0 comments on commit 02540c6

Please sign in to comment.