Paste: aoc2023 input file downloader

Author: erg
Mode: factor
Date: Fri, 1 Dec 2023 19:49:17
Plain Text |
! Copyright (C) 2023 Doug Coleman.
! See https://factorcode.org/license.txt for BSD license.
USING: accessors calendar formatting http http.client io
io.directories io.encodings.binary io.encodings.utf8 io.files
kernel math.order sequences ;
IN: aoc2023

CONSTANT: aoc2023-session "paste from browser just the numbers part"

: download-aoc2023-input ( url path -- )
    utf8 [
        <get-request> aoc2023-session "session" <cookie> put-cookie
        [ write ] with-http-request drop
    ] with-file-writer ;

: check-aoc2023-file ( path -- path/f )
    dup utf8 file-contents
    "Please don't repeatedly request" head? [
        delete-file f
    ] when ;

: ensure-aoc2023-input-file-ready ( n -- n )
    dup [ 2023 12 ] dip <date-utc> 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 ] bi
    [
        dup file-exists? [ 2drop ] [ download-aoc2023-input ] if
    ] keep
    check-aoc2023-file [ utf8 file-contents ] [ "still too early" throw ] if* ;

Annotation: how to use

Author: erg
Mode: factor
Date: Fri, 1 Dec 2023 19:54:54
Plain Text |
1 ?download-aoc2023-input ! puts string on stack

Annotation: fix bugs (again)

Author: erg
Mode: factor
Date: Sun, 3 Dec 2023 00:23:42
Plain Text |
! 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 [
        <get-request> aoc2023-session "session" <cookie> 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? ]
            [ [[ <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">]] head? ]
        } 1|| [
            [ delete-file ] keep
        ] when 
    ] when ;

: ensure-aoc2023-input-file-ready ( n -- n )
    dup [ 2023 12 ] dip <date-utc> 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* ;

New Annotation

Summary:
Author:
Mode:
Body: