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

141 lines
4.1 KiB
Factor
Raw Normal View History

2024-01-27 13:27:34 -05:00
USING: urls pcre sequences accessors kernel assocs parser quotations combinators.short-circuit regexp arrays unicode math ;
USING: combinators ;
2023-09-06 12:40:23 -04:00
FROM: regexp => matches? ;
USING: xml.syntax xml.writer ;
2024-01-27 13:27:34 -05:00
USING: json ;
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: html.parser html.parser.analyzer ;
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 ;
! : render-day-header ( timestamp -- xml )
! [ day-of-week day-name ]
! [ month>> month-name ]
! [ day>> dup number>string swap ordinal-suffix append ] tri
! [XML
! <h1 class="day-header">
! <span class="day-name"><-></span>
! <span class="month"><-></span>
! <span class="day"><-></span>
! </h1>
! XML] ;
!
!
! : render-team-times ( schedule -- xml )
! [ away>> color>> [ "team " prepend ] keep ]
! [ home>> color>> [ "team " prepend ] keep ]
! [ team-schedule>time-string ] tri
! [XML
! <div class="team-times">
! <span class="at">@</span>
! <div class="teams">
! <span class=<->><-></span>
! <span class=<->><-></span>
! </div>
! <div class="time"><-></div>
! </div>
! XML] ;
!
! : render-no-games ( -- xml )
! [XML <div class="team-times"><div class="none">No Scheduled TBL games</div></div> XML] ;
!
! : render-day-schedule ( assoc -- xml )
! [ first render-day-header ]
! [ second
! dup empty?
! [ drop render-no-games ]
! [ [ render-team-times ] map ] if
! ] bi
! [XML <section class="game"> <-> <-> </section> XML] ;
!
! : tbl-games? ( -- ? )
! tbl-games empty? not ;
!
! : render-schedule ( -- )
! tbl-games [ render-day-schedule pprint-xml ] each ;