def geocode(rawLocation): """ :rtype : Location """ url = "https://maps.googleapis.com/maps/api/geocode/json?address=%s" % rawLocation.replace(" ","+") response = urllib2.urlopen(url) geocode = json.load(response) #debug pprint.pprint(geocode) location = Location() location.status = geocode["status"].encode('utf_8').decode('utf_8') if location.status == "OK": location.address = geocode["results"][0]["formatted_address"].encode('utf_8').decode('utf_8') location.latlng = ndb.GeoPt(geocode["results"][0]["geometry"]["location"]["lat"], geocode["results"][0]["geometry"]["location"]["lng"]) else: location.address = None location.latlng = None # Debugs print "in geocode" print (location.status.encode('utf_8') if location.status else None) print type(location.status) print (location.address.encode('utf_8') if location.address else None) print type(location.address) return location
def add_location(data, location_key=""): if location_key: location = Location.get_by_id(location_key) else: location_id = slugify(data["name"]) temp_location_id = location_id while True: count = 1 if Location.get_by_id(temp_location_id): temp_location_id = location_id + str(count) count += 1 else: location = Location(id=temp_location_id) break if data["name"]: location.name = data["name"] if data["needs"]: location.needs = data["needs"] if data["centers"]: location.centers = data["centers"] if data["latlong"]: location.latlong = data["latlong"] if data["featured_photo"]: location.featured_photo = data["featured_photo"] if data["death_count"]: location.death_count = int(data["death_count"]) if data["death_count_text"]: location.death_count_text = data["death_count_text"] if data["affected_count"]: location.affected_count = int(data["affected_count"]) if data["affected_count_text"]: location.affected_count_text = data["affected_count_text"] if data["status_board"]: location.status_board = data["status_board"] if data["needs"]: location.needs = data["needs"] if data["status"]: location.status = data["status"] if data["images"]: location.images = data["images"] if data["hash_tag"]: location.hash_tag = data["hash_tag"] location.put() return location