factor-work/triangle-beer-league/daysmart/daysmart.factor

90 lines
2.7 KiB
Factor
Raw Normal View History

2024-01-27 13:31:48 -05:00
USING: urls sequences accessors kernel assocs parser quotations arrays unicode math combinators json ;
2024-01-27 13:27:34 -05:00
USING: io io.streams.string io.encodings.utf8 io.encodings.string ;
2023-09-06 12:40:23 -04:00
USING: math.parser math.text.english ;
2024-01-27 13:27:34 -05:00
USING: calendar calendar.english calendar.format calendar.parser ;
2023-09-06 12:40:23 -04:00
USING: http.client ;
IN: triangle-beer-league.daysmart
2024-01-27 13:27:34 -05:00
TUPLE: team
id color ;
2023-09-06 12:40:23 -04:00
2024-01-27 13:27:34 -05:00
TUPLE: game-summary ot? home-score away-score ;
2023-09-06 12:40:23 -04:00
2024-01-27 13:27:34 -05:00
TUPLE: team-schedule
start end home away summary ;
2023-09-06 12:40:23 -04:00
2024-01-27 13:27:34 -05:00
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" }
}
2023-09-06 12:40:23 -04:00
2024-01-27 13:27:34 -05:00
CONSTANT: tbl-league-id 6551
2023-09-06 12:40:23 -04:00
2024-01-27 13:27:34 -05:00
CONSTANT: daysmart-base-url URL" https://apps.daysmartrecreation.com/dash/jsonapi/api/v1/"
2023-09-06 12:40:23 -04:00
2024-01-27 13:27:34 -05:00
: timestamp>filter-string ( timestamp -- str )
[ { YYYY-MM-DD " " hh:mm:ss } formatted ]
with-string-writer ;
2023-09-06 12:40:23 -04:00
2024-01-27 13:27:34 -05:00
: 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
: <tbl-team> ( id -- team )
teams [ id>> = ] with find nip ;
: tbl-game-events-params ( start end -- assoc )
[ timestamp>filter-string ] bi@
tbl-league-id
2023-09-06 12:40:23 -04:00
'{
2024-01-27 13:27:34 -05:00
{ "cache" f }
{ "sort" "start" }
{ "company" "dreamsports" }
{ "include" "resource.facility,homeTeam.league,visitingTeam.league,eventType" }
{ "filter[start__gte]" _ }
{ "filter[end__lte]" _ }
{ "filter[homeTeam.league_id]" _ }
2023-09-06 12:40:23 -04:00
} ;
2024-01-27 13:27:34 -05:00
! 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 <tbl-team> ]
[ "vteam_id" of <tbl-team> ]
[ "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 ;
: <tbl-events-url> ( params -- url )
[ daysmart-base-url URL" events" derive-url ] dip set-query-params ;
: tbl-game-events ( params -- seq )
<tbl-events-url> 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 ;