-
-
Save apainintheneck/de8b97052be4d827b13247b80225dc66 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env ruby | |
require "json" | |
require "net/http" | |
# Example: | |
# {"wordoftheday"=> | |
# {"date"=>"2024-12-03", | |
# "past"=>0, | |
# "future"=>0, | |
# "results"=> | |
# {"2024-12-03"=> | |
# {"direct_URL"=>"https://www.dictionary.com/e/word-of-the-day/askance-2024-12-03/", | |
# "word"=>"askance", | |
# "short_definition"=>"with suspicion, mistrust, or disapproval", | |
# "pos"=>"<span class=\"luna-pos\">adverb</span>", | |
# "pronunciation"=>"<span class=\"italic\">uh</span>-<span class=\"bold\">skans</span> ", | |
# "header_notes"=>nil, | |
# "footer_notes"=> | |
# "<ul>\r\n" + | |
# " \t<li><span style=\"font-weight: 400;\">First recorded in 1520–30.</span></li>\r\n" + | |
# " \t<li>Of obscure origin; possibly from the Middle English <i>ascaunces</i>, meaning \"as if to say,\" or the Old French *<i>as cans</i>, meaning \"along the corners.\"</li>\r\n" + | |
# "</ul>\r\n" + | |
# "<strong>EXAMPLES OF ASKANCE</strong>\r\n" + | |
# "<ul>\r\n" + | |
# " \t<li><span style=\"font-weight: 400;\">The crowd listened </span><i><span style=\"font-weight: 400;\">askance</span></i><span style=\"font-weight: 400;\"> to the salesman's pitch, skeptical of his promises.</span></li>\r\n" + | |
# " \t<li>The customers glanced <i>askance</i> at the shop's sudden sale prices, wondering if there was a catch.</li>\r\n" + | |
# "</ul>", | |
# "audio"=>"https://static.sfdict.com/audio/A07/A0737200.mp3", | |
# "definitions"=>[{"partofspeech"=>"<span class=\"luna-pos\">adverb</span>", "data"=>"with suspicion, mistrust, or disapproval"}], | |
# "examples"=>[], | |
# "imageUrl"=>"https://www.dictionary.com/e/wp-content/uploads/2024/10/WOTD_askance_sq.png", | |
# "accentColor"=>"#0063D7"}}}} | |
def fetch_word_of_the_day | |
body = Net::HTTP.get(URI("https://www.dictionary.com/e/wp-json/dictionary/v1/wotd")) | |
raw_data = JSON.parse(body) | |
end | |
def strip_html(text) | |
text.gsub(/<[a-z\/][^<>]*>/, "").strip | |
end | |
raw_data = fetch_word_of_the_day | |
date = raw_data.dig("wordoftheday", "date") | |
word_info = raw_data.dig("wordoftheday", "results", date) | |
word, | |
summary, | |
part_of_speech, | |
pronunciation, | |
header, | |
footer = %w[ | |
word | |
short_definition | |
pos | |
pronunciation | |
header_notes | |
footer_notes | |
].map do |key| | |
word_info[key]&.then { strip_html _1 } | |
end | |
puts <<-DEFINITION | |
[#{word} * #{pronunciation}] | |
#{part_of_speech} -- #{summary} | |
#{[header, footer].compact.join("\n\n")} | |
DEFINITION |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment