USING: urls pcre sequences accessors kernel assocs parser quotations combinators.short-circuit regexp arrays unicode ; FROM: regexp => matches? ; USING: xml.syntax xml.writer ; USING: io io.streams.string ; USING: math.parser math.text.english ; USING: calendar calendar.english calendar.format ; 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 "^(?\\w{3}) (?\\d{0,2}).{7}(?\\d{0,2}):(?\\d{0,2}) (?[ap]m)$" : tbl-team-name? ( name -- ? ) R/ (maroon|white|blue|teal|green|yellow|light blue|red|orange|black)$/i matches? ; : normalize-team-name ( name -- name' ) >lower R/ \s*team$/i "" re-replace ; ! 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 ] [ [ "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' ) ; ! 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 ; : 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 ; : ( action -- url ) daysmart-base-url swap "Action" set-query-param ; : ( team-id -- url ) "team/index" swap "teamid" set-query-param ; : ( -- url ) "Schedule/location" ; : ( datetime -- assoc ) [ { MM "/" DD "/" YYYY } formatted ] with-string-writer dup '{ { "startDate" _ } { "endDate" _ } { "facilityId" "1" } ! apex { "eventType" "g" } ! game types { "run" "true" } ! no idea } ; : ( datetime -- vector ) http-request nip parse-html ; : ( url -- response ) http-request nip parse-html ; : ( team-id -- response ) scrape-html nip ; : tbl-games ( -- assoc ) now [ wednesday ] [ thursday ] bi 2array dup [ parse-schedule-response ] map zip ; : render-day-header ( timestamp -- xml ) [ day-of-week day-name ] [ month>> month-name ] [ day>> dup number>string swap ordinal-suffix append ] tri [XML

<-> <-> <->

XML] ; : render-team-times ( schedule -- xml ) [ away>> [ "team " prepend ] keep ] [ home>> [ "team " prepend ] keep ] [ start>> ] tri [XML
@
><-> ><->
<->
XML] ; : render-no-games ( -- xml ) [XML
No Scheduled TBL games
XML] ; : render-day-schedule ( assoc -- xml ) [ first render-day-header ] [ second dup empty? [ drop render-no-games ] [ [ render-team-times ] map ] if ] bi [XML
<-> <->
XML] ; : tbl-games? ( -- ? ) tbl-games empty? not ; : render-schedule ( -- ) tbl-games [ render-day-schedule pprint-xml ] each ;