Пример #1
0
def get_animal_types(pf: Petfinder):
    animal_types = pf.animal_types()
    animal_list = []
    for animal in animal_types['types']:
        animal_list.append(animal['name'])

    return np.array(animal_list)
Пример #2
0
def __get_org_ids(pf: Petfinder, orgname=None):
    try:
        orgs = pf.organizations(name=orgname,
                                results_per_page=100,
                                pages=1,
                                return_df=True)
        # print(orgs['name'][0:5])
        # print(orgs.shape)
        return orgs['id']
    except:
        # print('Failed')
        return 0
Пример #3
0
def authenticate():
    pf = Petfinder(key=key, secret=secret_key)

    return pf
Пример #4
0
def test_authentication():
    p = Petfinder(key=key, secret=secret_key)

    assert isinstance(p._auth, str)
Пример #5
0
def find_pets(pf: Petfinder,
              location=None,
              animal_type=None,
              breed=None,
              size=None,
              gender=None,
              age=None,
              color=None,
              coat=None,
              org_name=None,
              distance=None,
              name=None,
              good_with=[],
              house_trained=None,
              special_needs=None,
              sort=None):
    actual_compatible = [None, None, None]
    possible_compatible = ['cat', 'dog', 'children']
    if len(good_with) != 0:
        for i in range(len(possible_compatible)):
            if possible_compatible[i] in good_with:
                actual_compatible[i] = True

    org_ids = None
    searches = 0
    if org_name is not None:
        """
        To do: Create a method that searches for the full name of an org. If the name cannot be found, slice the string
        and retry the search with the sliced string. Find the search result in the dataframe by checking name column
        value.
        """
        org_ids = __get_org_ids(pf, org_name)
        searches += 1
        if not org_ids:
            return 0, searches
    try:
        ret_pets = pf.animals(location=location,
                              animal_type=animal_type,
                              breed=breed,
                              size=size,
                              gender=gender,
                              age=age,
                              color=color,
                              coat=coat,
                              distance=distance,
                              name=name,
                              good_with_cats=actual_compatible[0],
                              good_with_dogs=actual_compatible[1],
                              good_with_children=actual_compatible[2],
                              results_per_page=50,
                              organization_id=org_ids,
                              pages=1,
                              sort=sort,
                              return_df=True)
        searches += 1
    except:
        return 0, searches

    if house_trained is not None:
        ret_pets = ret_pets.loc[ret_pets['attributes.house_trained'] == True]
    if special_needs is not None:
        ret_pets = ret_pets.loc[ret_pets['attributes.special_needs'] == True]

    ret_pets = __add_coords_col(ret_pets)
    return ret_pets, searches
Пример #6
0
def authenticate(key_val, secret_key_val):
    pf = Petfinder(key=key_val, secret=secret_key_val)
    return pf
Пример #7
0
def find_pets(pf: Petfinder,
              location=None,
              animal_type=None,
              breed=None,
              size=None,
              gender=None,
              age=None,
              color=None,
              coat=None,
              org_name=None,
              distance=None,
              name=None,
              good_with=[],
              house_trained=None,
              special_needs=None,
              sort=None,
              lat_long=False):
    # Create a boolean list of values related to what the animal is good_with
    actual_compatible = [None, None, None]
    possible_compatible = ['cat', 'dog', 'children']
    if len(good_with) != 0:
        for i in range(len(possible_compatible)):
            if possible_compatible[i] in good_with:
                actual_compatible[i] = True

    search_count = 0
    org_ids = None
    if org_name is not None:
        """
        If the org name is not found in the database, meaning a possible faulty search occurred, slice words from the
        front of the org name until either organization ids are returned or none are. The organization ids returned will 
        be used to search for pets within those organizations.
        """
        tmp_name = org_name
        org_ids = __get_org_ids(pf, orgname=tmp_name)
        search_count += 1
        if not isinstance(org_ids, Series):
            length = len(tmp_name.split()) - 1
            org_bool = False
            for i in range(length):
                tmp_name = tmp_name.split(' ', 1)[1]
                org_ids = __get_org_ids(pf, orgname=tmp_name)
                search_count += 1
                if isinstance(org_ids, Series):
                    org_bool = True
                    break

            if not org_bool:
                return 0, search_count

    # Check to see if animals exist for each org
    animals_in = False
    if isinstance(org_ids, Series):
        org_ids = org_ids.tolist()
    else:
        org_ids = [None]
    pets_list = []
    for i in range(len(org_ids)):
        try:
            pets: DataFrame = pf.animals(
                location=location,
                animal_type=animal_type,
                breed=breed,
                size=size,
                gender=gender,
                age=age,
                color=color,
                coat=coat,
                distance=distance,
                name=name,
                good_with_cats=actual_compatible[0],
                good_with_dogs=actual_compatible[1],
                good_with_children=actual_compatible[2],
                results_per_page=50,
                organization_id=org_ids[i],
                pages=1,
                sort=sort,
                return_df=True)
            if i == 0:
                pets_list = pets
            else:
                pets_list = pets_list.loc[:, ~pets_list.columns.duplicated()]
                pets = pets.loc[:, ~pets.columns.duplicated()]
                # Add pets from org i to total pets dataframe
                pets_list = concat([pets_list, pets],
                                   ignore_index=True,
                                   axis=0)
            # pets_list.append(pets)
            animals_in = True
            search_count += 1
        except KeyError:
            continue

    if not animals_in:
        return 0, search_count

    # list_of_dicts = [cur_df.T.to_dict().values() for cur_df in pets_list]
    # ret_pets = DataFrame(list(chain(*list_of_dicts)))
    ret_pets = pets_list

    if house_trained is not None:
        ret_pets = ret_pets.loc[ret_pets['attributes.house_trained'] == True]
    if special_needs is not None:
        ret_pets = ret_pets.loc[ret_pets['attributes.special_needs'] == True]

    ret_pets = __add_coords_col(ret_pets.head(n=MAX_PETS), location, lat_long)
    if 'primary_photo_cropped' in pets.columns:
        del pets['primary_photo_cropped']
    ret_pets['primary_photo_cropped.small'] = ret_pets[
        'primary_photo_cropped.small'].fillna('default')

    return ret_pets, search_count
Пример #8
0
def get_animal_breeds(pf: Petfinder, pet_type: str):
    return np.array(pf.breeds(pet_type, return_df=True)['name'])
Пример #9
0
def authenticate():
    pf = Petfinder(str(key))

    return pf