Created
February 24, 2012 20:03
-
-
Save EdwardBetts/1903384 to your computer and use it in GitHub Desktop.
Untruncate using Google Suggest API
This file contains 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
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Clever idea. Wish this trick worked crossdomain in browser!