Rewrite markets code

This commit is contained in:
Edward Betts 2023-11-05 15:00:32 +00:00
parent cfdb06083b
commit 5693b2522f

View file

@ -10,22 +10,28 @@ from .types import Event
uk_tz = pytz.timezone("Europe/London") uk_tz = pytz.timezone("Europe/London")
def windmill_hill(start_date: date, months: int = 24) -> list[Event]: def event(title: str, d: date, start: time, end: time, url: str) -> Event:
"""Windmill Hill Market days for the next 24 months from a given date.""" """Build Event object for market."""
return Event(
name="market",
title=title,
date=uk_tz.localize(datetime.combine(d, start)),
end_date=uk_tz.localize(datetime.combine(d, end)),
url=url,
)
def windmill_hill(start_date: date, end_date: date) -> list[Event]:
"""Windmill Hill Market days betwen start and end dates."""
events: list[Event] = [] events: list[Event] = []
current_date = start_date current_date = start_date
url = ( url = (
"https://www.windmillhillcityfarm.org.uk" "https://www.windmillhillcityfarm.org.uk"
+ "/visit-us/shops-more/windmill-hill-market-bristol-market/" + "/visit-us/shops-more/windmill-hill-market-bristol-market/"
) )
start, end = time(10, 0), time(15, 0)
# To keep count of how many market days have been calculated while current_date < end_date:
count = 0
start = time(10, 0)
end = time(15, 0)
while count < months:
# Skip months outside of April to December # Skip months outside of April to December
if current_date.month < 4 or current_date.month > 12: if current_date.month < 4 or current_date.month > 12:
current_date += relativedelta(months=1) current_date += relativedelta(months=1)
@ -38,15 +44,8 @@ def windmill_hill(start_date: date, months: int = 24) -> list[Event]:
# Include it in the list only if it's on or after the start_date # Include it in the list only if it's on or after the start_date
if first_saturday >= start_date: if first_saturday >= start_date:
events.append( events.append(
Event( event("Windmill Hill Market", first_saturday, start, end, url)
name="market",
title="Windmill Hill Market",
date=uk_tz.localize(datetime.combine(first_saturday, start)),
end_date=uk_tz.localize(datetime.combine(first_saturday, end)),
url=url,
) )
)
count += 1
# Move to the next month # Move to the next month
current_date += relativedelta(months=1) current_date += relativedelta(months=1)
@ -55,33 +54,22 @@ def windmill_hill(start_date: date, months: int = 24) -> list[Event]:
return events return events
def tobacco_factory(start_date: date, weeks: int = 52 * 2) -> list[Event]: def tobacco_factory(start_date: date, end_date: date) -> list[Event]:
"""Tobacco Factory Market days for the next 12 months from a given date.""" """Tobacco Factory Market days for the next 12 months from a given date."""
events: list[Event] = [] events: list[Event] = []
current_date = start_date current_date = start_date
count = 0
url = "https://tobaccofactory.com/whats-on/sunday-market/" url = "https://tobaccofactory.com/whats-on/sunday-market/"
start, end = time(10, 0), time(14, 30)
start = time(10, 0) while current_date <= end_date:
end = time(14, 30)
while count < weeks: # 52 weeks in a year
# Calculate the next Sunday from the current date # Calculate the next Sunday from the current date
next_sunday = current_date + relativedelta(weekday=6) # Sunday is 6 next_sunday = current_date + relativedelta(weekday=6) # Sunday is 6
# Include it in the list only if it's on or after the start_date # Include it in the list only if it's on or after the start_date
if next_sunday >= start_date: if next_sunday >= start_date:
events.append( events.append(
Event( event("Tobacco Factory Sunday Market", next_sunday, start, end, url)
name="market",
title="Tobacco Factory Sunday Market",
date=uk_tz.localize(datetime.combine(next_sunday, start)),
end_date=uk_tz.localize(datetime.combine(next_sunday, end)),
url=url,
) )
)
count += 1
# Move to the next week # Move to the next week
current_date += timedelta(weeks=1) current_date += timedelta(weeks=1)
@ -89,27 +77,21 @@ def tobacco_factory(start_date: date, weeks: int = 52 * 2) -> list[Event]:
return events return events
def nailsea_farmers(start_date: date, months: int = 24) -> list[Event]: def nailsea_farmers(start_date: date, end_date: date) -> list[Event]:
"""Nailsea Farmers Market days for the next 12 months from a given date.""" """Nailsea Farmers Market days between start and end dates."""
events: list[Event] = [] events: list[Event] = []
current_date = start_date current_date = start_date
count = 0 start, end = time(9, 0), time(13, 0) # Times: 9am-1pm
url = "https://www.somersetfarmersmarkets.co.uk/markets/nailsea/"
t = time(9, 0) # The market starts at 9am while current_date < end_date:
while count < months:
# Calculate the 3rd Saturday of the current month # Calculate the 3rd Saturday of the current month
third_saturday = current_date + relativedelta(day=1, weekday=SA(+3)) third_saturday = current_date + relativedelta(day=1, weekday=SA(+3))
count += 1
# Include it in the list only if it's on or after the start_date # Include it in the list only if it's on or after the start_date
if third_saturday >= start_date: if third_saturday >= start_date:
events.append( events.append(
Event( event("Nailsea Farmers Market", third_saturday, start, end, url)
name="market",
title="Nailsea Farmers Market",
date=uk_tz.localize(datetime.combine(third_saturday, t)),
)
) )
# Move to the next month # Move to the next month