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 | 544 | try: |
| 545 | + # Send the initial extract request | |
| 545 | 546 | response = self._post_request( |
| 546 | 547 | f'{self.api_url}/v1/extract', |
| 547 | 548 | request_data, |
| @@ -550,7 +551,29 @@ class FirecrawlApp: · 2 unmodified lines | ||
| 550 | 551 | if response.status_code == 200: |
| 551 | 552 | data = response.json() |
| 552 | 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 | 577 | else: |
| 555 | 578 | raise Exception(f'Failed to extract. Error: {data["error"]}') |
| 556 | 579 | else: |