Skip to content

Instantly share code, notes, and snippets.

@EdwardBetts
Created February 24, 2012 20:03
Show Gist options
  • Save EdwardBetts/1903384 to your computer and use it in GitHub Desktop.
Save EdwardBetts/1903384 to your computer and use it in GitHub Desktop.
Untruncate using Google Suggest API
from urllib import quote_plus
from lxml import etree
def untruncate(s):
q = s.strip(u' .\u2026')
url = 'http://google.com/complete/search?output=toolbar&q=' + quote_plus(q.encode('latin-1'))
root = etree.parse(url).getroot()
return q + root[0][0].attrib['data'][len(q):] if len(root) else s
def demo():
from itertools import cycle
sample = [ 'Micros', 'Paul Grah', 'not able to handle this exampl', 'Y Comb', 'Wikip', 'Hacker Ne',
'Hackers and Pa', 'Zen and the Art', 'Founders at W', 'A Deepness in th']
for s, ending in zip(sample, cycle(['', '...', u'\u2026'])):
s += ending
print '#', s, '->', untruncate(s)
if __name__ == '__main__':
demo()
# Micros -> Microsoft
# Paul Grah… -> Paul Graham
# not able to handle this exampl... -> not able to handle this exampl...
# Y Comb -> Y Combinator
# Wikip… -> Wikipedia
# Hacker Ne... -> Hacker News
# Hackers and Pa -> Hackers and Painters
# Zen and the Art… -> Zen and the Art of motorcycle maintenance
# Founders at W... -> Founders at Work
# A Deepness in th -> A Deepness in the sky
@buley
Copy link

buley commented Feb 24, 2012

Clever idea. Wish this trick worked crossdomain in browser!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment