Skip to content

Commit

Permalink
feat(event scrape): add filtering of events by time
Browse files Browse the repository at this point in the history
  • Loading branch information
pkheav committed Mar 15, 2021
1 parent 0340904 commit 934633c
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions drivers/place/event_scrape.cr
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ class Place::EventScrape < PlaceOS::Driver
logger.debug { "Getting bookings for zones" }
logger.debug { @zone_ids.inspect }

start_epoch = Time.utc.at_beginning_of_day.to_unix
end_epoch = start_epoch + 86400 # seconds in a day

@zone_ids.each do |z_id|
staff_api.systems(zone_id: z_id).get.as_a.each do |sys|
sys_id = sys["id"].as_s
Expand All @@ -55,19 +58,32 @@ class Place::EventScrape < PlaceOS::Driver
response[:systems][sys_id] = SystemWithEvents.new(
name: sys["name"].as_s,
zones: Array(String).from_json(sys["zones"].to_json),
events: get_system_bookings(sys_id)
events: get_system_bookings(sys_id, start_epoch, end_epoch)
)
end
end

response
end

def get_system_bookings(sys_id : String) : Array(Event)
def get_system_bookings(sys_id : String, start_epoch : Int64?, end_epoch : Int64?) : Array(Event)
booking_module = staff_api.modules_from_system(sys_id).get.as_a.find { |mod| mod["name"] == "Bookings" }
# If the system has a booking module with bookings
if booking_module && (bookings = staff_api.get_module_state(booking_module["id"].as_s).get["bookings"]?)
JSON.parse(bookings.as_s).as_a.map { |b| Event.from_json(b.to_json) }
bookings = JSON.parse(bookings.as_s).as_a.map { |b| Event.from_json(b.to_json) }

# If both start_epoch and end_epoch are passed
if start_epoch && end_epoch
# Convert start/end_epoch to Time object as Event.event_start.class == Time
start_time = Time.unix(start_epoch)
end_time = Time.unix(end_epoch)
range = (start_time..end_time)
# Only select bookings within start_epoch and end_epoch
bookings.select! { |b| range.includes?(b.event_start) }
bookings
end

bookings
else
[] of Event
end
Expand Down

0 comments on commit 934633c

Please sign in to comment.