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

90 lines
2.7 KiB
Factor

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 26812 "green" }
T{ team f 26814 "maroon" }
T{ team f 26818 "white" }
T{ team f 26811 "blue" }
T{ team f 26817 "teal" }
T{ team f 26819 "yellow" }
T{ team f 26813 "light blue" }
T{ team f 26816 "red" }
T{ team f 26815 "orange" }
T{ team f 26810 "black" }
}
CONSTANT: tbl-league-id 6570
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
: <tbl-team> ( 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 <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 ;