! Copyright (C) 2023 Doug Coleman. ! See https://factorcode.org/license.txt for BSD license. USING: accessors calendar combinators.short-circuit formatting http http.client io io.directories io.encodings.binary io.encodings.utf8 io.files kernel math.order multiline sequences ; IN: aoc2023 CONSTANT: aoc2023-session "session stuff" : download-aoc2023-input ( url path -- ) utf8 [ aoc2023-session "session" put-cookie [ write ] with-http-request drop ] with-file-writer ; : check-aoc2023-file ( path -- path ) dup file-exists? [ dup utf8 file-contents { [ "Please don't repeatedly request" head? ] [ [[ ]] head? ] } 1|| [ [ delete-file ] keep ] when ] when ; : ensure-aoc2023-input-file-ready ( n -- n ) dup [ 2023 12 ] dip 5 >>hour now-utc before=? [ "too early for aoc2023 day %d (opens at 5:00 UTC)" sprintf throw ] unless ; : ?download-aoc2023-input ( n -- string/* ) ensure-aoc2023-input-file-ready "resource:aoc2023/" make-directories [ "https://adventofcode.com/2023/day/%d/input" sprintf ] [ "resource:aoc2023/input%s.txt" sprintf check-aoc2023-file ] bi [ dup file-exists? [ 2drop ] [ download-aoc2023-input ] if ] keep [ utf8 file-contents ] [ "still too early" throw ] if* ;