Created
July 21, 2011 10:55
-
-
Save msimpson/1096958 to your computer and use it in GitHub Desktop.
Google definition (this method is frowned upon by Google, use for educational purposes only).
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 | |
# Define: a Google Dictionary API tool | |
require "rubygems" | |
require "net/http" | |
require "json" | |
if not ARGV[0]; exit; end | |
query = ARGV.join("+").gsub(" ","+") | |
response = Net::HTTP.start("www.google.com",80) do |http| | |
http.get("/dictionary/json?callback=r&sl=en&tl=en&q="+query); end | |
begin | |
data = JSON.parse(/r\((:?.*?),200,null\)/.match(response.body)[1]) | |
phonetic = data["primaries"][0]["terms"][1]["text"] | |
label = data["primaries"][0]["terms"][0]["labels"] | |
type = (label)?label[0]["text"]:"person" | |
puts "\n%s %s (%s)\n" % [data["query"].capitalize, phonetic, type.downcase] | |
for i in (0..5) | |
entry = data["primaries"][0]["entries"][i]["terms"][0]["text"]. | |
gsub(/(x3cemx3e|x3c\/emx3e)/,"").scan(/.{1,80}(?:\s|\Z)/) | |
puts "\u2022 "+entry[0].capitalize | |
entry[1..-1].each do |line|; puts " "+line; end | |
end | |
puts | |
rescue | |
puts | |
exit | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment