Updated sheetsync to calculate shifts

pull/449/head
Christopher Usher 3 weeks ago
parent a0953ecdcb
commit 1fb3fca993

@ -12,6 +12,7 @@ setup(
"psycopg2", "psycopg2",
"python-dateutil", "python-dateutil",
"requests", "requests",
"tzinfo",
"wubloader-common", "wubloader-common",
], ],
) )

@ -2,6 +2,7 @@
import json import json
import logging import logging
import signal import signal
import zoneinfo
from collections import defaultdict from collections import defaultdict
from urllib.parse import urlparse from urllib.parse import urlparse
@ -276,10 +277,13 @@ class EventsSync(SheetSync):
"category", "category",
} }
def __init__(self, name, middleware, stop, dbmanager, reverse_sync=False, media_dir=None): def __init__(self, name, middleware, stop, dbmanager, reverse_sync=False, media_dir=None, timezone=None, shifts=None):
super().__init__(name, middleware, stop, dbmanager, reverse_sync) super().__init__(name, middleware, stop, dbmanager, reverse_sync)
self.media_dir = media_dir self.media_dir = media_dir
self.media_downloads = None if media_dir is None else {} self.media_downloads = None if media_dir is None else {}
self.timezone = timezone
self.shifts = shifts
def observe_rows(self, rows): def observe_rows(self, rows):
counts = defaultdict(lambda: 0) counts = defaultdict(lambda: 0)
@ -413,7 +417,14 @@ class PlaylistsSync(SheetSync):
event_id: The id of the streamlog event to sync event_id: The id of the streamlog event to sync
""", """,
) )
def main(dbconnect, sync_configs, metrics_port=8005, backdoor_port=0, media_dir="."): @argh.arg('--timezone', help="Local timezone for determining shift times")
@argh.arg('--shifts', type=json.loads, help="""
Shift definitions in JSON form.
Always present:
repeating: a list of repeating shifts. Each of these consist of a sequence of shift name, start hour and end hour. The start and end hours are in local time.
one_off: a list of non-repeating shifts. Each of these consist of a sequence of shift name, start and end. A start or end time can be a string repersenting timestamp or a URL or null. If it is a URL, the URL will be queried for a timestamp. If no timezone info is provided the timestamp will be assumed to be UTC. If the start time is None, then the start will be assumed to be the earliest possible datetime; if the end is None, it will be assumed to be the oldest possible datetime. If both the start and end are None, the shift will be ignored.
""")
def main(dbconnect, sync_configs, metrics_port=8005, backdoor_port=0, media_dir=".", shifts=None, timezone=None):
""" """
Sheet sync constantly scans a Google Sheets sheet and a database, copying inputs from the sheet Sheet sync constantly scans a Google Sheets sheet and a database, copying inputs from the sheet
to the DB and outputs from the DB to the sheet. to the DB and outputs from the DB to the sheet.
@ -508,6 +519,9 @@ def main(dbconnect, sync_configs, metrics_port=8005, backdoor_port=0, media_dir=
"archive": ArchiveSync, "archive": ArchiveSync,
}[config["type"]] }[config["type"]]
sync_class_kwargs = {} sync_class_kwargs = {}
if config["type"] == "events":
sync_class_kwargs["timezone"] = zoneinfo.ZoneInfo(timezone)
sync_class_kwargs["shifts"] = shifts
if config["type"] == "events" and config.get("download_media", False): if config["type"] == "events" and config.get("download_media", False):
sync_class_kwargs["media_dir"] = media_dir sync_class_kwargs["media_dir"] = media_dir
sync = sync_class(config["name"], middleware, stop, dbmanager, reverse_sync, **sync_class_kwargs) sync = sync_class(config["name"], middleware, stop, dbmanager, reverse_sync, **sync_class_kwargs)

@ -292,6 +292,11 @@ class SheetsEventsMiddleware(SheetsMiddleware):
"tags": lambda v: ", ".join(v), "tags": lambda v: ", ".join(v),
} }
def get_rows(self):
# only need to update the shifts once per sync
self.latest_shifts = common.shifts.parse_shifts(self.shifts)
return super().get_rows()
def parse_bustime(self, value, preserve_dash=False): def parse_bustime(self, value, preserve_dash=False):
"""Convert from HH:MM or HH:MM:SS format to datetime. """Convert from HH:MM or HH:MM:SS format to datetime.
If preserve_dash=True and value is "--", returns "--" If preserve_dash=True and value is "--", returns "--"
@ -301,7 +306,7 @@ class SheetsEventsMiddleware(SheetsMiddleware):
return None return None
if value.strip() == "--": if value.strip() == "--":
return "--" if preserve_dash else None return "--" if preserve_dash else None
bustime = common.parse_bustime(value) bustime = common.shifts.parse_bustime(value)
return common.bustime_to_dt(self.bustime_start, bustime) return common.bustime_to_dt(self.bustime_start, bustime)
def encode_bustime(self, value): def encode_bustime(self, value):
@ -328,7 +333,7 @@ class SheetsEventsMiddleware(SheetsMiddleware):
if 'tags' in row_dict: if 'tags' in row_dict:
row_dict['tags'] = ( row_dict['tags'] = (
[ [
calculate_shift(row_dict['event_start']) common.shifts.calculate_shift(row_dict['event_start'], self.current_shifts, self.timezone),
row_dict['category'], # category name row_dict['category'], # category name
worksheet, # sheet name worksheet, # sheet name
] + (['Poster Moment'] if row_dict['poster_moment'] else []) ] + (['Poster Moment'] if row_dict['poster_moment'] else [])

Loading…
Cancel
Save