Calculate Refuse collection dates from day name

This commit is contained in:
dt215git
2023-10-21 14:40:07 +01:00
parent 21bcbaa537
commit fc4b6fbb83
2 changed files with 26 additions and 20 deletions

View File

@@ -15,7 +15,19 @@ TEST_CASES = {
"Test_004": {"uprn": 90092621},
}
ICON_MAP = {"RECYCLING": "mdi:recycle", "GARDEN": "mdi:leaf", "REFUSE": "mdi:trash-can"}
REGEX = r"(\d+ \w{3})"
REGEX = {
"DATES": r"(\d+ \w{3})",
"DAYS": r"every: (Monday|Tuesday|Wednesday|Thursday|Friday)",
}
DAYS = {
"Monday": 0,
"Tuesday": 1,
"Wednesday": 2,
"Thursday": 3,
"Friday": 4,
"Saturday": 5,
"Sunday": 6,
}
class Source:
@@ -35,12 +47,6 @@ class Source:
return date.date()
def append_entries(self, d: datetime, w: str, e: list) -> list:
"""
Append provided entry and Refuse entry for the same day.
Refuse is collected on the same dates as alternating Recycling/Garden collections,
so create two entries for each date Refuse & Recycling, or Refuse & Garden
"""
e.append(
Collection(
date=d,
@@ -48,17 +54,9 @@ class Source:
icon=ICON_MAP.get(w.upper()),
)
)
e.append(
Collection(
date=d,
t="Refuse",
icon=ICON_MAP.get("REFUSE"),
)
)
return e
def fetch(self):
today = datetime.now()
today = today.replace(hour=0, minute=0, second=0, microsecond=0)
yr = int(today.year)
@@ -71,22 +69,30 @@ class Source:
panel = soup.find("div", {"aria-label": "Refuse and Recycling Collection"})
panel_data = panel.find("div", {"class": "atPanelData"})
panel_data = panel_data.text.split("Next")[
waste_data = panel_data.text.split("Next")[
1:
] # remove first element it just contains general info
entries = []
for item in panel_data:
# Deal with Recycling and Garden collections
for item in waste_data:
text = item.replace("\r\n", "").strip()
if "recycling" in text:
dates = re.findall(REGEX, text)
dates = re.findall(REGEX["DATES"], text)
for dt in dates:
dt = self.check_date(dt, today, yr)
self.append_entries(dt, "Recycling", entries)
elif "garden" in text:
dates = re.findall(REGEX, text)
dates = re.findall(REGEX["DATES"], text)
for dt in dates:
dt = self.check_date(dt, today, yr)
self.append_entries(dt, "Garden", entries)
# Refuse collections only have a DAY not a date, so work out dates for the next few collections
refuse_day = re.findall(REGEX["DAYS"], panel_data.text)[0]
refuse_date = today + timedelta((int(DAYS[refuse_day]) - today.weekday()) % 7)
for i in range(0, 4):
temp_date = refuse_date + timedelta(days=7 * i)
self.append_entries(temp_date.date(), "Refuse", entries)
return entries

View File

@@ -12,6 +12,6 @@ tests:
- B318
- B319
- B320
- B325
# - B325
- B602
- B604