Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -121,6 +121,63 @@ def search_pets(params):
|
|
121 |
st.error(f"⚠️ Error searching pets: {str(e)}")
|
122 |
return None
|
123 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
124 |
# Function to get breeds
|
125 |
def get_breeds(animal_type):
|
126 |
token = get_access_token()
|
@@ -199,6 +256,9 @@ def display_pet_card(pet, is_favorite=False, context="search"):
|
|
199 |
tags_html += f"<span class='tag'>{pet['gender']}</span> "
|
200 |
if pet['size']:
|
201 |
tags_html += f"<span class='tag'>{pet['size']}</span> "
|
|
|
|
|
|
|
202 |
|
203 |
st.markdown(f"<div>{tags_html}</div>", unsafe_allow_html=True)
|
204 |
|
@@ -417,7 +477,10 @@ def display_pet_card(pet, is_favorite=False, context="search", tab_id="tab1"):
|
|
417 |
tags_html += f"<span class='tag'>{pet['gender']}</span> "
|
418 |
if pet['size']:
|
419 |
tags_html += f"<span class='tag'>{pet['size']}</span> "
|
420 |
-
|
|
|
|
|
|
|
421 |
st.markdown(f"<div>{tags_html}</div>", unsafe_allow_html=True)
|
422 |
|
423 |
st.markdown("<div class='pet-details'>", unsafe_allow_html=True)
|
@@ -537,9 +600,25 @@ def main():
|
|
537 |
|
538 |
# Perform search
|
539 |
results = search_pets(params)
|
|
|
|
|
540 |
if results and 'animals' in results:
|
541 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
542 |
st.session_state.page = 1
|
|
|
|
|
|
|
543 |
st.success(f"Found {len(results['animals'])} pets!")
|
544 |
else:
|
545 |
st.error("No pets found with those criteria. Try expanding your search.")
|
|
|
121 |
st.error(f"⚠️ Error searching pets: {str(e)}")
|
122 |
return None
|
123 |
|
124 |
+
# Function to search pets from RescueGroups.org
|
125 |
+
def search_rescuegroups_pets(params):
|
126 |
+
api_key = os.environ.get('RESCUEGROUPS_API_KEY') or st.secrets.get('RESCUEGROUPS_API_KEY')
|
127 |
+
if not api_key:
|
128 |
+
st.error("⚠️ RescueGroups API key missing. Please set it.")
|
129 |
+
return []
|
130 |
+
|
131 |
+
url = "https://api.rescuegroups.org/v5/public/animals/search/available"
|
132 |
+
|
133 |
+
headers = {
|
134 |
+
"Authorization": f"Bearer {api_key}",
|
135 |
+
"Content-Type": "application/vnd.api+json"
|
136 |
+
}
|
137 |
+
|
138 |
+
# Build RescueGroups.org query (adjust as needed based on API spec)
|
139 |
+
query_params = {
|
140 |
+
"filter[species]": params.get("type", ""),
|
141 |
+
"filter[location]": params.get("location", ""),
|
142 |
+
"page[limit]": 100
|
143 |
+
}
|
144 |
+
|
145 |
+
try:
|
146 |
+
response = requests.get(url, headers=headers, params=query_params)
|
147 |
+
response.raise_for_status()
|
148 |
+
animals = response.json().get('data', [])
|
149 |
+
formatted_pets = []
|
150 |
+
|
151 |
+
# Normalize result
|
152 |
+
for pet in animals:
|
153 |
+
formatted_pet = {
|
154 |
+
'id': f"rg_{pet['id']}",
|
155 |
+
'name': pet['attributes']['name'],
|
156 |
+
'status': 'adoptable',
|
157 |
+
'age': pet['attributes']['ageGroup'],
|
158 |
+
'gender': pet['attributes']['sex'],
|
159 |
+
'size': pet['attributes']['size'],
|
160 |
+
'photos': [{'medium': pet['attributes'].get('pictureThumbnailUrl', '')}] if pet['attributes'].get('pictureThumbnailUrl') else [],
|
161 |
+
'description': pet['attributes']['descriptionText'],
|
162 |
+
'breeds': {'primary': pet['attributes']['breedPrimary'], 'secondary': pet['attributes']['breedSecondary'], 'mixed': False},
|
163 |
+
'colors': {'primary': None, 'secondary': None, 'tertiary': None},
|
164 |
+
'contact': {
|
165 |
+
'address': {'city': pet['attributes'].get('locationCity', ''), 'state': pet['attributes'].get('locationState', ''), 'postcode': ''},
|
166 |
+
'email': pet['attributes'].get('contactEmail', ''),
|
167 |
+
'phone': pet['attributes'].get('contactPhone', '')
|
168 |
+
},
|
169 |
+
'organization_id': pet['relationships'].get('orgs', {}).get('data', [{}])[0].get('id', ''),
|
170 |
+
'source': 'RescueGroups'
|
171 |
+
}
|
172 |
+
formatted_pets.append(formatted_pet)
|
173 |
+
|
174 |
+
return formatted_pets
|
175 |
+
|
176 |
+
except requests.exceptions.RequestException as e:
|
177 |
+
st.error(f"⚠️ Error searching RescueGroups pets: {str(e)}")
|
178 |
+
return []
|
179 |
+
|
180 |
+
|
181 |
# Function to get breeds
|
182 |
def get_breeds(animal_type):
|
183 |
token = get_access_token()
|
|
|
256 |
tags_html += f"<span class='tag'>{pet['gender']}</span> "
|
257 |
if pet['size']:
|
258 |
tags_html += f"<span class='tag'>{pet['size']}</span> "
|
259 |
+
# Attribution source tag
|
260 |
+
if 'source' in pet:
|
261 |
+
st.markdown(f"<span class='tag' style='background-color: #444;'>Source: {pet['source']}</span>", unsafe_allow_html=True)
|
262 |
|
263 |
st.markdown(f"<div>{tags_html}</div>", unsafe_allow_html=True)
|
264 |
|
|
|
477 |
tags_html += f"<span class='tag'>{pet['gender']}</span> "
|
478 |
if pet['size']:
|
479 |
tags_html += f"<span class='tag'>{pet['size']}</span> "
|
480 |
+
# Attribution source tag
|
481 |
+
if 'source' in pet:
|
482 |
+
st.markdown(f"<span class='tag' style='background-color: #444;'>Source: {pet['source']}</span>", unsafe_allow_html=True)
|
483 |
+
|
484 |
st.markdown(f"<div>{tags_html}</div>", unsafe_allow_html=True)
|
485 |
|
486 |
st.markdown("<div class='pet-details'>", unsafe_allow_html=True)
|
|
|
600 |
|
601 |
# Perform search
|
602 |
results = search_pets(params)
|
603 |
+
combined_results = []
|
604 |
+
|
605 |
if results and 'animals' in results:
|
606 |
+
pf_pets = results['animals']
|
607 |
+
# Add source attribution
|
608 |
+
for pet in pf_pets:
|
609 |
+
pet['source'] = 'PetFinder'
|
610 |
+
combined_results.extend(pf_pets)
|
611 |
+
|
612 |
+
# Fetch from RescueGroups
|
613 |
+
rg_pets = search_rescuegroups_pets(params)
|
614 |
+
combined_results.extend(rg_pets)
|
615 |
+
|
616 |
+
if combined_results:
|
617 |
+
st.session_state.search_results = {'animals': combined_results}
|
618 |
st.session_state.page = 1
|
619 |
+
st.success(f"Found {len(combined_results)} pets from PetFinder and RescueGroups!")
|
620 |
+
else:
|
621 |
+
st.error("No pets found with those criteria.")
|
622 |
st.success(f"Found {len(results['animals'])} pets!")
|
623 |
else:
|
624 |
st.error("No pets found with those criteria. Try expanding your search.")
|