Created
March 10, 2021 23:32
-
-
Save johnholdun/03e8df43bd595f6cc942d11df14abc1a to your computer and use it in GitHub Desktop.
Track Zach Gage's Scarcity. Run this every day or you risk losing it forever.
This file contains hidden or 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 'date' | |
require 'digest' | |
require 'net/http' | |
ledger = (DATA || '').each_line.map { |l| l.strip.split(' ') }.to_h | |
def url_for(token) | |
"http://stfj.net/scarcity/#{token}" | |
end | |
def calculate_token(ledger, last_modified = nil) | |
keys = ledger.keys.sort.map { |k| ledger[k][0, 1] } | |
last_modified ||= Date.parse(ledger.keys.max) | |
Digest::MD5.hexdigest(keys.join('') + last_modified.strftime('%m%d%Y')) | |
end | |
def valid_token?(token) | |
response = Net::HTTP.get_response(URI(url_for(token))) | |
response.is_a?(Net::HTTPSuccess) && | |
response.body =~ /<title>\s*scarcity\s*<\/title>/i | |
end | |
if valid_token?(calculate_token(ledger)) | |
STDERR.puts('The work has not moved') | |
exit | |
end | |
last_modified = Date.new(2021, 3, 8) # Date.today | |
token = calculate_token(ledger, last_modified) | |
if valid_token?(token) | |
file = File.open(__FILE__, 'a') | |
file.puts("#{last_modified} #{token}") | |
STDERR.puts('The work has moved') | |
exit | |
else | |
STDERR.puts('Could not calculate the new location') | |
exit 1 | |
end | |
__END__ | |
2021-03-08 cd8ab6ab905103b5e3c2b68da30732e4 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment