Files
firecrawl/apps/api/src/search/index.ts
T

71 lines
1.4 KiB
TypeScript
Raw Normal View History

2024-07-25 09:48:06 -03:00
import { Logger } from "../../src/lib/logger";
2024-04-24 10:11:01 -07:00
import { SearchResult } from "../../src/lib/entities";
2024-08-16 17:57:11 -03:00
import { googleSearch } from "./googlesearch";
2024-08-16 19:33:57 -04:00
import { fireEngineMap } from "./fireEngine";
2024-09-03 18:26:11 +03:00
import { searchapi_search } from "./searchapi";
2024-08-16 19:33:57 -04:00
import { serper_search } from "./serper";
2024-04-25 10:00:35 -07:00
2024-04-23 16:45:06 -07:00
export async function search({
query,
advanced = false,
num_results = 7,
tbs = null,
filter = null,
lang = "en",
country = "us",
location = undefined,
2024-04-23 16:45:06 -07:00
proxy = null,
sleep_interval = 0,
timeout = 5000,
}: {
query: string;
advanced?: boolean;
num_results?: number;
tbs?: string;
filter?: string;
lang?: string;
country?: string;
location?: string;
2024-04-23 16:45:06 -07:00
proxy?: string;
sleep_interval?: number;
timeout?: number;
2024-08-16 19:33:57 -04:00
}): Promise<SearchResult[]> {
2024-04-23 16:45:06 -07:00
try {
2024-09-03 18:26:11 +03:00
if (process.env.SEARCHAPI_API_KEY) {
return await searchapi_search(query, {
num_results,
tbs,
filter,
lang,
country,
location
});
}
2024-08-16 19:33:57 -04:00
if (process.env.SERPER_API_KEY) {
return await serper_search(query, {
num_results,
tbs,
filter,
lang,
country,
location,
});
2024-04-23 16:45:06 -07:00
}
2024-08-16 17:57:11 -03:00
return await googleSearch(
2024-04-23 16:45:06 -07:00
query,
advanced,
num_results,
tbs,
filter,
lang,
country,
2024-04-23 16:45:06 -07:00
proxy,
sleep_interval,
timeout
);
} catch (error) {
2024-07-25 09:48:06 -03:00
Logger.error(`Error in search function: ${error}`);
2024-08-16 19:33:57 -04:00
return [];
2024-04-23 16:45:06 -07:00
}
}