Last active
March 9, 2025 19:39
-
-
Save fancyremarker/f6cabaa2f664f9b74c81c1b20f63868b to your computer and use it in GitHub Desktop.
Print NYT Wordle stats: date, hard mode?, # guesses
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'net/http' | |
require 'time' | |
require 'json' | |
require 'uri' | |
require 'pry' | |
TOKEN = File.read('nyt-s.token').rstrip | |
API_BASE = 'https://www.nytimes.com/svc/games' | |
DELAY = 0.2 | |
SECONDS_IN_DAY = 24 * 60 * 60 | |
START_DATE = Time.parse('2025-02-24') | |
END_DATE = Time.parse('2025-03-09') | |
def format_date(date) | |
date.strftime('%Y-%m-%d') | |
end | |
def each_puzzle(start_date, end_date, &block) | |
uri1 = URI.parse("#{API_BASE}/v1/archive/wordle/#{format_date(start_date)}/#{format_date(end_date)}") | |
http = Net::HTTP.new(uri1.host, uri1.port) | |
http.use_ssl = true | |
request1 = Net::HTTP::Get.new(uri1.request_uri) | |
request1['nyt-s'] = TOKEN | |
id_map = Hash[JSON.parse(http.request(request1).body).map { |el| [el['id'], el['print_date']] }] | |
# result_count = id_map.keys.count | |
# expected_count = ((end_date - start_date) / SECONDS_IN_DAY).floor + 1 | |
# fail "Expected #{expected_count}, got #{result_count}" unless expected_count == result_count | |
uri2 = URI.parse("#{API_BASE}/state/wordleV2/latests") | |
args = { | |
puzzle_ids: id_map.keys.join(',') | |
} | |
uri2.query = URI.encode_www_form(args) | |
request2 = Net::HTTP::Get.new(uri2.request_uri) | |
request2['nyt-s'] = TOKEN | |
JSON.parse(http.request(request2).body)['states'].sort_by { |el| el['print_date'] }.each do |r| | |
next if Time.parse(r['print_date']) < start_date | |
next if Time.parse(r['print_date']) > end_date | |
print_date = r['print_date'] || '' | |
print_date = id_map[r['puzzle_id'].to_i] if print_date.empty? | |
yield [print_date, r] | |
end | |
end | |
d = START_DATE | |
while d <= END_DATE | |
# Maximum number of days allowed in request = 31 | |
each_puzzle(d, d + 30 * SECONDS_IN_DAY) do |print_date, r| | |
game_data = r['game_data'] | |
next unless game_data['status'] == 'WIN' | |
puts [print_date, r['puzzle_id'], game_data['hardMode'], game_data['currentRowIndex']].join("\t") | |
end | |
d += (31 * SECONDS_IN_DAY) | |
sleep DELAY | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment