Menu

[r189]: / trunk / libs / google / maps.py  Maximize  Restore  History

Download this file

111 lines (105 with data), 4.5 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
# -*- coding: utf-8 -*-
import re, sys, urllib, locale
reload(sys)
sys.setdefaultencoding('utf-8')
locale.setlocale(locale.LC_ALL, '')
system_language = locale.getlocale()[0].split("_")[1]
api_key = ""
def distance(a, b):
try:
data = {"q" : "from: %s to: %s" % (a, b), "output" : "json", "oe" : "utf8", "sensor" : "false", "key" : api_key, "hl" : system_language.lower(), "gl" : system_language.upper()}
data = urllib.urlencode(data)
req = "http://maps.google.com/maps/nav?" + data
info = urllib.urlopen(req).read().decode()
info = eval(info)
if not info["Status"]["code"] == 200:
return None
a = info["Placemark"][0]["address"]
b = info["Placemark"][1]["address"]
seconds = info["Directions"]["Duration"]["seconds"]
meters = info["Directions"]["Distance"]["meters"]
return {"a" : a, "b" : b, "time" : getInHMS(seconds), "meters" : meters}
except:
return None
def coordinates(address):
try:
data = {"q" : address, "output" : "json", "oe" : "utf8", "sensor" : "false", "key" : api_key}
data = urllib.urlencode(data)
req = "http://maps.google.com/maps/geo?" + data
info = urllib.urlopen(req).read().decode()
info = eval(info)
if not info["Status"]["code"] == 200:
return None
coordinates = info["Placemark"][0]["Point"]["coordinates"]
coordinates.pop(2)
return coordinates
except:
pass
def address(coordinates):
try:
if type(coordinates) != list or len(coordinates) != 2:
return None
xml = urllib.urlopen("http://maps.google.com/cbk?output=xml&ll=%s,%s" % (coordinates[1], coordinates[0])).read().decode()
if len(xml) < 60:
return None
xml = xml.encode("utf-8")
lat = re.findall('lat="(-?\d+\.\d+)"', xml)[0]
lng = re.findall('lng="(-?\d+\.\d+)"', xml)[0]
text = re.findall('<text>(.*?)</text>', xml)[0]
region = re.findall('<region>(.*?)</region>', xml)[0]
country = re.findall('<country>(.*?)</country>', xml)[0]
return {"latitude" : lat, "longitude" : lng, "text" : text, "region" : region, "country" : country}
except:
return None
def street_view(location, x=1, y=1):
try:
coords = coordinates(location)
xml = urllib.urlopen("http://maps.google.com/cbk?output=xml&ll=%s,%s" % (coords[1], coords[0])).read().decode()
xml = xml.encode("utf-8")
if len(xml) < 60:
return None
zoom = re.findall('num_zoom_levels="(\d+)"', xml)[0]
pano_ids = re.findall('pano_id="(.*?)"', xml)
urls = []
for panoid in pano_ids:
urls.append("http://cbk1.google.com/cbk?output=tile&zoom=%s&x=%s&y=%s&panoid=%s" % (zoom, x, y, panoid))
return urls
except:
return None
def route(a, b):
try:
data = {"q" : "from: %s to: %s" % (a, b), "output" : "json", "oe" : "utf8", "sensor" : "false", "key" : api_key, "hl" : system_language.lower(), "gl" : system_language.upper()}
data = urllib.urlencode(data)
req = "http://maps.google.com/maps/nav?" + data
info = urllib.urlopen(req).read().decode()
res = info.encode("utf-8")
if '"Status":{"code":602' in res:
return None
replace_chars = {'\\u003C' : '<', '\\u003E' : '>', '\\u0026' : '&'}
for key in replace_chars.keys():
res = res.replace(key, replace_chars[key])
res = res.replace('\\', '').replace('<wbr/>', '')
res = res.replace('&nbsp;', ' ')
res = re.sub('&#(\d+);', lambda x: chr(int(x.groups()[0])), res)
res = re.sub('<div class="google_note".*?</div>', '', res)
res = re.sub('<.*?>', '', res)
res = eval(res)
(total_meters, total_seconds, summary, steps) = (res['Directions']['Distance']['meters'], res['Directions']['Duration']['seconds'], res['Directions']['summaryHtml'], res['Directions']['Routes'][0]['Steps'])
nice_steps = []
counter = 1
for step in steps:
coords = tuple(map(lambda x: str(x)[:str(x).index('.')+5], step['Point']['coordinates'][:2]))
distance = str(step['Distance']['meters'])
nice_steps.append({'duration' : step['Duration']['seconds'], 'distance' : distance, 'description' : step['descriptionHtml'], 'coordinates' : coords, 'stepnum' : counter})
counter += 1
return {'meters' : total_meters, 'seconds' : total_seconds, 'summary' : summary, 'steps' : nice_steps}
except:
return None
def getInHMS(seconds):
hours = seconds / 3600
seconds -= 3600*hours
minutes = seconds / 60
seconds -= 60*minutes
if hours == 0:
return "%02d:%02d" % (minutes, seconds)
return "%02d:%02d:%02d" % (hours, minutes, seconds)