Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix date parsing for kingston.gov.uk. Fixes #3224 #3225

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ class Source:
def __init__(self, uprn: str):
self._uprn = str(uprn)

def interpret_date(self, date_string):
now = datetime.now()
date = datetime.strptime(date_string + f" {now.year}", "%A %d %B %Y")

if date < now:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is also true for collections on the current date so they will be moved to the next year. You could resolve this by converting now and date from datetime objects to date objects before this instead of at the end of the function.

# This means the next collection crosses a year boundary
date = date.replace(year=now.year+1)

return date.date()

def fetch(self):
s = requests.Session()

Expand Down Expand Up @@ -76,9 +86,7 @@ def fetch(self):
if "echo_refuse_next_date" in data and data["echo_refuse_next_date"]:
entries.append(
Collection(
date=datetime.strptime(
data["echo_refuse_next_date"], "%Y/%m/%d %H:%M:%S"
).date(),
date=self.interpret_date(data["echo_refuse_next_date"]),
t="refuse bin",
icon="mdi:trash-can",
)
Expand All @@ -87,20 +95,16 @@ def fetch(self):
if "echo_food_waste_next_date" in data and data["echo_food_waste_next_date"]:
entries.append(
Collection(
date=datetime.strptime(
data["echo_food_waste_next_date"], "%Y/%m/%d %H:%M:%S"
).date(),
date=self.interpret_date(data["echo_food_waste_next_date"]),
t="food waste bin",
icon="mdi:trash-can",
)
)

if "echo_paper_and_card_next_date" in data and data["echo_paper_and_card_next_date"]:
entries.append(
Collection(
date=datetime.strptime(
data["echo_paper_and_card_next_date"], "%Y/%m/%d %H:%M:%S"
).date(),
date=self.interpret_date(data["echo_paper_and_card_next_date"]),
t="paper and card recycling bin",
icon="mdi:recycle",
)
Expand All @@ -109,20 +113,16 @@ def fetch(self):
if "echo_mixed_recycling_next_date" in data and data["echo_mixed_recycling_next_date"]:
entries.append(
Collection(
date=datetime.strptime(
data["echo_mixed_recycling_next_date"], "%Y/%m/%d %H:%M:%S"
).date(),
date=self.interpret_date(data["echo_mixed_recycling_next_date"]),
t="mixed recycling bin",
icon="mdi:recycle",
)
)

if "echo_garden_waste_next_date" in data and data["echo_garden_waste_next_date"]:
entries.append(
Collection(
date=datetime.strptime(
data["echo_garden_waste_next_date"], "%Y/%m/%d %H:%M:%S"
).date(),
date=self.interpret_date(data["echo_garden_waste_next_date"]),
t="garden waste bin",
icon="mdi:leaf",
)
Expand Down