Split market day code into own file

This commit is contained in:
Edward Betts 2023-11-05 12:24:11 +00:00
parent dd5ec4d233
commit 583bfcac2e
2 changed files with 125 additions and 123 deletions

View file

@ -21,11 +21,11 @@ import pytz
import requests import requests
import yaml import yaml
from dateutil.easter import easter from dateutil.easter import easter
from dateutil.relativedelta import FR, SA, relativedelta from dateutil.relativedelta import FR, relativedelta
from agenda import thespacedevs from agenda import thespacedevs
from . import gwr, waste_schedule from . import gwr, markets, waste_schedule
from .types import Event from .types import Event
warnings.simplefilter(action="ignore", category=FutureWarning) warnings.simplefilter(action="ignore", category=FutureWarning)
@ -315,124 +315,6 @@ def critical_mass(start_date: date, limit: int = 12) -> list[Event]:
return events return events
def windmill_hill_market_days(start_date: date) -> list[Event]:
"""Windmill Hill Market days for the next 12 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
tz = pytz.timezone("Europe/London")
start = time(10, 0)
end = time(15, 0)
while count < 12:
# 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=tz.localize(datetime.combine(first_saturday, start)),
end_date=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_market_days(start_date: date) -> 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/"
tz = pytz.timezone("Europe/London")
start = time(10, 0)
end = time(14, 30)
while count < 52: # 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=tz.localize(datetime.combine(next_sunday, start)),
end_date=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_market_days(start_date: date) -> list[Event]:
"""Nailsea Farmers Market days for the next 12 months from a given date."""
events: list[Event] = []
current_date = start_date
count = 0
tz = pytz.timezone("Europe/London")
t = time(9, 0) # The market starts at 9am
while count < 12:
# 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=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
# Test the function
if __name__ == "__main__":
start_date = date(2023, 10, 29)
print(windmill_hill_market_days(start_date))
def as_date(d: date | datetime) -> date: def as_date(d: date | datetime) -> date:
"""Return date for given date or datetime.""" """Return date for given date or datetime."""
return d.date() if isinstance(d, datetime) else d return d.date() if isinstance(d, datetime) else d
@ -691,9 +573,9 @@ def get_data(now: datetime) -> typing.Mapping[str, str | object]:
"gwr_advance_tickets": gwr.advance_ticket_date(data_dir), "gwr_advance_tickets": gwr.advance_ticket_date(data_dir),
"critical_mass": critical_mass(today), "critical_mass": critical_mass(today),
"market": ( "market": (
windmill_hill_market_days(today) markets.windmill_hill(today)
+ tobacco_factory_market_days(today) + markets.tobacco_factory(today)
+ nailsea_farmers_market_days(today) + markets.nailsea_farmers(today)
), ),
"rockets": thespacedevs.get_launches(rocket_dir, limit=40), "rockets": thespacedevs.get_launches(rocket_dir, limit=40),
} }

120
agenda/markets.py Normal file
View file

@ -0,0 +1,120 @@
"""Market days."""
from datetime import date, datetime, time, timedelta
import pytz
from dateutil.relativedelta import SA, relativedelta
from .types import Event
def windmill_hill(start_date: date) -> list[Event]:
"""Windmill Hill Market days for the next 12 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
tz = pytz.timezone("Europe/London")
start = time(10, 0)
end = time(15, 0)
while count < 12:
# 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=tz.localize(datetime.combine(first_saturday, start)),
end_date=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) -> 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/"
tz = pytz.timezone("Europe/London")
start = time(10, 0)
end = time(14, 30)
while count < 52: # 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=tz.localize(datetime.combine(next_sunday, start)),
end_date=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) -> list[Event]:
"""Nailsea Farmers Market days for the next 12 months from a given date."""
events: list[Event] = []
current_date = start_date
count = 0
tz = pytz.timezone("Europe/London")
t = time(9, 0) # The market starts at 9am
while count < 12:
# 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=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