def handle(self, *args, **kwargs): zips_text = get_cached_url("https://raw.github.com/yourcelf/getzips/master/zips.csv") reader = csv.reader(StringIO(zips_text)) zip_to_state = {} for zipcode,city,state in reader: zip_to_state[zipcode] = state zip_to_state['78721'] = 'TX' zip_to_state['14517'] = 'NY' zip_to_state['80234'] = 'CO' qs = Answer.objects.filter(question__widget="geo") for term in blacklist: qs = qs.exclude(answer=term) problems = set() no_states = set() for answer in qs: if answer.answer in whitelist: Geocode.objects.get_or_create(**whitelist[answer.answer]) continue if not Geocode.objects.filter(term=answer.answer).exists(): is_zip = re.match("^\d{5}$", answer.answer) if is_zip: query = "zipcode:%s;country:US" % answer.answer else: query = query_substitution.get(answer.answer, "city:%s" % (answer.answer.replace("_", "%20")) ) url = "http://geocoding.cloudmade.com/%s/geocoding/v2/find.js?query=%s" % (settings.CLOUDMADE_API_KEY, query) json_text = get_cached_url(url) if json_text != "": data = json.loads(json_text) else: data = {} #print answer.answer, data if 'found' in data and data['found'] != 0: feature = data['features'][0] if is_zip: state = zip_to_state[answer.answer] elif 'is_in' in feature['properties'] and feature['properties']['is_in'].endswith("USA"): if "," in feature['properties']['is_in']: state = feature['properties']['is_in'].split(",")[-2] elif " " in feature['properties']['is_in']: state = feature['properties']['is_in'].split(" ")[-2] state = STATES_NORMALIZED.get(state.lower().strip(), "") else: no_states.add(answer.answer) state = "" Geocode.objects.get_or_create( term=answer.answer, lat=feature['centroid']['coordinates'][0], lng=feature['centroid']['coordinates'][1], state=state ) else: problems.add(answer.answer) print "No states" pprint.pprint(no_states) print "Problems" pprint.pprint(problems)
def state_abbr(value): return STATES_NORMALIZED.get(value.strip().lower(), '')
def get_postal_code(self): return STATES_NORMALIZED.get(self.name.lower())