Split sunrise/sunset code into own file

This commit is contained in:
Edward Betts 2023-11-05 12:29:46 +00:00
parent 7949e16195
commit b52a076c5b
2 changed files with 27 additions and 23 deletions

View file

@ -11,7 +11,6 @@ from typing import List
import dateutil import dateutil
import dateutil.parser import dateutil.parser
import dateutil.tz import dateutil.tz
import ephem
import exchange_calendars import exchange_calendars
import holidays import holidays
import lxml import lxml
@ -24,7 +23,7 @@ from dateutil.relativedelta import FR, relativedelta
from agenda import thespacedevs from agenda import thespacedevs
from . import fx, gwr, markets, waste_schedule from . import fx, gwr, markets, sun, waste_schedule
from .types import Event from .types import Event
warnings.simplefilter(action="ignore", category=FutureWarning) warnings.simplefilter(action="ignore", category=FutureWarning)
@ -35,7 +34,6 @@ warnings.simplefilter(action="ignore", category=FutureWarning)
# morzine ski lifts # morzine ski lifts
# chalet availablity calendar # chalet availablity calendar
# sunrise and sunset
# starlink visible # starlink visible
here = dateutil.tz.tzlocal() here = dateutil.tz.tzlocal()
@ -450,23 +448,6 @@ def bristol_waste_collection_events(start_date: date) -> list[Event]:
return waste_schedule.get_bristol_gov_uk(start_date, data_dir, uprn) return waste_schedule.get_bristol_gov_uk(start_date, data_dir, uprn)
def bristol() -> ephem.Observer:
"""Location of Bristol."""
observer = ephem.Observer()
observer.lat, observer.lon = "51.4545", "-2.5879"
return observer
def sunrise(observer: ephem.Observer) -> datetime:
"""Sunrise."""
return typing.cast(datetime, observer.next_rising(ephem.Sun(observer)).datetime())
def sunset(observer: ephem.Observer) -> datetime:
"""Sunrise."""
return typing.cast(datetime, observer.next_setting(ephem.Sun(observer)).datetime())
def build_events_for_calendar(events: list[Event]) -> list[dict[str, typing.Any]]: def build_events_for_calendar(events: list[Event]) -> list[dict[str, typing.Any]]:
"""Build list of events for FullCalendar.""" """Build list of events for FullCalendar."""
items: list[dict[str, typing.Any]] = [] items: list[dict[str, typing.Any]] = []
@ -562,9 +543,9 @@ def get_data(now: datetime) -> typing.Mapping[str, str | object]:
event = Event(name=key, date=value) event = Event(name=key, date=value)
events.append(event) events.append(event)
observer = bristol() observer = sun.bristol()
reply["sunrise"] = sunrise(observer) reply["sunrise"] = sun.sunrise(observer)
reply["sunset"] = sunset(observer) reply["sunset"] = sun.sunset(observer)
for key, value in xmas_last_posting_dates.items(): for key, value in xmas_last_posting_dates.items():
events.append(Event(name=f"xmas_last_{key}", date=value)) events.append(Event(name=f"xmas_last_{key}", date=value))

23
agenda/sun.py Normal file
View file

@ -0,0 +1,23 @@
"""Sunrise and sunset times."""
import typing
from datetime import datetime
import ephem
def bristol() -> ephem.Observer:
"""Location of Bristol."""
observer = ephem.Observer()
observer.lat, observer.lon = "51.4545", "-2.5879"
return observer
def sunrise(observer: ephem.Observer) -> datetime:
"""Sunrise."""
return typing.cast(datetime, observer.next_rising(ephem.Sun(observer)).datetime())
def sunset(observer: ephem.Observer) -> datetime:
"""Sunrise."""
return typing.cast(datetime, observer.next_setting(ephem.Sun(observer)).datetime())