Skip to content

Commit

Permalink
Fix date parsing for kingston.gov.uk. Fixes #3224 (#3225)
Browse files Browse the repository at this point in the history
* Move date parsing into its own function so it can be a little more complex and parse yearless date strings

* Fix year boundary handling

* Convert datetime objects to date objects before comparing them
  • Loading branch information
cmsj authored Jan 17, 2025
1 parent 59cacf2 commit 624a23f
Showing 1 changed file with 17 additions and 17 deletions.
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()
date = datetime.strptime(date_string + f" {now.year}", "%A %d %B %Y").date()

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

return 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

0 comments on commit 624a23f

Please sign in to comment.