Skip to content

Instantly share code, notes, and snippets.

@notjosh
Created November 13, 2016 16:14
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 notjosh/44f036f96e2573dd6855863d81839320 to your computer and use it in GitHub Desktop.
Save notjosh/44f036f96e2573dd6855863d81839320 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# sigh, nokogiri :(
require 'nokogiri'
require 'open-uri'
require 'CGI'
DICTIONARY_HOME = 'https://dictionary.cambridge.org/dictionary/english/'.freeze
def fetch(url)
open(url)
end
def wotd(doc)
doc.at_css('.wotd-hw').text
rescue
return nil
end
def link(doc)
doc.at_css('a.a--rev.a--b')['href']
rescue
return nil
end
def audio(doc, region, format = 'ogg')
doc.at_css(".pron-info .audio_play_button.#{region}")["data-src-#{format}"]
rescue
return nil
end
def gtranslate(word)
word = CGI.escape(word)
"https://translate.google.com/#en/de/#{word}"
end
html = fetch(DICTIONARY_HOME)
doc = Nokogiri::HTML(html)
wotd = wotd(doc)
link = link(doc)
unless wotd
puts "Can't find word in HTML. Something changed? :("
exit(1)
end
word_html = fetch(link) if link
word_doc = Nokogiri::HTML(word_html)
uk = audio(word_doc, 'uk')
us = audio(word_doc, 'us')
gtranslate = gtranslate(wotd)
puts "WotD: #{wotd}"
puts "Link: #{link}" if link
puts "UK: #{uk}" if uk
puts "US: #{us}" if us
puts "Google Translate: #{gtranslate}" if gtranslate
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment