def serve_data(): item_type = request.args.get('item_type', None) rargs = request.args.copy() kwargs = {} # Copy request args into a copy for k in rargs.keys(): if k != 'item_type': kwargs[k] = rargs[k] if '_id' in k: kwargs[k] = ObjectId(rargs[k]) if item_type == 'cart': objs = carts.find(**kwargs) else: objs = reviews.find(**kwargs) results = [o._obj for o in objs] # Remove incompatible types for r in results: serialize(r) # Return results as an array data = {'results': results} return json.dumps(data)
def load(): carts.remove_all() request = "http://data.cityofnewyork.us/resource/akqf-qv4n.json" for i in range(0, 24000, 1000): query = "?$offset=%d" % i data = urllib2.urlopen(request + query) results = json.loads(data.read()) data.close() required_keys = [ "license_permit_holder", "license_permit_holder_name", "license_permit_number", "permit_issuance_date", "permit_expiration_date", "longitude_wgs84", "latitude_wgs84", "zip_code", "borough", ] for r in results: for k in required_keys: if not r.has_key(k): r[k] = "" try: carts.insert( name=r["license_permit_holder"], owner=r["license_permit_holder_name"], permit_number=r["license_permit_number"], issuance=r["permit_issuance_date"], expiration=r["permit_expiration_date"], loc=[float(r["latitude_wgs84"]), float(r["longitude_wgs84"])], zip_code=r["zip_code"], borough=r["borough"], ) except ValueError: carts.insert( name=r["license_permit_holder"], owner=r["license_permit_holder_name"], permit_number=r["license_permit_number"], issuance=r["permit_issuance_date"], expiration=r["permit_expiration_date"], loc=[None, None], zip_code=r["zip_code"], borough=r["borough"], ) out = [c for c in carts.find()] print len(out)
def search(item_type, offset, number, keywords, location): objs = [] # Change objs depending on item type if item_type == 'cart': # Get results for each keyword for word in keywords: if word == '': continue # Check each text field for tfield in carts.text_fields(): kwd = re.compile(r'(?: |^)' + word + '(?: |$)', re.IGNORECASE) search_object = {tfield: kwd} if len(objs) == 0: if location != '': objs += get_carts_near(location, offset, number, search_object) else: objs += carts.find(offset, number, **search_object) search_object = {'tags': {'$in': [word]}} objs += carts.find(offset, number, **search_object) # If there are no keywords, use location instead if len(objs) == 0 and keywords[0] == '' and len(keywords) == 1: objs += get_carts_near(location, offset, number) elif item_type == 'review': for word in keywords: for tfield in reviews.text_fields(): kwd = re.compile(r'(?: |^)' + word + '(?: |$)', re.IGNORECASE) search_object = {tfield: kwd} objs += reviews.find(**search_object) return unique_objs(objs)
def add_zips(): for c in carts.find(): try: data = urllib2.urlopen('http://maps.googleapis.com/maps/api/geocode/json?api_key%s&sensor=false&latlng=%f,%f' % (api_key, c.loc[0], c.loc[1])) except TypeError: continue try: data = json.loads(data.read()) zip_code = data['results'][0]['address_components'][-1]['long_name'] c.zip_code = zip_code c.save() except IndexError: continue
def load(): carts.remove_all() request = 'http://data.cityofnewyork.us/resource/akqf-qv4n.json' for i in range(0, 24000, 1000): query = '?$offset=%d' % i data = urllib2.urlopen(request + query) results = json.loads(data.read()) data.close() required_keys = ['license_permit_holder', 'license_permit_holder_name', 'license_permit_number', 'permit_issuance_date', 'permit_expiration_date', 'longitude_wgs84', 'latitude_wgs84', 'zip_code', 'borough'] for r in results: for k in required_keys: if not r.has_key(k): r[k] = '' try: carts.insert(name=r['license_permit_holder'], owner=r['license_permit_holder_name'], permit_number=r['license_permit_number'], issuance=r['permit_issuance_date'], expiration=r['permit_expiration_date'], loc=[ float(r['latitude_wgs84']), float(r['longitude_wgs84'])], zip_code=r['zip_code'], borough=r['borough']) except ValueError: carts.insert(name=r['license_permit_holder'], owner=r['license_permit_holder_name'], permit_number=r['license_permit_number'], issuance=r['permit_issuance_date'], expiration=r['permit_expiration_date'], loc=[ None, None ], zip_code=r['zip_code'], borough=r['borough']) out = [c for c in carts.find()] print len(out)