agenda/agenda/markets.py

120 lines
3.8 KiB
Python

"""Market days."""
from datetime import date, datetime, time, timedelta
import pytz
from dateutil.relativedelta import SA, relativedelta
from .types import Event
uk_tz = pytz.timezone("Europe/London")
def windmill_hill(start_date: date, months: int = 24) -> list[Event]:
"""Windmill Hill Market days for the next 24 months from a given date."""
events: list[Event] = []
current_date = start_date
url = (
"https://www.windmillhillcityfarm.org.uk"
+ "/visit-us/shops-more/windmill-hill-market-bristol-market/"
)
# To keep count of how many market days have been calculated
count = 0
start = time(10, 0)
end = time(15, 0)
while count < months:
# Skip months outside of April to December
if current_date.month < 4 or current_date.month > 12:
current_date += relativedelta(months=1)
current_date = date(current_date.year, current_date.month, 1)
continue
# Calculate the first Saturday of the current month
first_saturday = current_date + relativedelta(day=1, weekday=SA(+1))
# Include it in the list only if it's on or after the start_date
if first_saturday >= start_date:
events.append(
Event(
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
current_date += relativedelta(months=1)
current_date = date(current_date.year, current_date.month, 1)
return events
def tobacco_factory(start_date: date, weeks: int = 52 * 2) -> list[Event]:
"""Tobacco Factory Market days for the next 12 months from a given date."""
events: list[Event] = []
current_date = start_date
count = 0
url = "https://tobaccofactory.com/whats-on/sunday-market/"
start = time(10, 0)
end = time(14, 30)
while count < weeks: # 52 weeks in a year
# Calculate the next Sunday from the current date
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
if next_sunday >= start_date:
events.append(
Event(
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
current_date += timedelta(weeks=1)
return events
def nailsea_farmers(start_date: date, months: int = 24) -> list[Event]:
"""Nailsea Farmers Market days for the next 12 months from a given date."""
events: list[Event] = []
current_date = start_date
count = 0
t = time(9, 0) # The market starts at 9am
while count < months:
# Calculate the 3rd Saturday of the current month
third_saturday = current_date + relativedelta(day=1, weekday=SA(+3))
# Include it in the list only if it's on or after the start_date
if third_saturday >= start_date:
events.append(
Event(
name="market",
title="Nailsea Farmers Market",
date=uk_tz.localize(datetime.combine(third_saturday, t)),
)
)
count += 1
# Move to the next month
current_date += relativedelta(months=1)
current_date = date(current_date.year, current_date.month, 1)
return events