Created
September 15, 2017 15:30
-
-
Save davidrichards/c99ad0d5a024011718e0360fc0773156 to your computer and use it in GitHub Desktop.
This API was supposed to be turned off two years ago, so no promises it will work for you.
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 'yaml' | |
class AutoSuggest | |
def self.call(*args) | |
new(*args).call | |
end | |
attr_reader :term | |
def initialize(term) | |
@term = term | |
end | |
def cmd | |
@cmd ||= <<-CMD | |
curl -G --data-urlencode "q=#{term}" --data-urlencode "client=chrome" http://suggestqueries.google.com/complete/search | |
CMD | |
end | |
def result | |
@result ||= `#{cmd}` | |
end | |
def list | |
@list ||= JSON.parse(result) rescue [] | |
end | |
def returned_term | |
@returned_term ||= list[0] | |
end | |
def suggestions | |
@suggestions ||= list[1] | |
@suggestions ||= [] | |
end | |
def meta | |
@meta ||= list[4] | |
@meta ||= {} | |
end | |
def relevance | |
@relevance ||= meta.fetch("google:suggestrelevance", []) | |
end | |
def types | |
@types ||= meta.fetch("google:suggesttype", []) | |
end | |
def object | |
@object ||= { | |
"term" => returned_term, | |
"suggestions" => suggestions, | |
"relevance" => suggestions.zip(relevance), | |
"types" => suggestions.zip(types) | |
} | |
end | |
def yaml | |
@yaml ||= YAML.dump(object) | |
end | |
def call | |
yaml | |
end | |
end | |
if __FILE__ == $0 | |
puts AutoSuggest.call(ARGV[0]) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment