Skip to content

Commit 6ef8bb4

Browse files
authored
Merge pull request #133 from AIRInstitute/pre
Fix: Capture "Too many request" error in xenocanto extractor
2 parents 9a0d4b6 + 1d17b42 commit 6ef8bb4

1 file changed

Lines changed: 42 additions & 15 deletions

File tree

ia4birds-all/ai4birds-ingest-service/ai4birds_ingest_service/model/extractor/xenocanto_extractor.py

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,14 @@ async def fetch_page(self, session, page):
3030
"""
3131
async with self.semaphore:
3232
async with session.get(self.BASE_URL.format(page)) as resp:
33+
if resp.status == 429:
34+
raise ClientResponseError(
35+
request_info=resp.request_info,
36+
history=resp.history,
37+
status=resp.status,
38+
message="Too Many Requests",
39+
headers=resp.headers
40+
)
3341
resp.raise_for_status()
3442
return await resp.json()
3543

@@ -95,18 +103,37 @@ def _format_results(self, data):
95103
list: A list of dictionaries with formatted species and recording details.
96104
"""
97105
species_list = config.SPECIES_LIST.values()
98-
return [{
99-
"speciesSciName": f"{bird['gen']} {bird['sp']}",
100-
"recordings": [{
101-
"recordingId": bird['id'],
102-
"location": bird['loc'],
103-
"quality": bird['q'],
104-
"lat": bird['lat'],
105-
"lng": bird['lng'],
106-
"alt": bird['alt'],
107-
"file": bird['file'],
108-
"file-name": bird['file-name'],
109-
"time": bird['time'],
110-
"date": bird['date']
111-
}]
112-
} for bird in data if f"{bird['gen']} {bird['sp']}" in species_list]
106+
formatted_results = []
107+
108+
for bird in data:
109+
try:
110+
sci_name = f"{bird.get('gen')} {bird.get('sp')}"
111+
if sci_name not in species_list:
112+
continue
113+
114+
# Asegúrate de que los campos críticos están presentes
115+
required_fields = ['id', 'loc', 'q', 'lat', 'lng', 'alt', 'file', 'file-name', 'time', 'date']
116+
if not all(field in bird and bird[field] not in [None, ''] for field in required_fields):
117+
logger.warning(f"Skipping incomplete bird record: {bird}")
118+
continue
119+
120+
formatted_results.append({
121+
"speciesSciName": sci_name,
122+
"recordings": [{
123+
"recordingId": bird.get('id'),
124+
"location": bird.get('loc'),
125+
"quality": bird.get('q'),
126+
"lat": bird.get('lat'),
127+
"lng": bird.get('lng'),
128+
"alt": bird.get('alt'),
129+
"file": bird.get('file'),
130+
"file-name": bird.get('file-name'),
131+
"time": bird.get('time'),
132+
"date": bird.get('date')
133+
}]
134+
})
135+
except Exception as e:
136+
logger.warning(f"Error formatting bird record: {e} | Record: {bird}")
137+
138+
return formatted_results
139+

0 commit comments

Comments
 (0)