def extract_information(nct_list):

    result_list = []
    pure_lat_long_list = []

    selected_df = aact_trial[aact_trial['nct_id'].isin(nct_list)]

    for index, zipcode, b in zip(selected_df.nct_id, selected_df.zip_codes,
                                 selected_df.brief_title):
        if pd.isnull(zipcode) == False:
            zipcodelist = zipcode.split('|')
            for z in zipcodelist:
                z = z[:5]
                if zipcodes.matching(z):
                    current_location = [
                        zipcodes.matching(z)[0]['lat'],
                        zipcodes.matching(z)[0]['long']
                    ]
                    result_list.append([
                        index,
                        zipcodes.matching(z)[0]['lat'],
                        zipcodes.matching(z)[0]['long'], b
                    ])
                    pure_lat_long_list.append(current_location)
    return result_list, pure_lat_long_list
Example #2
0
    def get(self, request):
        username = request.user

        records = TrackerRecordSerializer(TrackRecord.objects.filter(user__username=username), many=True).data

        covid_cases = CovidCase.objects.latest('timestamp')
        zipcode = User.objects.get(username=username).postal_code
        local_info = {}
        if zipcodes.matching(zipcode):
            county = zipcodes.matching(zipcode)[0]['county']
            county_name = county.split(' ')[0]
            if county_name in covid_cases.counties_json:
                local_info['local_cases'] = covid_cases.counties_json[county_name]['total']
                local_info['local_deaths'] = covid_cases.counties_json[county_name]['deaths']

        return Response(data={
            'tracker': records,
            'covid_cases': {
                **CovidCaseSerializer(covid_cases).data,
                **local_info
            },
            'pollen': {
                **get_pollen_data(zipcode)
            },
            'bewei': {
                **self._get_beiwe(username)
            }
        }, status=status.HTTP_200_OK)
Example #3
0
def map(data_visited):
    zips = pd.read_csv("data/zips/zips.csv")
    unique_zips = []
    counties = []
    cities = []
    states = []

    map_of_hikes = folium.Map(location=[34.9, -118.8863],
                              tiles='Stamen Terrain',
                              zoom_start=6,
                              zoom_control=True,
                              left=75)

    for index, row in data_visited.iterrows():
        country = row["Location"].split(", ")[-1].lower()

        epsilon = 0
        if row["Zip"] not in unique_zips:
            unique_zips.append(row["Zip"])
            if zipcodes.matching(row["Zip"])[0]['county'] not in counties:
                counties.append(zipcodes.matching(row["Zip"])[0]['county'])
        else:
            epsilon = epsilon + random.uniform(0, 1) / 100

        if country == "us":
            info = zips.loc[zips["Zip"] == int(row["Zip"])]
            lat = info["Latitude"] + epsilon
            lon = info["Longitude"] + epsilon

        else:
            # loc = geolocator.geocode(row["Location"] + ", " + row["Zip"])
            # lat = loc.latitude
            # lon = loc.longitude
            pass

        text = row["Name"] + "\t" + "Date: " + str(
            row["Date"]) + "\t" + "Length: " + str(row["Length"])
        folium.Marker(location=[lat, lon],
                      icon=folium.Icon(color='lightred'),
                      popup=folium.Popup(text,
                                         max_width=100)).add_to(map_of_hikes)

    map_of_hikes.save("temp.html")

    with open("map.html", "w") as infile:

        with open("header.html", "r") as outfile1:
            for line in outfile1:
                infile.write(line)

        with open("temp.html", "r") as outfile2:
            for line in outfile2:
                infile.write(line)

    infile.close()
    outfile1.close()
    outfile2.close()

    return len(unique_zips), len(counties)
def calculateDistance(zip1, zip2):
    zipcode1 = zipcodes.matching(str(zip1))
    zipcode2 = zipcodes.matching(str(zip2))
    z1 = (float(zipcode1[0]["lat"]), float(zipcode1[0]["long"]))
    z2 = (float(zipcode2[0]["lat"]), float(zipcode2[0]["long"]))

    dist = mpu.haversine_distance(z1, z2)

    dist = round((dist / 2) + (((dist / 2)) / 4), 2)
    return dist
Example #5
0
def check_restaurant(restaurant):
    print('checking restaurant data: {}'.format(restaurant['id']))
    restaurant = Restaurant(restaurant['Address'], restaurant['id'],
                            restaurant['GeoLocation']['Latitude'],
                            restaurant['GeoLocation']['Longitude'])

    try:
        zipcode = restaurant.address['PostCode'].encode('UTF-8').strip()
        returned_json = zipcodes.matching(zipcode)
    except:
        zipcode = restaurant.address['PostCode']
        if zipcode is None:
            restaurant.problem = 'no zipcode'
        else:
            restaurant.problem = 'zipcode couldn\'t be parsed: {}'.format(
                zipcode.encode('UTF-8'))
        dirty_restaurants.append(restaurant)
        return

    if len(returned_json) == 0:
        restaurant.problem = 'could not match zipcode: {}'.format(
            zipcode.encode('UTF-8'))
        dirty_restaurants.append(restaurant)
        return

    returned_address = returned_json[0]

    check_city(restaurant, returned_address)
    check_state(restaurant, returned_address)
    check_zipcode(restaurant, returned_address)
Example #6
0
def get_data_from_zipcode(zipcode):
    """get data from NYT csv based on a zip-code lookup"""
    #return -1 if zipcode doesn't exist
    if not zipcodes.is_real(zipcode):
        return -1
    try:
        zipcode_result = zipcodes.matching(zipcode)
        state = states_code_to_full[zipcode_result[0]['state']]
        county = zipcode_result[0]['county'].replace('County', '').strip()

        #special case for NYC for NYT data
        if "New York" in county:
            county = "New York City"
        return {
            'state':
            state,
            'county':
            county,
            'confirmed':
            data[data.county == county][data.state == state][['cases']].iat[0,
                                                                            0],
            'deaths':
            data[data.county == county][data.state == state][['deaths'
                                                              ]].iat[0, 0],
        }
    except:
        return -1
Example #7
0
def validate_zips(df):
    df_out = pd.DataFrame()
    
    for index, row in df.iterrows():
        
        record = zipcodes.matching(row['zip_code'])
        
        if len(record) > 0:
            row['validated_state'] = record[0]['state']
            row['validated_city'] = record[0]['city'] 
            if row['validated_state'] == row['state'].upper():
                row['state_match'] = 'Y'
            else:
                row['state_match'] = 'N'
            if row['validated_city'] == row['city'].upper():
                row['city_match'] = 'Y'
            else:
                row['city_match'] = 'N'
        else:
            row['validated_state'] = ''
            row['validated_city'] = ''
        
        df_out = df_out.append(row)
        
    return df_out
Example #8
0
def sanitize_event_payload(payload):
    """
    Remove potentially sensitive fields from the Mobilize America API event
    response.

    This method is required because we fetch events using the authenticated GET
    /events API, which can return private fields that we don't want to serve
    """
    location = payload.pop("location", None)
    address_vis = payload.pop("address_visibility", None)
    sanitized = {k: v for k, v in payload.items() if k in __MA_FIELD_WHITELIST}
    if address_vis == PUBLIC_VISIBILITY:
        sanitized["location"] = location
    elif location and location.get("postal_code"):
        matching_codes = zipcodes.matching(location.get("postal_code"))
        if len(matching_codes) > 0:
            zip_code_data = matching_codes[0]
            location_from_zip = {
                "location": {
                    "longitude": float(zip_code_data["long"]),
                    "latitude": float(zip_code_data["lat"]),
                }
            }
            sanitized["location"] = location_from_zip

    return sanitized
    def begin(self, data):
        self.perc_complete(10)

        vals, keys = get_args_from_form(data)

        try:
            zip_code = vals[4]
            zips = zipcodes.matching(zip_code)
            city = zips[0]['city']
            state = zips[0]['state']
        except:
            city = "not defined"
            state = "not defined"
            zip_code = "00000"

        new_vals = [city, state]
        vals += new_vals
        new_keys = ['city', 'state']
        keys += new_keys

        dbactions = DBActions(owner_id=self.id,
                              table='customer_basic',
                              keys=keys,
                              vals=vals)
        query = dbactions.update()
        query += " WHERE id = %s" % (self.id, )

        db.execute(query, False, vals, commit=True)
def zipno(list):
    zipnomi = (input('enter zip code in 12345 format pls: '))
    if zipnomi == 'state':
        pass
    else:
        while True:
            try:
                ziplist = len(str(zipnomi))
                if ziplist == 5:
                    zipper =[0]
                    from pprint import pprint
                    import zipcodes
                    zipper[0] = (zipcodes.matching(zipnomi))
                    if len(zipper[0]) == 0:
                        print('not there try again')
                        zipno(list)
                    else:
                        list[8] = zipper[0][0]['zip_code']
                        list[7] = zipper[0][0]['state']
                        list[6] = zipper[0][0]['city']
                else:
                    print('nooooo')
                    zipno(list)
            except ValueError :
                print('Exceptumondo Dude! this thing just takes numbers!!!Try again!')
                zipno(list)
                break
            else:
                break
Example #11
0
def getForecastData(zipcode, response, dayNum, dayOfWeek):
    """
    Use "null" for dayOfWeek param 
    unless using string format id = 2
    """
    # Uses zipcode to get city name and state.
    zipCodeInfo = zipcodes.matching(zipcode)
    zipCodeInfo = zipCodeInfo[0]

    # Store in easy to use variables
    city = zipCodeInfo["city"].lower().title()
    state = zipCodeInfo["state"]
    currentTemp = response["main"]["temp"]
    currentTemp = int(currentTemp)
    description = response["weather"][0]["description"]
    winds = response["wind"]["speed"]
    winds = int(winds)

    # Take current date's high's and lows
    highLow = getHighLows(zipcode, dayNum)
    high = highLow[0]
    low = highLow[1]

    # Forecast array
    forecast = [
        city, state, currentTemp, description, winds, high, low, dayOfWeek
    ]
    return forecast
Example #12
0
def update():

    st.write("Bot: " + "What is your zip code?")
    code = talk("What is your zip code?")

    try:
        x = zipcodes.matching(code)
        c = x[0]['county']
        s = x[0]['state']
        cases = int(new_cases[(new_cases['county'] == c)
                              & (new_cases['state'] == s)].cases)
        deaths = int(new_cases[(new_cases['county'] == c)
                               & (new_cases['state'] == s)].deaths)
        cases2 = int(counties[(counties['county'] == c)
                              & (counties['state'] == s)].cases)
        deaths2 = int(counties[(counties['county'] == c)
                               & (counties['state'] == s)].deaths)
        msg = "There have been " + str(cases) + " new cases and " + str(
            deaths) + " new deaths in " + c + " as of " + date2
        msg += ". There have been " + str(cases2) + " total cases and " + str(
            deaths2) + " total deaths in " + c + "."
        st.write("Bot: " + msg)
        say(msg)

    except:
        st.write("Bot: " + "Sorry I did not get that. Please try again.")
        say("Sorry, I did not get that. Please try again.")
        update()
Example #13
0
def grab_weather(wi, zc):
    call = make_request(zc)

    wi.set_city(call['name'])
    wi.set_state(zipcodes.matching(str(zc))[0]['state'])
    grab_information(call, wi)
    show_weather(wi)
Example #14
0
def isZipValid(z):
    try:
        if not zipcodes.matching(z):
            return False
        else:
            return True
    except ValueError:
        pass
Example #15
0
def merchantSearch(merchantName,merchantPostalCode):
	country=zipcodes.matching(merchantPostalCode)[0]["country"]
	merchantCountryCode = pycountry.countries.get(alpha_2=country).numeric
	payload["searchAttrList"]["merchantName"]=merchantName
	payload["searchAttrList"]["merchantPostalCode"]=merchantPostalCode
	payload["searchAttrList"]["merchantCountryCode"]=merchantCountryCode

	response = requests.request("POST",  url,cert=(cert,key), headers=headers, json = payload)
	return(response.text.encode('utf8'))
Example #16
0
def lf_contains_zipcode(x):
    try:
        match = zipcodes.matching(str(x.text))
        if len(match) > 0:
            return ZIPCODE
        else:
            return ABSTAIN
    except:
        return ABSTAIN
Example #17
0
def read_county_stats_zip_ny(zipcode: str) -> Dict:
    """Return stats for New York State zip_codes
    """

    zip_info = zipcodes.matching(str(zipcode))[0]
    county = zip_info["county"].rsplit(" ", 1)[0]
    state = zip_info["state"]

    try:
        deaths = pd.read_csv(app_config.STATE_DEATH)
        confirmed_df = pd.read_csv(app_config.STATE_CONFIRMED)
    except:
        raise DataReadingError(
            f"Data reading error State: {state}, and County: {county}.")

    try:
        confirmed_df = confirmed_df[confirmed_df["Province_State"] ==
                                    reverse_states_map[state]]
        confirmed_df = confirmed_df[confirmed_df["Admin2"] == county]

        confirmed = confirmed_df.iloc[:, -1]
        new_confirmed = (confirmed_df.iloc[:, 12:].astype("int32").diff(
            axis=1).iloc[:, -1].values[0])

        # used data source 2 for new death number
        deaths = deaths[deaths["Province_State"] == reverse_states_map[state]]
        deaths = deaths[deaths["Admin2"] == county]
        # 4/15/20: force cast into int before diff as pd sometimes read as
        # float and throws nan.
        death = deaths.iloc[:, -1]
        new_death = (deaths.iloc[:, 12:].astype("int32").diff(
            axis=1).iloc[:, -1].values[0])
        try:
            fatality_rate = int(death) / int(confirmed)
        except:  # pylint: disable=W0702
            fatality_rate = 0

        data = {
            "county_name": county,
            "state_name": reverse_states_map[state],
            "confirmed": int(confirmed),
            "new": int(new_confirmed),
            "death": int(death),
            "new_death": int(new_death),
            "fatality_rate": f"{fatality_rate}%",
            "latitude": float(zip_info["lat"]),
            "longitude": float(zip_info["long"]),
            "last_update": str("2020-04-17 19:50 EDT"),
        }
        print(data)
        # data = json.dumps(data)
        # print(data)
    except:
        raise DataValidationError(
            f"Can't find State: {state}, and County: {county} combination.")
    return data
Example #18
0
def _zip_to_lat_long(zipcode):
    """ Returns the lat / long for a zip code, or None if none is found. """
    zip_location_list = zipcodes.matching(zipcode)
    try:
        first_location = zip_location_list.pop()
        lat = float(first_location.get("lat"))
        lon = float(first_location.get("long"))
    except Exception:
        return None, None
    return lat, lon
Example #19
0
 def parse_zipcode(self, value):
     try:
         result = zipcodes.matching(value)
         if (len(result) > 1):
             return result[0]
         else:
             return None
     except ValueError:
         return None
     except TypeError:
         raise TypeError
 def getInvalidZips(self):
     zipCodeFields = [self.patientZip, self.providerZip]
     invalidZips = []
     for zip in zipCodeFields:
         if not zip:
             continue
         if not (re.match("^\d{5}$", zip)
                 or re.match("^\d{5}-\d{4}$", zip)):
             issue = (zip, "Invalid zip format")
             invalidZips.append(issue)
         elif not zipcodes.matching(zip):
             issue = (zip, "Zip appears not to exist")
             invalidZips.append(issue)
     return invalidZips
Example #21
0
    def validate(self, document):
        """Validates zipcodes

        Args:
            document: User input to questionary prompt

        References:
            Zipcodes: https://pypi.org/project/zipcodes/

        Returns:
            True if user zipcode is valid number with len=5 and according to zipcodes package

        """
        ok = False
        if document.text.isdigit() and len(document.text) == 5:
        # Only take non-Hawaii and non-Alaska zipcodes
            ok = zipcodes.is_real(document.text) and \
                 (zipcodes.matching(document.text)[0]['state'] != 'HI') and \
                 (zipcodes.matching(document.text)[0]['state'] != 'AK')
        if not ok:
            raise ValidationError(
                message='Please enter a valid zipcode',
                cursor_position=len(document.text))  # Move cursor to end
Example #22
0
def correctLostLeadingZeroZipcode(zip: str, expectedState: str):
    if not zip:
        return zip
    if not expectedState:
        return zip
    try:
        zipData = zipcodes.matching(zip)
    except ValueError:
        return zip
    if zipData:
        return zip
    if len(zip) < 5:
        proposedZip = zip.zfill(5)
        try:
            proposedZipInfo = zipcodes.matching(proposedZip)
        except ValueError:
            return zip
        proposedState = proposedZipInfo[0]["state"]
        if proposedState.upper().strip() == expectedState.upper().strip():
            return proposedZip
        else:
            return zip
    return zip
Example #23
0
def get_zipcode_info(zipcode_str):

    """
    :param zipcode_str: -> String; zipcode for which the info is required
    :return: -> dict; keys :- isValid, city, state.
    """

    zipcode_info = zipcodes.matching(zipcode_str)
    result = {__IS_VALID__:False, __CITY__: None, __STATE__: None}
    if zipcode_info:
        result[__IS_VALID__] = True
        result[__CITY__] = zipcode_info[0]["city"]
        result[__STATE__] = zipcode_info[0]["state"]

    return result
Example #24
0
def valid_zip(field_value, field_name):
    """Validates the zip code by using the python zipcodes library.

    Args:
        field_value:str
        field_name:str
    Returns:
        returns Validation Result Object
    """
    try:
        zip_code_normal = zipcodes.matching(field_value)

        return ValidationResult('passed', field_value, field_name)
    except:
        return ValidationResult('failed', field_value, field_name)
def fetch_data(zip_code=72204):
    '''
    This function uses the input zipcode to generate city location and also
    create the url where the weather data will be fetched from.
    The weather data is then fetched from this url using the date generated.
        zipcode = 5 digit US zipcode only.
    '''

    #Get today's date
    today = date.today()
    todays_date = today.strftime("%a %#m/%d/%Y").replace('x0', '')
    year = today.strftime("%Y")

    #Generate url using zipcode
    city = z.matching(zip_code)[0]['city']
    state = z.matching(zip_code)[0]['state']
    city_state = city.replace(' ', '-').lower() + '/' + zip_code
    state_url = 'https://www.accuweather.com/en/browse-locations/nam/us/' + state.lower(
    )

    #Generate url specific location id
    agent = {"User-Agent": 'Chrome/80.0.3987.132'}
    res = requests.get(state_url, headers=agent)
    s = BeautifulSoup(res.text, features="lxml")
    # Getting the specific location of the url_location_id requires looking through the text extracted from the url
    text_list = s.findAll('script')
    city_id = text_list[2].get_text().strip()
    pattern = '"' + city + '","id":"(.+?)","localizedName":"' + city + '"'
    url_location_id = re.search(pattern, city_id).group(1)
    location_url = 'https://www.accuweather.com/en/us/' + city_state + '/daily-weather-forecast/' + url_location_id

    #Extract entire data from location_url
    res = requests.get(location_url, headers=agent)
    soup = BeautifulSoup(res.content, features="lxml")

    return soup, year, location_url, todays_date, city
Example #26
0
    def addr_to_latlong(self, str_address, add_delay=False):
        """

        :param str_address:  The string expression of address. Most accurate when OSM Geocrawler is initialized with region_search set to "city_name, state_name"
        :return: Tuple of (lat, long) in floating points
        """
        # zip_code = re.search(r'.*(\d{5}(\-\d{4})?)$', str_address)
        # zip_code = re.search(r"(.*\d{5}-\d{4}\b|.*\d{5})", str_address)
        if add_delay:
            gc = RateLimiter(self.locator.geocode,
                             min_delay_seconds=1,
                             error_wait_seconds=10,
                             swallow_exceptions=True,
                             max_retries=5000)
        regex = re.compile(r"[0-9]{5}(?:-[0-9]{4})?")

        matches = re.findall(regex, str_address)
        # print(matches)
        try:
            assert len(matches) == 1
            zip_code_str = matches[0]
            # zip_code_str = zip_code.groups()[0]
            print(f"*** zip_code_str:{zip_code_str}")
        except:
            zip_code_str = None

        if zip_code_str is not None:
            try:
                CITY = zipcodes.matching(zip_code_str)[0]["city"]
                City = CITY[:1] + CITY[1:].lower()
                str_address = str_address.replace(zip_code_str, City)
                print(str_address)

            except:
                print(
                    "No zipcode and/or cannot replace zipcode with city name.")

        try:
            location = self.locator.geocode(str_address)
        # print("Latitude = {}, Longitude = {}".format(location.latitude, location.longitude))
        except GeocoderTimedOut:
            time.sleep(2)
            return self.addr_to_latlong(str_address)

        if location is not None:
            return (location.latitude, location.longitude)
        else:
            return None
def zip2FIPS(zip:str):
    if not zip:
        return ""
    try:
        zipData = zipcodes.matching(zip)
    except ValueError:
        return ""
    if not zipData:
        print("Warning: Zipcode %s does not have any associated information." % zip)
        return ""
    state = zipData[0]["state"]
    county = zipData[0]["county"]
    try:
        return fipsData[state][county]
    except KeyError:
        return ""
def is_valid_zip_code(zip_code):
    # This function validates the pattern of the zip code. The regular expression below
    # returns valid for five digit value as well as five digits with hyphen followed by four or 5 digits.:
    # Example of valid patterns : 78133, 78133-3212
    # Example pf invalid patterns: Los Angels, 8976541,87676-123456
    result = False
    try:
        result = zipcodes.matching(zip_code.split('-')[0])
        if result.__len__() > 0:
            result = True
        else:
            result = False
    except:
        print("Error matching zip code.")

    return result
Example #29
0
def normalize_zip(entry, field_name):
    """Returns a zip code that is 5 digits long, utilizes python zipcodes package

        Args:
            entry:custom entry (Ex.EligibilityEntry, CensusEntry)
            field_name: str
        Returns:
            None (modifies object attr)
        """

    try:
        zip_code_normal = zipcodes.matching(getattr(entry,
                                                    field_name))[0]['zip_code']
        setattr(entry, field_name, zip_code_normal)
    except:
        setattr(entry, field_name, None)
Example #30
0
    def get_message(phone):

        account_sid = "ACd3dbda8dd8bf0f40cec66339052a9eca"
        auth_token = "INSERT TOKEN HERE (TWILIO DEACTIVATES TOKENS THAT ARE SHARED ON PUBLIC GITHUB REPOSITORIES) "

        try:
            x = zipcodes.matching(code)
            c = x[0]['county']
            s = x[0]['state']
            cases = int(new_cases[(new_cases['county'] == c)
                                  & (new_cases['state'] == s)].cases)
            deaths = int(new_cases[(new_cases['county'] == c)
                                   & (new_cases['state'] == s)].deaths)
            cases2 = int(counties[(counties['county'] == c)
                                  & (counties['state'] == s)].cases)
            deaths2 = int(counties[(counties['county'] == c)
                                   & (counties['state'] == s)].deaths)
            msg = "There have been " + str(cases) + " new cases and " + str(
                deaths) + " new deaths in " + c + " as of " + date2
            msg += ". There have been " + str(
                cases2) + " total cases and " + str(
                    deaths2) + " total deaths in " + c + "."
            temp = msg

            r = requests.get('https://www.reddit.com/r/Coronavirus/top/.json',
                             headers={'user-agent': 'Mozilla/5.0'})
            top_headers = []
            for i in range(10):
                post = r.json()['data']['children'][i]['data']['title']
                top_headers.append(post)
            temp2 = (' | '.join(top_headers[0:5]))

            client = Client(account_sid, auth_token)

            message = client.messages.create(body="Update: " + temp +
                                             "\nNews: " + temp2,
                                             from_='+14242971568',
                                             to='+1' + phone.replace('-', ''))

            print(message.sid)
            st.write("Bot: " + "Updates and news successfully sent to " +
                     phone + ".")

        except:
            st.write("Bot: " + "Sorry I did not get that. Please try again.")
            say("Sorry, I did not get that. Please try again.")
            mobile()