2020-12-02 21:33:18 -06:00
|
|
|
import httpclient, strformat, os, logging, streams
|
|
|
|
|
|
|
|
var fileLog = newFileLogger("aoc2020.log")
|
|
|
|
var consoleLog = newConsoleLogger()
|
|
|
|
addHandler(consoleLog)
|
|
|
|
addHandler(fileLog)
|
|
|
|
|
2020-12-03 22:49:54 -06:00
|
|
|
let cacheDir = joinPath(getEnv("XDG_CACHE_HOME", expandTilde("~/.cache")), "/aoc2020")
|
2020-12-02 21:33:18 -06:00
|
|
|
createDir(cacheDir)
|
|
|
|
|
|
|
|
# TODO: add login capabilities via `pass` for auto-cookie-retrieval?
|
|
|
|
|
|
|
|
proc requestAocContentAuthed(url: string): TaintedString =
|
|
|
|
let cookie = getEnv("ADVENT_OF_CODE_AUTH_COOKIE", readFile(expandTilde("~/.advent-of-code-auth-cookie")))
|
|
|
|
let client = newHttpClient()
|
|
|
|
client.headers = newHttpHeaders({"cookie": cookie})
|
2020-12-04 01:14:56 -06:00
|
|
|
client.getContent(url)
|
2020-12-02 21:33:18 -06:00
|
|
|
|
|
|
|
proc getInputFileStreamForDay*(day: int): FileStream =
|
|
|
|
# retrieve the input and dump it to a file if we don't have it yet
|
|
|
|
let cacheFile = joinPath(cacheDir, fmt"/day-{day}.aoc-input")
|
|
|
|
if not fileExists(cacheFile):
|
|
|
|
let url = fmt"https://adventofcode.com/2020/day/{day}/input"
|
|
|
|
writeFile(cacheFile, requestAocContentAuthed(url).string)
|
|
|
|
openFileStream(cacheFile)
|
|
|
|
|
|
|
|
proc getInputForDay*(day: int): string =
|
|
|
|
getInputFileStreamForDay(day).readAll()
|