Add problem downloader
This commit is contained in:
parent
01ad55fcbd
commit
faff51b9d3
6 changed files with 93 additions and 1 deletions
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
tmp/
|
2
Gemfile
2
Gemfile
|
@ -1,3 +1,5 @@
|
||||||
source "https://rubygems.org"
|
source "https://rubygems.org"
|
||||||
|
|
||||||
|
gem "nokogiri"
|
||||||
gem "rspec"
|
gem "rspec"
|
||||||
|
gem "watir"
|
||||||
|
|
24
Gemfile.lock
24
Gemfile.lock
|
@ -1,7 +1,18 @@
|
||||||
GEM
|
GEM
|
||||||
remote: https://rubygems.org/
|
remote: https://rubygems.org/
|
||||||
specs:
|
specs:
|
||||||
|
base64 (0.2.0)
|
||||||
diff-lcs (1.5.1)
|
diff-lcs (1.5.1)
|
||||||
|
logger (1.6.2)
|
||||||
|
mini_portile2 (2.8.8)
|
||||||
|
nokogiri (1.17.1)
|
||||||
|
mini_portile2 (~> 2.8.2)
|
||||||
|
racc (~> 1.4)
|
||||||
|
nokogiri (1.17.1-arm64-darwin)
|
||||||
|
racc (~> 1.4)
|
||||||
|
racc (1.8.1)
|
||||||
|
regexp_parser (2.9.3)
|
||||||
|
rexml (3.3.9)
|
||||||
rspec (3.13.0)
|
rspec (3.13.0)
|
||||||
rspec-core (~> 3.13.0)
|
rspec-core (~> 3.13.0)
|
||||||
rspec-expectations (~> 3.13.0)
|
rspec-expectations (~> 3.13.0)
|
||||||
|
@ -15,13 +26,26 @@ GEM
|
||||||
diff-lcs (>= 1.2.0, < 2.0)
|
diff-lcs (>= 1.2.0, < 2.0)
|
||||||
rspec-support (~> 3.13.0)
|
rspec-support (~> 3.13.0)
|
||||||
rspec-support (3.13.2)
|
rspec-support (3.13.2)
|
||||||
|
rubyzip (2.3.2)
|
||||||
|
selenium-webdriver (4.27.0)
|
||||||
|
base64 (~> 0.2)
|
||||||
|
logger (~> 1.4)
|
||||||
|
rexml (~> 3.2, >= 3.2.5)
|
||||||
|
rubyzip (>= 1.2.2, < 3.0)
|
||||||
|
websocket (~> 1.0)
|
||||||
|
watir (7.3.0)
|
||||||
|
regexp_parser (>= 1.2, < 3)
|
||||||
|
selenium-webdriver (~> 4.2)
|
||||||
|
websocket (1.2.11)
|
||||||
|
|
||||||
PLATFORMS
|
PLATFORMS
|
||||||
arm64-darwin-24
|
arm64-darwin-24
|
||||||
ruby
|
ruby
|
||||||
|
|
||||||
DEPENDENCIES
|
DEPENDENCIES
|
||||||
|
nokogiri
|
||||||
rspec
|
rspec
|
||||||
|
watir
|
||||||
|
|
||||||
BUNDLED WITH
|
BUNDLED WITH
|
||||||
2.5.23
|
2.5.23
|
||||||
|
|
65
download-problems
Executable file
65
download-problems
Executable file
|
@ -0,0 +1,65 @@
|
||||||
|
#!/usr/bin/env ruby
|
||||||
|
|
||||||
|
require "fileutils"
|
||||||
|
require "html2markdown"
|
||||||
|
require "nokogiri"
|
||||||
|
require "watir"
|
||||||
|
require "debug"
|
||||||
|
|
||||||
|
browser = Watir::Browser.new(:chrome, headless: true)
|
||||||
|
|
||||||
|
def login(browser)
|
||||||
|
browser.goto("https://adventofcode.com/auth/github")
|
||||||
|
|
||||||
|
browser.text_field(name: "login").set(ENV.fetch("GITHUB_USERNAME"))
|
||||||
|
browser.text_field(name: "password").set(ENV.fetch("GITHUB_PASSWORD"))
|
||||||
|
browser.button(name: "commit").click
|
||||||
|
|
||||||
|
code = browser.text.scan(/Enter the digits shown below to verify your identity.\n(\d+)/)
|
||||||
|
|
||||||
|
if browser.url.include?("two-factor")
|
||||||
|
puts "Complete 2FA on phone. Code: #{code}"
|
||||||
|
|
||||||
|
Watir::Wait.until(timeout: 120) do
|
||||||
|
!browser.url.include?("two-factor")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
if browser.button(name: "authorize").exists?
|
||||||
|
browser.button(name: "authorize").click
|
||||||
|
end
|
||||||
|
|
||||||
|
final_url = browser.url
|
||||||
|
browser.goto(final_url)
|
||||||
|
|
||||||
|
session_cookies = browser.cookies.to_a
|
||||||
|
File.write("tmp/cookies.json", session_cookies.to_json)
|
||||||
|
|
||||||
|
browser.close
|
||||||
|
end
|
||||||
|
|
||||||
|
local = Dir
|
||||||
|
.entries(".")
|
||||||
|
.select { |entry| File.directory?(entry) && !entry.match?(/^\./) }
|
||||||
|
|
||||||
|
browser.goto("https://adventofcode.com/")
|
||||||
|
cookies = JSON.parse(File.read("tmp/cookies.json"))
|
||||||
|
cookies.each { |cookie| browser.cookies.add(cookie["name"], cookie["value"], domain: cookie["domain"]) }
|
||||||
|
pre_element = browser.element(tag_name: "pre", class: "calendar")
|
||||||
|
doc = Nokogiri::HTML(pre_element.html)
|
||||||
|
links = doc.css("a").map { |link| link["href"] }
|
||||||
|
|
||||||
|
available = links.map { _1.scan(/day\/(\d+)/) }.flatten.map { "%02d" % _1 }
|
||||||
|
|
||||||
|
missing = available - local
|
||||||
|
|
||||||
|
if missing.any?
|
||||||
|
missing.each do |problem|
|
||||||
|
Dir.mkdir(problem)
|
||||||
|
browser.goto("https://adventofcode.com/2024/day/#{problem}/input")
|
||||||
|
File.write("#{problem}/input", browser.text)
|
||||||
|
FileUtils.copy("./template.rb", "./#{problem}/main.rb")
|
||||||
|
end
|
||||||
|
else
|
||||||
|
puts "All problems are currently local"
|
||||||
|
end
|
|
@ -1,7 +1,7 @@
|
||||||
#!/usr/bin/env ruby
|
#!/usr/bin/env ruby
|
||||||
|
|
||||||
problem = :fake
|
problem = :fake
|
||||||
#input = File.readlines("#{problem}.input", chomp: true)
|
#input = File.readlines("input", chomp: true)
|
||||||
input = DATA.read
|
input = DATA.read
|
||||||
|
|
||||||
result = :solution
|
result = :solution
|
||||||
|
|
0
tmp/.gitinclude
Normal file
0
tmp/.gitinclude
Normal file
Loading…
Reference in a new issue