Skip to content

Commit

Permalink
dudley_gov_uk add xmas holiday check
Browse files Browse the repository at this point in the history
  • Loading branch information
5ila5 committed Dec 18, 2023
1 parent f5817f8 commit 084b694
Showing 1 changed file with 40 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import re
from datetime import datetime, timedelta
from datetime import date, datetime, timedelta

import requests
from bs4 import BeautifulSoup
Expand Down Expand Up @@ -41,7 +41,10 @@ def check_date(self, d: str, t: datetime, y: int):
This tries to deal year-end dates when the YEAR is missing
"""
d += " " + str(y)
date = datetime.strptime(d, "%d %b %Y")
try:
date = datetime.strptime(d, "%d %b %Y")
except ValueError:
date = datetime.strptime(d, "%A %d %b %Y")
if (date - t) < timedelta(days=-31):
date = date.replace(year=date.year + 1)
return date.date()
Expand All @@ -56,6 +59,32 @@ def append_entries(self, d: datetime, w: str, e: list) -> list:
)
return e

def get_xmas_map(self, footer_panel) -> dict[date, date]:
if not (
footer_panel
and footer_panel.find("table")
and footer_panel.find("table").find("tr")
):
print(
footer_panel,
footer_panel.find("table"),
footer_panel.find("table").find("tr"),
)
return {}
xmas_map: dict = {}
today = datetime.now()
yr = int(today.year)
for tr in footer_panel.find("table").findAll("tr")[1:]:
try:
moved, moved_to = tr.findAll("td")
moved = self.check_date(moved.text, today, yr)
moved_to = self.check_date(moved_to.text, today, yr)
xmas_map[moved] = moved_to
except Exception as e:
print(e)
continue
return xmas_map

def fetch(self):
today = datetime.now()
today = today.replace(hour=0, minute=0, second=0, microsecond=0)
Expand All @@ -73,6 +102,10 @@ def fetch(self):
1:
] # remove first element it just contains general info

# get table of holiday moved dates (only around xmas)
footer_panel = panel.find("div", {"class": "atPanelFooter"})
xmas_map = self.get_xmas_map(footer_panel)

entries = []
# Deal with Recycling and Garden collections
for item in waste_data:
Expand All @@ -81,18 +114,21 @@ def fetch(self):
dates = re.findall(REGEX["DATES"], text)
for dt in dates:
dt = self.check_date(dt, today, yr)
dt = xmas_map.get(dt, dt)
self.append_entries(dt, "Recycling", entries)
elif "garden" in text:
dates = re.findall(REGEX["DATES"], text)
for dt in dates:
dt = self.check_date(dt, today, yr)
dt = xmas_map.get(dt, dt)
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)
temp_date = (refuse_date + timedelta(days=7 * i)).date()
temp_date = xmas_map.get(temp_date, temp_date)
self.append_entries(temp_date, "Refuse", entries)

return entries

0 comments on commit 084b694

Please sign in to comment.