Topic: https://brettterpstra.com/2012/09/26/markdown-linking-services-with-the-google-api/
hide preview

What's next? verify your email address for reply notifications!

Wes Campaigne 12y, 174d ago

Python doesn't require any non-standard libraries to do this:

import urllib
import urllib2
import json
import sys

inputphrase = sys.stdin.read()
terms = urllib.quote_plus(inputphrase.strip())
url = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&filter=1&rsz=small&q=" + terms
data = urllib2.urlopen(url).read()
output = "[" + inputphrase + "](" + json.loads(data)["responseData"]["results"][0]["unescapedUrl"] + ")"
print output
hide preview

What's next? verify your email address for reply notifications!

Lauri Ranta 12y, 175d ago

The url fields are percent-encoded twice, so %20 becomes %25%20. You should get the unescapedUrl instead.

I'm just using sed to "parse" the JSON in my urlalias script:

curl -s --get --data-urlencode q=example http://ajax.googleapis.com/ajax/services/search/web?v=1.0 | sed 's/"unescapedUrl":"\([^"]*\).*/\1/;s/.*GwebSearch",//'

remark link
hide preview

What's next? verify your email address for reply notifications!

Brett 12y, 175d ago

Good call on unescapedURL. I've updated the gists and Services.

hide preview

What's next? verify your email address for reply notifications!