def geocode_address(self, origin_address): """Geocodes an address string Filters results by settings.STATE Sets self.lat/lng to result Throws exceptions if the number of results isn't exactly 1 """ self.places = [] b = geocoders.Bing(settings.BING_KEY) try: place, (lat, lng) = b.geocode(origin_address) self.places.append((place, lat, lng)) except ValueError: for result in b.geocode(origin_address, exactly_one=False): place, (lat, lng) = result self.places.append((place, lat, lng)) except: self.places.append(None) self.places = _filter_addresses(self.places) if len(self.places) > 1: raise ValueError("More than one location found") if len(self.places) == 0: raise LookupError("No Locations in PA found") else: self.lat = self.places[0][1] self.lng = self.places[0][2]
def read_scrub_list_and_update(args): try: filename = args[0] except: pass # Thomas' Bing Geocoder api key (free basic access) # dotus_geocoder = geocoders.GeocoderDotUS() for consideration as a fallback bing_geocoder = geocoders.Bing( 'AvxLEwiPhVJzf0S3Pozgg01NnUQQX0RR6g9K46VPLlZ8OfZkKS-76gaPyzoV6IHI') path = os.path.join(settings.BASE_DIR, 'readonly', filename) try: f = open(path) except IOError: print_stack_trace() rows = [] for row in f: rows.append(row.replace("\r\n", "").split("\t")) for row in rows[1:]: # Skip the header try: coupon_pk = int(row[1]) is_duplicate = True if row[3] == '1' else False is_inactive = True if row[4] == '1' else False is_category_wrong = True if row[5] == '1' else False is_location_wrong = True if row[8] == '1' else False correction_needed = is_duplicate or is_inactive or is_category_wrong or is_location_wrong if correction_needed: coupon_obj = Coupon.all_objects.get(pk=coupon_pk) if is_duplicate: coupon_obj.is_duplicate = True # print "Correction: ", coupon_pk, " is_duplicate=True" #DEBUG if is_inactive: coupon_obj.status = 'confirmed-inactive' # print "Correction: ", coupon_pk, " status=confirmed-inactive" #DEBUG if is_category_wrong: coupon_obj.categories.clear() try: parent_category = Category.objects.get( ref_id_source='sqoot', name=row[6]) coupon_obj.categories.add(parent_category) # print "Correction: ", coupon_pk, " Parent category -> ", parent_category.name #DEBUG except: pass try: child_category = Category.objects.get( ref_id_source='sqoot', name=row[7]) coupon_obj.categories.add(child_category) # print "Correction: ", coupon_pk, " Child category -> ", child_category.name #DEBUG except: pass if is_location_wrong: location_obj = coupon_obj.merchant_location address = row[9] if row[9] != '' else '' locality = row[10] if row[10] != '' else '' region = row[11] if row[11] != '' else '' postal_code = row[12] if row[12] != '' else '' spacer1 = ', ' if address != '' else '' spacer2 = ' ' if locality != '' else '' lookup_text = address + spacer1 + locality + spacer2 + region try: place, (lat, lng) = bing_geocoder.geocode(lookup_text) pnt = 'POINT({} {})'.format(lng, lat) location_obj.geometry = pnt except: pass location_obj.address = address if address != '' else location_obj.address location_obj.locality = locality if locality != '' else location_obj.locality location_obj.region = region if region != '' else location_obj.region location_obj.postal_code = postal_code if postal_code != '' else location_obj.postal_code location_obj.save() # print "Correction: ", coupon_pk, " Location fixed" #DEBUG coupon_obj.save() except: print_stack_trace() scrub_list_retrieved = [ row[1] for row in rows[1:] ] # list of original coupon pks imported from 'scrub_list.py' deals_to_scrub = Coupon.all_objects.filter(pk__in=scrub_list_retrieved)\ .exclude(Q(status='confirmed-inactive') | Q(status='implied-inactive') | Q(is_duplicate=True))\ .order_by('merchant__name') probably_dup_deals_list = [ ] # List of coupon pks that look like a duplicate. probably_dup_deals_list = crosscheck_by_field(deals_to_scrub, probably_dup_deals_list, 'coupon_directlink') probably_dup_deals_list = crosscheck_by_field(deals_to_scrub, probably_dup_deals_list, 'merchant_name') probably_dup_deals_list = list(set(probably_dup_deals_list)) for pk in probably_dup_deals_list: try: coupon = Coupon.all_objects.get(pk=pk) coupon.is_duplicate = True coupon.save() # print "Correction: ", coupon_pk, " is_duplicate=True" #DEBUG except: print_stack_trace()