Skip to content

Commit

Permalink
Merge pull request #166 from mattjoyce/main
Browse files Browse the repository at this point in the history
Allow override of API URL
  • Loading branch information
nickscamara committed May 20, 2024
2 parents c74f757 + 7e5ef4d commit 4bb536e
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions apps/python-sdk/firecrawl/firecrawl.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
import time

class FirecrawlApp:
def __init__(self, api_key=None):
def __init__(self, api_key=None, api_url='https://api.firecrawl.dev'):
self.api_key = api_key or os.getenv('FIRECRAWL_API_KEY')
if self.api_key is None:
raise ValueError('No API key provided')
self.api_url = api_url or os.getenv('FIRECRAWL_API_URL')



Expand Down Expand Up @@ -38,7 +39,7 @@ def scrape_url(self, url: str, params: Optional[Dict[str, Any]] = None) -> Any:
scrape_params[key] = value
# Make the POST request with the prepared headers and JSON data
response = requests.post(
'https://api.firecrawl.dev/v0/scrape',
f'{self.api_url}/v0/scrape',
headers=headers,
json=scrape_params
)
Expand All @@ -63,7 +64,7 @@ def search(self, query, params=None):
if params:
json_data.update(params)
response = requests.post(
'https://api.firecrawl.dev/v0/search',
f'{self.api_url}/v0/search',
headers=headers,
json=json_data
)
Expand All @@ -85,7 +86,7 @@ def crawl_url(self, url, params=None, wait_until_done=True, timeout=2):
json_data = {'url': url}
if params:
json_data.update(params)
response = self._post_request('https://api.firecrawl.dev/v0/crawl', json_data, headers)
response = self._post_request(f'{self.api_url}/v0/crawl', json_data, headers)
if response.status_code == 200:
job_id = response.json().get('jobId')
if wait_until_done:
Expand All @@ -97,7 +98,7 @@ def crawl_url(self, url, params=None, wait_until_done=True, timeout=2):

def check_crawl_status(self, job_id):
headers = self._prepare_headers()
response = self._get_request(f'https://api.firecrawl.dev/v0/crawl/status/{job_id}', headers)
response = self._get_request(f'{self.api_url}/v0/crawl/status/{job_id}', headers)
if response.status_code == 200:
return response.json()
else:
Expand Down Expand Up @@ -130,7 +131,7 @@ def _get_request(self, url, headers, retries=3, backoff_factor=0.5):
def _monitor_job_status(self, job_id, headers, timeout):
import time
while True:
status_response = self._get_request(f'https://api.firecrawl.dev/v0/crawl/status/{job_id}', headers)
status_response = self._get_request(f'{self.api_url}/v0/crawl/status/{job_id}', headers)
if status_response.status_code == 200:
status_data = status_response.json()
if status_data['status'] == 'completed':
Expand Down

0 comments on commit 4bb536e

Please sign in to comment.