update for new api

master
Steve Ayerhart 2024-01-27 13:27:34 -05:00
parent 94a1b78664
commit 996c7778c7
Signed by: steve
GPG Key ID: BB368A0698AE49B7
4 changed files with 157 additions and 128 deletions

View File

@ -1,141 +1,140 @@
USING: urls pcre sequences accessors kernel assocs parser quotations combinators.short-circuit regexp arrays unicode ;
USING: urls pcre sequences accessors kernel assocs parser quotations combinators.short-circuit regexp arrays unicode math ;
USING: combinators ;
FROM: regexp => matches? ;
USING: xml.syntax xml.writer ;
USING: io io.streams.string ;
USING: json ;
USING: io io.streams.string io.encodings.utf8 io.encodings.string ;
USING: math.parser math.text.english ;
USING: calendar calendar.english calendar.format ;
USING: calendar calendar.english calendar.format calendar.parser ;
USING: html.parser html.parser.analyzer ;
USING: http.client ;
USING: triangle-beer-league.entities ;
IN: triangle-beer-league.daysmart
CONSTANT: daysmart-base-url URL" https://apps.daysmartrecreation.com/dash/index.php?company=dreamsports"
CONSTANT: datetime-regex "^(?<month>\\w{3}) (?<day>\\d{0,2}).{7}(?<hour>\\d{0,2}):(?<minute>\\d{0,2}) (?<ampm>[ap]m)$"
TUPLE: team
id color ;
: tbl-team-name? ( name -- ? )
R/ (maroon|white|blue|teal|green|yellow|light blue|red|orange|black)$/i matches? ;
TUPLE: game-summary ot? home-score away-score ;
: normalize-team-name ( name -- name' )
>lower R/ \s*team$/i "" re-replace ;
TUPLE: team-schedule
start end home away summary ;
! turns something like "Aug 23rd Wed 8:20 pm" into a timestamp
: parse-daysmart-schedule-time ( string -- timestamp )
datetime-regex findall
first
[
[ now year>> swap "month" of month-abbreviation-index ]
[ "day" of string>number ] bi <date>
]
[
[ "hour" of string>number ]
[ "minute" of string>number ]
[ "ampm" of ] tri
] bi
[ 0 set-time dup hour>> ] dip
parse-word 1quotation call( ts h -- ts' ) ;
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" }
}
! TODO team schedules
! dup [ "event__date" html-class? ] find-between-all [ trim-text [ { [ name>> text = ] [ text>> empty? not ] } 1&& ] filter 2 swap remove-nth " " join ] map
: parse-team-schedule-response ( vector -- schedules )
[ "event__date" html-class? ] find-between-all
[ trim-text [ { [ name>> text = ] [ text>> empty? not ] } 1&& ] filter
2 swap remove-nth [ text>> ] map " " join ] map ;
CONSTANT: tbl-league-id 6551
: parse-schedule-response ( vector -- schedules )
[ "list-group-item" html-class? ] find-between-all
[
remove-blank-text trim-text
[ name>> text = ] filter
[ text>> ] map
[ first R/ \s*Game$/ "" re-replace ]
[ second ] [ fourth ] tri [ normalize-team-name ] bi@ team-schedule boa
] { } map-as
[ home>> tbl-team-name? ] filter ;
CONSTANT: daysmart-base-url URL" https://apps.daysmartrecreation.com/dash/jsonapi/api/v1/"
: <daysmart-url> ( action -- url )
daysmart-base-url swap "Action" set-query-param ;
: timestamp>filter-string ( timestamp -- str )
[ { YYYY-MM-DD " " hh:mm:ss } formatted ]
with-string-writer ;
: <daysmart-team-url> ( team-id -- url )
"team/index" <daysmart-url> swap "teamid" set-query-param ;
: relative-hh ( timestamp -- )
hour>> 12 mod write-00 ;
: <daysmart-schedule-url> ( -- url )
"Schedule/location" <daysmart-url> ;
: timestamp>relative-hh:mm ( timestamp -- str )
[ { relative-hh ":" mm } formatted ]
with-string-writer ;
: <daysmart-schedule-form-data> ( datetime -- assoc )
[ { MM "/" DD "/" YYYY } formatted ] with-string-writer dup
! 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
'{
{ "startDate" _ }
{ "endDate" _ }
{ "facilityId" "1" } ! apex
{ "eventType" "g" } ! game types
{ "run" "true" } ! no idea
{ "cache" f }
{ "sort" "start" }
{ "company" "dreamsports" }
{ "include" "resource.facility,homeTeam.league,visitingTeam.league,eventType" }
{ "filter[start__gte]" _ }
{ "filter[end__lte]" _ }
{ "filter[homeTeam.league_id]" _ }
} ;
: <daysmart-schedule-request> ( datetime -- vector )
<daysmart-schedule-form-data> <daysmart-schedule-url> <post-request>
http-request nip parse-html ;
! 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 ;
: <daysmart-request> ( url -- response )
<get-request> http-request nip parse-html ;
: <tbl-events-url> ( params -- url )
[ daysmart-base-url URL" events" derive-url ] dip set-query-params ;
: <daysmart-team-request> ( team-id -- response )
<daysmart-team-url> scrape-html nip ;
: tbl-game-events ( params -- seq )
<tbl-events-url> http-get nip utf8 decode json> "data" of ;
: tbl-games ( -- assoc )
now [ wednesday ] [ thursday ] bi 2array dup
[ <daysmart-schedule-request> parse-schedule-response ] map
zip ;
: 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>> [ "team " prepend ] keep ]
[ home>> [ "team " prepend ] keep ]
[ start>> ] 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 ;
! : 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 ;

View File

@ -3,14 +3,19 @@
<t:bind-each t:name="schedule">
<section class="game">
<h1 class="day-header"><t:label t:name="header"/></h1>
<t:bind-each t:name="schedules">
<t:bind-each t:name="games">
<div class="team-times">
<div class="teams">
<span class="@away"><t:label t:name="away"/></span>
<span class="at">@</span>
<span class="@home"><t:label t:name="home"/></span>
</div>
<div class="time"><t:label t:name="start"/></div>
<t:if t:value="show-times?">
<div class="time"><t:label t:name="start-time"/></div>
</t:if>
<t:if t:value="show-summary?">
<div class="time"><t:label t:name="summary"/></div>
</t:if>
</div>
</t:bind-each>
</section>

View File

@ -102,7 +102,7 @@ main {
border-right: .25em solid red; }
main table tr td.team.blue {
border-right: .25em solid blue; }
main table tr td.team.light-blue {
main table tr td.team.light td.team.blue {
border-right: .25em solid lightblue; }
main table tr td.team.orange {
border-right: .25em solid orange; }

View File

@ -1,4 +1,4 @@
USING: accessors namespaces kernel sequences arrays urls present farkup ;
USING: accessors namespaces kernel sequences arrays urls present farkup combinators classes ;
USING: math.parser math.text.english ;
USING: io.sockets.secure io.servers ;
USING: furnace.actions furnace.boilerplate furnace.utilities furnace.chloe-tags ;
@ -12,14 +12,16 @@ IN: triangle-beer-league
SYMBOLS: key-password key-file dh-file ;
! DTO
TUPLE: schedule header schedules ;
! DTOs
TUPLE: game start-time home away summary show-summary? show-times? ;
TUPLE: schedule header games ;
TUPLE: tbl < dispatcher ;
: <splash-action> ( -- action )
<page-action>
{ tbl "tbl" } >>template ;
: team-schedule>time-string ( schedule -- str )
[ start>> ] [ end>> ] bi
[ timestamp>relative-hh:mm "pm" append ] bi@
2array " - " join ;
: timestamp>day-header ( timestamp -- str )
[ day-of-week day-name ]
@ -27,14 +29,37 @@ TUPLE: tbl < dispatcher ;
[ day>> dup number>string swap ordinal-suffix append ] tri
3array " " join ;
: summary>string ( summary -- str )
[
[ away-score>> ] [ home-score>> ] bi 2array " - " join
]
[ "" ] if* ;
: tbl-game-schedules>tbl-games ( game-schedules -- games )
[
{
[ team-schedule>time-string ]
[ home>> color>> ]
[ away>> color>> ]
[ summary>> summary>string ]
} cleave dup empty? not dup not game boa
] map ;
: tbl-games>schedule ( game-schedules -- schedules )
[ [ first timestamp>day-header ] [ second ] bi schedule boa ] map ;
[
[ first timestamp>day-header ]
[ second tbl-game-schedules>tbl-games ] bi schedule boa
] map ;
: <splash-action> ( -- action )
<page-action>
{ tbl "tbl" } >>template ;
: <schedule-action> ( -- action )
<page-action>
[
t "show-header?" set-value
tbl-games tbl-games>schedule "schedule" set-value
tbl-game-schedules tbl-games>schedule "schedule" set-value
] >>init
{ tbl "schedule" } >>template ;