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

55 lines
1.1 KiB
TypeScript
Raw Normal View History

2024-04-24 10:11:01 -07:00
import { SearchResult } from "../../src/lib/entities";
2024-04-23 16:45:06 -07:00
import { google_search } from "./googlesearch";
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-04-24 10:11:01 -07:00
}) : Promise<SearchResult[]> {
2024-04-23 16:45:06 -07:00
try {
2024-04-24 18:00:25 -07: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
}
return await google_search(
query,
advanced,
num_results,
tbs,
filter,
lang,
country,
2024-04-23 16:45:06 -07:00
proxy,
sleep_interval,
timeout
);
} catch (error) {
console.error("Error in search function: ", error);
return []
}
// if process.env.SERPER_API_KEY is set, use serper
}