Add code to fetch input from AoC.

This commit is contained in:
Danila Fedorin 2020-12-05 15:56:11 -08:00
parent a8363ae168
commit 48ba4e1577

15
src/advent/input.cr Normal file
View File

@ -0,0 +1,15 @@
require "http/client"
require "uri"
def input(year, day)
filename = "year#{year}day#{day}.txt.cache"
return File.read(filename) if File.exists? filename
HTTP::Client.new(URI.parse("https://adventofcode.com")) do |client|
client.before_request do |request|
request.cookies["session"] = ENV["AOC_SESSION"]
end
input = client.get("/#{year}/day/#{day}/input").body.to_s
File.write(filename, input)
return input
end
end