Skip to content

Commit

Permalink
Merge pull request #1342 from dt215git/dudley-fix
Browse files Browse the repository at this point in the history
Update: dudley_gov_uk
  • Loading branch information
5ila5 authored Oct 22, 2023
2 parents 21bcbaa + fc4b6fb commit 8c5f2d4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,19 @@
"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:
Expand All @@ -35,30 +47,16 @@ def check_date(self, d: str, t: datetime, y: int):
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,
t=w,
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)
Expand All @@ -71,22 +69,30 @@ def fetch(self):

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
2 changes: 1 addition & 1 deletion tests/bandit.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ tests:
- B318
- B319
- B320
- B325
# - B325
- B602
- B604

0 comments on commit 8c5f2d4

Please sign in to comment.