Beispiel #1
0
    def city_autocomplete(self,city_name):
        import urllib
        params = urllib.urlencode({'q':city_name.encode('utf8'),'limit':10,'name':'city_name'})
        url = 'http://www.waze.co.il/WAS/autocomplete?%s'%params
        f = urllib.urlopen(url)
        fstr = f.read()
        results = fstr.strip().split("\n")
        #just for client debugging
        #if 'REFERER' not in request.headers:return fstr
        from greencouriers.model import City,meta
        for res_name in results:
            if not res_name.strip(): continue
            ctres = meta.Session.query(City).filter_by(name=res_name.strip()).all()
            if not ctres:
                ct = City()
                ct.name = res_name.strip()
                ct.population=-1
                import pylons
                ct.country = config['global_conf']['default_country'].lower()

                meta.Session.add(ct) ; 
        meta.Session.commit()
        import simplejson
        #response.headers['content-type']='application/json'
        return simplejson.dumps({'results':[{'id':res,'value':res} for res in results]
                                 ,'for':city_name})
Beispiel #2
0
 def index(self):
     from pylons import config
     import os,re
     rg = re.compile('^(?P<id>[0-9]*) \t(?P<city_name>[^\t]*)\t(?P<mahoz>[^\t]*)\t(?P<population>[0-9,]*)')
     rt=''
     fname = os.path.join(config['pylons.paths']['root'],'towns.txt')
     rt+='reading from %s\n'%fname
     f = open(fname)
     for line in f:
         res = rg.search(line)
         if not res:
             rt+='cannot parse "%s"\n'%line
             continue
         ct = City()
         ct.name=res.group('city_name').strip()
         if meta.Session.query(City).filter_by(name=ct.name).all():
             rt+='city %s ALREADY EXISTS! skipping\n'%ct.name
             continue
         ct.population = int(re.compile('([,]+)').sub('',res.group('population').strip()))
         if request.params.get('getlonlat'):
             lonlat = search_city(ct.name)
             if lonlat['successful']:
                 ct.lon = lonlat['result']['center']['lon']
                 ct.lat = lonlat['result']['center']['lat']
         meta.Session.add(ct)
         rt+='inserted city %s'%ct.name
     meta.Session.commit()
     c.output=rt
     return render('/parse_towns.html')
Beispiel #3
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