Beispiel #1
0
    def street_autocomplete(self,city_name,street_name):
        output='street_autocomplete(%s,%s)'%(city_name,street_name)
        log.info(output) 
        #raise Exception(output)
        import urllib
        params = urllib.urlencode({'q':street_name.encode('utf8'),'limit':10,'name':'street_name','city_name':city_name.encode('utf8')})
        url = 'http://www.waze.co.il/WAS/autocomplete?%s'%params
        f = urllib.urlopen(url)
        results = f.read().strip().split("\n")
        from greencouriers.model import City,Street
        try:
            ct = meta.Session.query(City).filter_by(name=city_name).one()
        except NoResultFound:
            ct=None
        import simplejson
        if ct:
            for sres in results:
                street_name = sres.strip()
                if not street_name: continue
                if not meta.Session.query(Street).filter_by(city=ct,name=street_name).all():
                    st = Street()
                    st.city=ct
                    st.name = street_name
                    import pylons

                    st.country = config['global_conf']['default_country'].lower()
                    #raise Exception('setting country to %s'%st.country)
                    meta.Session.add(st); meta.Session.commit()
            #response.headers['content-type']='application/json'
        return simplejson.dumps({'results':[{'id':res,'value':res} for res in results]})
Beispiel #2
0
def search_street(city_name,street_name,country,insdata=None):
    if not insdata and country!='il':
        return search_city_google(city_name,country,street_name)
    if not insdata:
        import urllib
        params = urllib.urlencode({'street_name':street_name.encode('utf8'),'city_name':city_name.encode('utf8')}) #,'street_name':u'מכבי'.encode('utf8')})
        murl = 'http://www.waze.co.il/WAS/search?%s'%params
        f = urllib.urlopen(murl)
        import simplejson
        fstr=f.read()
        resp = simplejson.loads(fstr)

    else:
        resp = insdata
    try:
        if resp['successful']:
            from greencouriers.model import City,Street
            #find city
            ct = meta.Session.query(City).filter_by(name=city_name,country=country).one()
            if ct:
                try:
                    to_create=False
                    st = meta.Session.query(Street).filter_by(city=ct,name=street_name).one()
                except NoResultFound:
                    st = Street()
                    st.name = street_name
                    st.city=ct
                    st.country=country
                    to_create=True
                st.lon = resp['result']['center']['lon']
                st.lat = resp['result']['center']['lat']
                st.extent_top = resp['result']['extent']['top']
                st.extent_bottom = resp['result']['extent']['bottom']
                st.extent_left = resp['result']['extent']['left']
                st.extent_right = resp['result']['extent']['right']
                if to_create:
                    if not st.country: raise Exception('country unset in street')
                    meta.Session.add(st) 
                meta.Session.commit()
    except ValueError:
        import re
        if re.compile('HTTP Status 500').search(fstr):
            return simplejson.dumps({'succesful':False,'reason':'remote server returned 500'})
        raise Exception('unexpected response from server')
    return resp