def do_GET(self): result = urllib.parse.urlparse(self.path) query = urllib.parse.parse_qs(result.query) def sparam(name): return query[name][-1] def fparam(name): return float(sparam(name)) params = pysearch.Params() try: params.query = sparam('query') params.locale = sparam('locale') params.position = pysearch.Mercator(fparam('posx'), fparam('posy')) params.viewport = pysearch.Viewport( pysearch.Mercator(fparam('minx'), fparam('miny')), pysearch.Mercator(fparam('maxx'), fparam('maxy'))) except KeyError: self.send_response(400) return results = HTTPHandler.engine.query(params) responses = [{ 'name': result.name, 'address': result.address, 'has_center': result.has_center, 'center': { 'x': result.center.x, 'y': result.center.y } } for result in results] self.send_response(200) self.send_header("Content-Type", "application/json") self.end_headers() self.wfile.write(json.dumps(responses).encode())
import os import pysearch as search DIR = os.path.dirname(__file__) RESOURCE_PATH = os.path.realpath(os.path.join(DIR, '..', '..', 'data')) MWM_PATH = os.path.realpath(os.path.join(DIR, '..', '..', 'data')) parser = argparse.ArgumentParser( formatter_class=argparse.ArgumentDefaultsHelpFormatter) parser.add_argument('-r', metavar='RESOURCE_PATH', default=RESOURCE_PATH, help='Path to resources directory.') parser.add_argument('-m', metavar='MWM_PATH', default=MWM_PATH, help='Path to mwm files.') args = parser.parse_args() search.init(args.r, args.m) engine = search.SearchEngine() params = search.Params() params.query = 'кафе юность' params.locale = 'ru' params.position = search.Mercator(37.618705, 67.455669) params.viewport = search.Viewport(search.Mercator(37.1336, 67.1349), search.Mercator(38.0314, 67.7348)) print(engine.query(params)) print(engine.trace(params))