Beispiel #1
0
def search_city(city_name,country,insdata=None):
    if not insdata and country!='il':
        return search_city_google(city_name,country)
    if not insdata:
        import urllib
        params = urllib.urlencode({'city_name':city_name}) #,'street_name':u'מכבי'.encode('utf8')})
        murl = 'http://www.waze.co.il/WAS/search?%s'%params
        f = urllib.urlopen(murl)
        import simplejson
        resp = simplejson.load(f)
    else:
        resp = insdata

    if resp['successful']:
        from greencouriers.model import City,meta
        commit=False
        qres=meta.Session.query(City).filter_by(name=city_name,country=country).all()
        if qres:
            ct = qres[0]
        else: 
            ct = City()
            ct.country=country
            ct.name = city_name
            meta.Session.add(ct)
            commit=True
        if ct and not ct.lon:
            ct.lon = resp['result']['center']['lon']
            ct.lat = resp['result']['center']['lat']
            ct.extent_top=resp['result']['extent']['top']
            ct.extent_bottom=resp['result']['extent']['bottom']
            ct.extent_right=resp['result']['extent']['right']
            ct.extent_left=resp['result']['extent']['left']
            ct.population=-1
            commit=True
        if commit:meta.Session.commit()
    return resp