mirror of
https://git.mirrors.martin98.com/https://github.com/mendableai/firecrawl
synced 2025-08-12 20:39:00 +08:00
Nick: fixed js sdk
This commit is contained in:
parent
07a6ba5d91
commit
eae393afb5
@ -290,7 +290,6 @@ export class FirecrawlError extends Error {
|
|||||||
* Defines options for searching and scraping search results.
|
* Defines options for searching and scraping search results.
|
||||||
*/
|
*/
|
||||||
export interface SearchParams {
|
export interface SearchParams {
|
||||||
query: string;
|
|
||||||
limit?: number;
|
limit?: number;
|
||||||
tbs?: string;
|
tbs?: string;
|
||||||
filter?: string;
|
filter?: string;
|
||||||
@ -401,26 +400,27 @@ export default class FirecrawlApp {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Searches using the Firecrawl API and optionally scrapes the results.
|
* Searches using the Firecrawl API and optionally scrapes the results.
|
||||||
* @param params - Parameters for the search request.
|
* @param query - The search query string.
|
||||||
|
* @param params - Optional parameters for the search request.
|
||||||
* @returns The response from the search operation.
|
* @returns The response from the search operation.
|
||||||
*/
|
*/
|
||||||
async search(params: SearchParams): Promise<SearchResponse> {
|
async search(query: string, params?: SearchParams | Record<string, any>): Promise<SearchResponse> {
|
||||||
const headers: AxiosRequestHeaders = {
|
const headers: AxiosRequestHeaders = {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
Authorization: `Bearer ${this.apiKey}`,
|
Authorization: `Bearer ${this.apiKey}`,
|
||||||
} as AxiosRequestHeaders;
|
} as AxiosRequestHeaders;
|
||||||
|
|
||||||
let jsonData: any = {
|
let jsonData: any = {
|
||||||
query: params.query,
|
query,
|
||||||
limit: params.limit ?? 5,
|
limit: params?.limit ?? 5,
|
||||||
tbs: params.tbs,
|
tbs: params?.tbs,
|
||||||
filter: params.filter,
|
filter: params?.filter,
|
||||||
lang: params.lang ?? "en",
|
lang: params?.lang ?? "en",
|
||||||
country: params.country ?? "us",
|
country: params?.country ?? "us",
|
||||||
location: params.location,
|
location: params?.location,
|
||||||
origin: params.origin ?? "api",
|
origin: params?.origin ?? "api",
|
||||||
timeout: params.timeout ?? 60000,
|
timeout: params?.timeout ?? 60000,
|
||||||
scrapeOptions: params.scrapeOptions ?? { formats: [] },
|
scrapeOptions: params?.scrapeOptions ?? { formats: [] },
|
||||||
};
|
};
|
||||||
|
|
||||||
if (jsonData?.scrapeOptions?.extract?.schema) {
|
if (jsonData?.scrapeOptions?.extract?.schema) {
|
||||||
@ -456,7 +456,7 @@ export default class FirecrawlApp {
|
|||||||
if (responseData.success) {
|
if (responseData.success) {
|
||||||
return {
|
return {
|
||||||
success: true,
|
success: true,
|
||||||
data: responseData.data,
|
data: responseData.data as FirecrawlDocument<any>[],
|
||||||
warning: responseData.warning,
|
warning: responseData.warning,
|
||||||
};
|
};
|
||||||
} else {
|
} else {
|
||||||
|
@ -21,22 +21,19 @@ import websockets
|
|||||||
|
|
||||||
logger : logging.Logger = logging.getLogger("firecrawl")
|
logger : logging.Logger = logging.getLogger("firecrawl")
|
||||||
|
|
||||||
class FirecrawlApp:
|
class SearchParams(pydantic.BaseModel):
|
||||||
class SearchParams(pydantic.BaseModel):
|
query: str
|
||||||
"""
|
limit: Optional[int] = 5
|
||||||
Parameters for the search operation.
|
tbs: Optional[str] = None
|
||||||
"""
|
filter: Optional[str] = None
|
||||||
query: str
|
lang: Optional[str] = "en"
|
||||||
limit: Optional[int] = 5
|
country: Optional[str] = "us"
|
||||||
tbs: Optional[str] = None
|
location: Optional[str] = None
|
||||||
filter: Optional[str] = None
|
origin: Optional[str] = "api"
|
||||||
lang: Optional[str] = "en"
|
timeout: Optional[int] = 60000
|
||||||
country: Optional[str] = "us"
|
scrapeOptions: Optional[Dict[str, Any]] = None
|
||||||
location: Optional[str] = None
|
|
||||||
origin: Optional[str] = "api"
|
|
||||||
timeout: Optional[int] = 60000
|
|
||||||
scrapeOptions: Optional[Dict[str, Any]] = None
|
|
||||||
|
|
||||||
|
class FirecrawlApp:
|
||||||
class SearchResponse(pydantic.BaseModel):
|
class SearchResponse(pydantic.BaseModel):
|
||||||
"""
|
"""
|
||||||
Response from the search operation.
|
Response from the search operation.
|
||||||
@ -133,60 +130,36 @@ class FirecrawlApp:
|
|||||||
else:
|
else:
|
||||||
self._handle_error(response, 'scrape URL')
|
self._handle_error(response, 'scrape URL')
|
||||||
|
|
||||||
def search(self, params: Union[str, SearchParams, Dict[str, Any]]) -> SearchResponse:
|
def search(self, query: str, params: Optional[Union[Dict[str, Any], SearchParams]] = None) -> Dict[str, Any]:
|
||||||
"""
|
"""
|
||||||
Search using the Firecrawl API and optionally scrape the results.
|
Search for content using the Firecrawl API.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
params (Union[str, SearchParams, Dict[str, Any]]): Search parameters. Can be:
|
query (str): The search query string.
|
||||||
- A string representing the search query
|
params (Optional[Union[Dict[str, Any], SearchParams]]): Additional search parameters.
|
||||||
- A SearchParams object
|
|
||||||
- A dictionary with search parameters
|
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
SearchResponse: The response from the search operation.
|
Dict[str, Any]: The search response containing success status and search results.
|
||||||
|
|
||||||
Raises:
|
|
||||||
Exception: If the search operation fails.
|
|
||||||
"""
|
"""
|
||||||
# Convert string query to SearchParams
|
if params is None:
|
||||||
if isinstance(params, str):
|
params = {}
|
||||||
params = self.SearchParams(query=params)
|
|
||||||
# Convert dict to SearchParams
|
|
||||||
elif isinstance(params, dict):
|
|
||||||
params = self.SearchParams(**params)
|
|
||||||
|
|
||||||
# Validate params
|
|
||||||
if not isinstance(params, self.SearchParams):
|
|
||||||
raise ValueError("Invalid search parameters")
|
|
||||||
|
|
||||||
# Convert to dict for request
|
if isinstance(params, dict):
|
||||||
json_data = params.model_dump(exclude_none=True)
|
search_params = SearchParams(query=query, **params)
|
||||||
|
|
||||||
# Handle schema conversion if present in scrapeOptions
|
|
||||||
if json_data.get('scrapeOptions', {}).get('extract', {}).get('schema'):
|
|
||||||
try:
|
|
||||||
schema = json_data['scrapeOptions']['extract']['schema']
|
|
||||||
if isinstance(schema, dict):
|
|
||||||
# Already a JSON schema
|
|
||||||
pass
|
|
||||||
else:
|
|
||||||
# Try to convert from a Pydantic model
|
|
||||||
schema = schema.model_json_schema()
|
|
||||||
json_data['scrapeOptions']['extract']['schema'] = schema
|
|
||||||
except Exception as e:
|
|
||||||
logger.warning(f"Failed to convert schema: {e}")
|
|
||||||
|
|
||||||
headers = self._prepare_headers()
|
|
||||||
response = self._post_request(f'{self.api_url}/v1/search', json_data, headers)
|
|
||||||
|
|
||||||
if response.status_code == 200:
|
|
||||||
response_data = response.json()
|
|
||||||
return self.SearchResponse(**response_data)
|
|
||||||
else:
|
else:
|
||||||
self._handle_error(response, 'search')
|
search_params = params
|
||||||
|
search_params.query = query
|
||||||
|
|
||||||
return self.SearchResponse(success=False, data=[], error="Internal server error.")
|
response = requests.post(
|
||||||
|
f"{self.api_url}/v1/search",
|
||||||
|
headers={"Authorization": f"Bearer {self.api_key}"},
|
||||||
|
json=search_params.dict(exclude_none=True)
|
||||||
|
)
|
||||||
|
|
||||||
|
if response.status_code != 200:
|
||||||
|
raise Exception(f"Request failed with status code {response.status_code}")
|
||||||
|
|
||||||
|
return response.json()
|
||||||
|
|
||||||
def crawl_url(self, url: str,
|
def crawl_url(self, url: str,
|
||||||
params: Optional[Dict[str, Any]] = None,
|
params: Optional[Dict[str, Any]] = None,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user