# If needed set the proxy address #import os #os.environ['http_proxy'] = 'http://[proxy_host]:[proxy_port]/' # Definations for eval() false = 0 true = 1 import urllib # To remove the comments in the response buffer import re cpp_pat = re.compile('(/\*.*?\*/)|(".*?")', re.S) def subfunc(match): if match.group(2): return match.group(2) else: return '' def getAddress(address): encaddress = urllib.urlencode({'b': address}) url = 'http://local.live.com/search.ashx?%s' % encaddress sock = urllib.urlopen(url) resp = cpp_pat.sub(subfunc, sock.read()) sock.close() lines = resp.split(';') for instruction in lines: if instruction.find('VE_Scratchpad.AddLocation') > -1: break locationstr = instruction.replace('VE_Scratchpad.AddLocation', '') (infostr, lat, lon, pad) = eval(locationstr) return (infostr, lat, lon) def getDirections(start, end): encaddress = urllib.urlencode({'start': start, 'end': end}) url = 'http://local.live.com/directions.ashx?%s' % encaddress sock = urllib.urlopen(url) resp = cpp_pat.sub(subfunc, sock.read()) sock.close() if resp.find('Disambiguate') > -1: ambiguousstr = resp.replace('VE_Directions.Disambiguate', '').replace('new VE_Location', '')[:-1] (end, [suggestion]) = eval(ambiguousstr) return (0, (suggestion[0], end)) directionstr = resp.replace('VE_Directions.Populate', '').replace('new VE_RouteInstruction', '').replace('new Msn.VE.LatLong', '').replace('new Msn.VE.LineRegion', '')[:-1] (p1info, p1coord, p2info, p2coord, dirsteps, points, levels, totaldistance, units, unknown1, totaltime, viewport, lineregions, levelzooms) = eval(directionstr) return (1, (p1info, p1coord, p2info, p2coord, dirsteps)) def locateMe(): url = 'http://local.live.com/WiFiIPService/locate.ashx' sock = urllib.urlopen(url) resp = cpp_pat.sub(subfunc, sock.read()) sock.close() lines = resp.split(';') for instruction in lines: if instruction.find('SetAutoLocateViewport') > -1: break locationstr = instruction.replace('SetAutoLocateViewport', '') if locationstr == '': raise SyntaxError('VE could not find your location') (lat, lon, zoomradius, unknown, message) = eval(locationstr) return (lat, lon, zoomradius) if __name__ == "__main__": result = getAddress('12278 scripps summit dr san diego ca') print result (status, result) = getDirections('1245 morning view dr escondido ca', '15016 avenida venusto san diego ca') while status == 0: prompt = 'Did you mean "' + result[0] + '" for ' + result[1] + '?' resp = raw_input(prompt) if resp == 'y': (status, result) = getDirections('1245 morning view dr escondido ca', result[0]) print result result = locateMe() print result