From faff51b9d33d242c4583b842c3582e5d75d8811d Mon Sep 17 00:00:00 2001 From: Andrew Tomaka Date: Tue, 10 Dec 2024 19:45:42 -0500 Subject: [PATCH] Add problem downloader --- .gitignore | 1 + Gemfile | 2 ++ Gemfile.lock | 24 +++++++++++++++++ download-problems | 65 +++++++++++++++++++++++++++++++++++++++++++++++ template.rb | 2 +- tmp/.gitinclude | 0 6 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 .gitignore create mode 100755 download-problems create mode 100644 tmp/.gitinclude diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3fec32c --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +tmp/ diff --git a/Gemfile b/Gemfile index 4a05f85..eb7e645 100644 --- a/Gemfile +++ b/Gemfile @@ -1,3 +1,5 @@ source "https://rubygems.org" +gem "nokogiri" gem "rspec" +gem "watir" diff --git a/Gemfile.lock b/Gemfile.lock index 60e3573..7ba749e 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,18 @@ GEM remote: https://rubygems.org/ specs: + base64 (0.2.0) 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-core (~> 3.13.0) rspec-expectations (~> 3.13.0) @@ -15,13 +26,26 @@ GEM diff-lcs (>= 1.2.0, < 2.0) rspec-support (~> 3.13.0) 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 arm64-darwin-24 ruby DEPENDENCIES + nokogiri rspec + watir BUNDLED WITH 2.5.23 diff --git a/download-problems b/download-problems new file mode 100755 index 0000000..8fbd82d --- /dev/null +++ b/download-problems @@ -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 diff --git a/template.rb b/template.rb index 06c4646..901906f 100755 --- a/template.rb +++ b/template.rb @@ -1,7 +1,7 @@ #!/usr/bin/env ruby problem = :fake -#input = File.readlines("#{problem}.input", chomp: true) +#input = File.readlines("input", chomp: true) input = DATA.read result = :solution diff --git a/tmp/.gitinclude b/tmp/.gitinclude new file mode 100644 index 0000000..e69de29