Showing the changes to apps/python-sdk (1 of 3 files). Show all
Unified · Split
| @@ -542,6 +542,7 @@ class FirecrawlApp: · 541 unmodified lines | |||
| 542 | } | 542 | } |
| 543 | 543 | ||
| 544 | try: | 544 | try: |
| 545 | # Send the initial extract request | ||
| 545 | response = self._post_request( | 546 | response = self._post_request( |
| 546 | f'{self.api_url}/v1/extract', | 547 | f'{self.api_url}/v1/extract', |
| 547 | request_data, | 548 | request_data, |
| @@ -550,7 +551,29 @@ class FirecrawlApp: · 2 unmodified lines | |||
| 550 | if response.status_code == 200: | 551 | if response.status_code == 200: |
| 551 | data = response.json() | 552 | data = response.json() |
| 552 | if data['success']: | 553 | if data['success']: |
| 553 | return data | 554 | job_id = data.get('id') |
| 555 | if not job_id: | ||
| 556 | raise Exception('Job ID not returned from extract request.') | ||
| 557 | |||
| 558 | # Poll for the extract status | ||
| 559 | while True: | ||
| 560 | status_response = self._get_request( | ||
| 561 | f'{self.api_url}/v1/extract/{job_id}', | ||
| 562 | headers | ||
| 563 | ) | ||
| 564 | if status_response.status_code == 200: | ||
| 565 | status_data = status_response.json() | ||
| 566 | if status_data['status'] == 'completed': | ||
| 567 | if status_data['success']: | ||
| 568 | return status_data | ||
| 569 | else: | ||
| 570 | raise Exception(f'Failed to extract. Error: {status_data["error"]}') | ||
| 571 | elif status_data['status'] in ['failed', 'cancelled']: | ||
| 572 | raise Exception(f'Extract job {status_data["status"]}. Error: {status_data["error"]}') | ||
| 573 | else: | ||
| 574 | self._handle_error(status_response, "extract-status") | ||
| 575 | |||
| 576 | time.sleep(2) # Polling interval | ||
| 554 | else: | 577 | else: |
| 555 | raise Exception(f'Failed to extract. Error: {data["error"]}') | 578 | raise Exception(f'Failed to extract. Error: {data["error"]}') |
| 556 | else: | 579 | else: |