#!/usr/bin/python2.4 import socket, thread, time, httplib import Image from StringIO import StringIO handlers = 0 def getSatUrl(x, y, z): quadrants = "qrts" result = "" n = 16 - z while n >= 0: xbit = (x >> n) & 1; ybit = (y >> n) & 1; result += quadrants[xbit+ 2*ybit] n -= 1 return "kh.google.com/kh?n=404&v=13&t=t"+result def httpget(url): server, file = url.split("/", 1) print "getting", server+"/"+file conn = httplib.HTTPConnection(server) conn.request("GET", "/"+file) resp = conn.getresponse() return resp def handler( channel, details ): try: global handlers send = channel.sendall req = channel.recv ( 1024 ) get = req.split("HTTP")[0].split("/")[1] if not get.startswith("map"): print "No map" send("HTTP/1.0 200 OK\n") send("Content-Type: text/html\n\n") send("No map") return else: map, x, y, z = get.split("&") file = "mt?n=404&v=w2t.38&x="+x+"&y="+y+"&zoom="+z overlay = httpget("mt.google.com/"+ file) satellite = httpget(getSatUrl(int(x), int(y), int(z))) print "got both" fakefile = StringIO() try: sa = Image.open(StringIO(satellite.read())).convert("RGBA") print x, y, z try: ov = Image.open(StringIO(overlay.read())) composite = Image.composite(ov, sa, ov) composite.save(fakefile, "JPEG", optimize=1 ) except: sa.save(fakefile, "JPEG", optimize=1) except: #could not get satellite image, sending only map to Mapper file = "mt?n=404&v=w2.37&x="+x+"&y="+y+"&zoom="+z print "Failed to get satellite photo, returning only plain map", file plainmap = httpget("mt.google.com/"+ file) plainmap = Image.open(StringIO(plainmap.read())).convert("RGB") plainmap.save(fakefile, "JPEG", optimize=1) result = fakefile.getvalue() send("HTTP/1.0 200 OK\n") send("Content-Type: %s\n\n" % overlay.getheader("content-type")) send(result) finally: channel.close() handlers -= 1 print handlers, "handlers left" # Set up the server: server = socket.socket ( socket.AF_INET, socket.SOCK_STREAM ) port = 42845 #import random #port -= random.randint(0,1) server.bind ( ( '', port ) ) server.listen ( 1000 ) print "Serving at", port # Have the server serve "forever": try: while True: channel, details = server.accept() thread.start_new_thread( handler, ( channel, details )) handlers += 1 except: pass server.close() print "...quit"