| Bytes | Lang | Time | Link |
|---|---|---|---|
| nan | 240703T093743Z | Deepak K | |
| 071 | Vim 8 + unimpaired.vim | 170424T015037Z | Brian Mc |
| 068 | Zsh + coreutils + w3m | 170423T220647Z | Dennis |
| 111 | R | 170424T203855Z | Billywob |
| 115 | PowerShell | 170424T081037Z | Andrei O |
| 122 | Groovy | 170424T173848Z | Magic Oc |
| 103 | Python + requests | 170423T214152Z | ovs |
| 111 | C# | 170424T110334Z | FatalMer |
| 127 | C# | 170424T144102Z | TheLetha |
| 109 | JavaScript | 170424T001112Z | bren |
Google Autocomplete can be quite entertaining! Here are a few fun ways to use it:
Predictive Search Game:
Type the beginning of a phrase like "Why do dogs" and note the funniest or most surprising suggestions. Autocomplete Stories:
Begin with a phrase like "Once upon a time," choose an autocomplete suggestion, and keep going to create a quirky story. Autocomplete Challenges:
Compete with friends to see who can come up with the funniest or most bizarre search completions. Learning Trends:
Discover trending questions and topics by typing "How to" or "What is." Try these and enjoy the unexpected results!
Vim 8 + unimpaired.vim, 93 89 85 70 73 71 bytes
- -4 bytes thanks to tsh
- -2 bytes thanks to Ian Emnace
- -2 bytes thanks to FatalMerlin
- -1 byte thanks to tsh/ckjbgames
:s/ /+/g
D:e http://google.us/complete/search?client=gma&q="
d3f";D]yy
As a bonus, the last bytes look like they're winking at you ;D Since this contains non-printing characters, the explanation contains substitutions (and I've replaced the pre-querystring part of the url with [url], for brevity):
:s/ /+/g<CR>D:e [url]?client=gma&q=<C-R>"<CR>d3f";D]yy
:s/ /+/g<CR> " Replace spaces with +
D " Delete and yank result
:e " Type :e ('edit' command) and a space
[url]?client=gma&q= " Type the url, except the query
<C-R>" " Paste from the default register
<CR> " Execute the command, which opens the response
" in a new buffer
d3f" " Delete through the first three quotation marks
" This deletes everything before the suggestion
; " Repeat motion; this jumps to the next \"
D " Delete everything from there, leaving only the suggestion
]yy " unimpaired.vim C string unescape to handle escaped '
As far as running goes, it works fine if you save it to a file named script and run with vim -s script input.txt on macOS, at least. It doesn't work if you add -u NONE, but it works fine if my .vimrc is empty. I assume it is using something from the system .vimrc to make the URL stuff work. This means, however, that it doesn't work in V, so no TIO link.
Some more test cases:
'what' => 'whataburger'
'what ' => 'what time is it' (Seriously? People Google that?)
What I really need is a way to open a URL with spaces in it. Replacing them with + first is just too many bytes!
Zsh + coreutils + w3m, 88 71 68 bytes
echo `w3m "google.com/complete/search?client=gma&q=$*"|cut -d\" -f4`
Switching from Bash to Zsh saved 3 bytes.
Thanks to @FatalMerlin for the shorter URL, saving 17 bytes!
Sample run
$ zsh complete.sh how to
how to make slime
$ zsh complete.sh dont you
don't you forget about me
$ zsh complete.sh don\'t you
don't you worry child
R, 111 bytes
Long time since I last came here but giving it a shot:
jsonlite::fromJSON(readLines(url(paste0("http://google.com/complete/search?client=gma&q=",scan(,"")))))[[2]][1]
Using the package jsonlite to convert the string fetched from readLines() into a list object.
Subsequently extract the second element, e.g (gives a warning that we don't have to care about):
> jsonlite::fromJSON(readLines(url(paste0("http://google.com/complete/search?client=gma&q=",scan(,"")))))[[2]][1]
1: "how to"
2:
Read 1 item
[[1]]
[1] "how to make slime"
Warning message:
In readLines(url(paste0("http://google.com/complete/search?client=gma&q=", :
incomplete final line found on 'http://google.com/complete/search?client=gma&q=how to'
PowerShell, 133 115 bytes
([net.webclient]::new().downloadstring("http://google.com/complete/search?client=gma&q=$_")|convertfrom-json)[1][0]
Sample run
Windows CMD command line:
powershell.exe -c "'code golf l'|%{([net.webclient]::new().downloadstring(""""http://google.com/complete/search?client=gma&q=$_"""")|convertfrom-json)[1][0]}"
PowerShell console:
'code golf l'|%{([net.webclient]::new().downloadstring("http://google.com/complete/search?client=gma&q=$_")|convertfrom-json)[1][0]}
Groovy, 122 bytes
{Eval.me(new URL("http://suggestqueries.google.com/complete/search?client=chrome&q="+it).text.split(",\\{")[0]+"]")[1][0]}
Basically:
Get the text from the end-point.
Remove the part with the brackets at the end, this isn't valid syntax.
Parse the remaining bit as a groovy array.
Grab the second element of the result array.
Python + requests 121 117 103 bytes
from requests import*
lambda s:get("http://google.com/complete/search?client=gma&q="+s).json()[1][0][0]
C#, 192 112 111 Bytes
Saved 80 Bytes thanks to @TheLethalCoder. Thanks for reformatting my code, I didn't know it was allowed to just leave off the surrounding Class and Method Body :)
Saved another Byte by replacing gma by hp, as it doesn't matter for the parsing and there is just some gibberish before the response body.
I litterally brute-forced the API to find gma and hp.
s=>new System.Net.WebClient().DownloadString("http://google.com/complete/search?client=hp&q="+s).Split('"')[3];
C#, 127 bytes
s=>new System.Net.WebClient().DownloadString("http://suggestqueries.google.com/complete/search?client=gma&q="+s).Split('"')[3];
Complete and formatted version:
static void Main()
{
System.Func<string, string> f = s =>
new System.Net.WebClient()
.DownloadString("http://suggestqueries.google.com/complete/search?client=gma&q="+s)
.Split('"')[3];
System.Console.WriteLine(f("you"));
System.Console.ReadLine();
}
JavaScript, 109 Bytes
q=>fetch("//suggestqueries.google.com/complete/search?client=chrome&q="+q).then(r=>r.json()).then(r=>r[1][0])
Promise, you gotta love it, but man is it verbose! This answer uses fetch(), a promise-based fetch API present in modern browsers. Promises work by establishing handlers for async actions at the beginning, like callbacks, but better. The .then() takes a function which will be called with the result of the async action. .then(r=>r.json()) uses the .json() response method to convert the text array to a manipulable variable, the second .then() just pulls the first response.
Usage:
S = q=>fetch("//suggestqueries.google.com/complete/search?client=chrome&q="+q).then(r=>r.json()).then(r=>r[1][0])
S("node.js").then(console.log); // Prints the result to the debug console
