USING: urls sequences accessors kernel assocs parser quotations arrays unicode math combinators json ; USING: io io.streams.string io.encodings.utf8 io.encodings.string ; USING: math.parser math.text.english ; USING: calendar calendar.english calendar.format calendar.parser ; USING: http.client ; IN: triangle-beer-league.daysmart TUPLE: team id color ; TUPLE: game-summary ot? home-score away-score ; TUPLE: team-schedule start end home away summary ; CONSTANT: teams { T{ team f 26398 "green" } T{ team f 26400 "maroon" } T{ team f 26396 "white" } T{ team f 26397 "blue" } T{ team f 26403 "teal" } T{ team f 26404 "yellow" } T{ team f 26399 "light blue" } T{ team f 26402 "red" } T{ team f 26401 "orange" } T{ team f 26395 "black" } } CONSTANT: tbl-league-id 6551 CONSTANT: daysmart-base-url URL" https://apps.daysmartrecreation.com/dash/jsonapi/api/v1/" : timestamp>filter-string ( timestamp -- str ) [ { YYYY-MM-DD " " hh:mm:ss } formatted ] with-string-writer ; : relative-hh ( timestamp -- ) hour>> 12 mod write-00 ; : timestamp>relative-hh:mm ( timestamp -- str ) [ { relative-hh ":" mm } formatted ] with-string-writer ; ! look up a team by id in the teams sequence : ( id -- team ) teams [ id>> = ] with find nip ; : tbl-game-events-params ( start end -- assoc ) [ timestamp>filter-string ] bi@ tbl-league-id '{ { "cache" f } { "sort" "start" } { "company" "dreamsports" } { "include" "resource.facility,homeTeam.league,visitingTeam.league,eventType" } { "filter[start__gte]" _ } { "filter[end__lte]" _ } { "filter[homeTeam.league_id]" _ } } ; ! maps the league event data to a list of team schedules : tbl-game-events>team-schedules ( events -- team-schedules ) [ "attributes" of { [ "start" of rfc3339>timestamp ] [ "end" of rfc3339>timestamp ] [ "hteam_id" of ] [ "vteam_id" of ] [ "is_overtime" of ] [ "hscore" of ] [ "vscore" of ] } cleave dup json-null? [ 3drop f ] [ [ number>string ] bi@ game-summary boa ] if team-schedule boa ] { } map-as ; : ( params -- url ) [ daysmart-base-url URL" events" derive-url ] dip set-query-params ; : tbl-game-events ( params -- seq ) http-get nip utf8 decode json> "data" of ; : tbl-game-schedules ( -- assoc ) now [ wednesday midnight ] [ thursday end-of-day ] bi 2dup [ 2array ] 2dip tbl-game-events-params tbl-game-events tbl-game-events>team-schedules [ start>> wednesday? ] partition 2array zip ;