Skip to content

Instantly share code, notes, and snippets.

@kejadlen
Created January 6, 2020 21:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kejadlen/bbd8e87f68139be5de1dd0eed573a6a7 to your computer and use it in GitHub Desktop.
Save kejadlen/bbd8e87f68139be5de1dd0eed573a6a7 to your computer and use it in GitHub Desktop.
require "date"
require "open-uri"
class Store
def initialize(base_dir)
@base_dir = base_dir
end
# Super inefficient, but whatever...
def []=(date, day_data)
path = date.strftime("#@base_dir/%Y-%m.json")
month_data = File.exist?(path) ? JSON.parse(File.read(path)) : {}
month_data[date.to_s] = day_data
File.write(path, JSON.dump(month_data))
end
def last_date
path = Dir["#@base_dir/*.json"].sort.last
return nil if path.nil?
JSON.parse(File.read(path)).keys.map {|k| Date.parse(k) }.sort.last
end
end
class NYT
API = "https://nyt-games-prd.appspot.com/svc/crosswords"
def initialize(nyt_s)
@nyt_s = nyt_s
@puzzles = {}
end
def fetch(date)
id = puzzle_id(date)
url = "#{API}/v6/game/#{id}.json"
JSON.parse(URI.open(url, "nyt-s" => @nyt_s).read)
end
private
def puzzle_id(date)
return @puzzles.fetch(date).fetch("puzzle_id") if @puzzles.has_key?(date)
last_date = @puzzles.keys.sort.last || date - 1
date_start = last_date + 1
date_end = date_start >> 3 # 3 months
url = "#{API}/v3/55348624/puzzles.json?date_start=#{date_start}&date_end=#{date_end}"
json = JSON.parse(URI.open(url, "nyt-s" => @nyt_s).read)
results = json.fetch("results")
@puzzles.merge!(results.map {|result|
[Date.parse(result.fetch("print_date")), result]
}.to_h)
@puzzles.fetch(date).fetch("puzzle_id")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment