Nick: search

This commit is contained in:
Nicolas 2025-05-29 17:00:13 -03:00
parent a8e0482718
commit 9297afd1ff
2 changed files with 71 additions and 4 deletions

View File

@ -5,6 +5,58 @@ import { logger } from "../lib/logger";
dotenv.config();
export async function fire_engine_search(
q: string,
options: {
tbs?: string;
filter?: string;
lang?: string;
country?: string;
location?: string;
numResults: number;
page?: number;
},
abort?: AbortSignal,
): Promise<SearchResult[]> {
try {
let data = JSON.stringify({
query: q,
lang: options.lang,
country: options.country,
location: options.location,
tbs: options.tbs,
numResults: options.numResults,
page: options.page ?? 1,
});
if (!process.env.FIRE_ENGINE_BETA_URL) {
return [];
}
const response = await fetch(`${process.env.FIRE_ENGINE_BETA_URL}/search`, {
method: "POST",
headers: {
"Content-Type": "application/json",
"X-Disable-Cache": "true",
},
body: data,
signal: abort,
});
if (response.ok) {
const responseData = await response.json();
return responseData;
} else {
return [];
}
} catch (error) {
logger.error(error);
Sentry.captureException(error);
return [];
}
}
export async function fireEngineMap(
q: string,
options: {
@ -34,7 +86,7 @@ export async function fireEngineMap(
return [];
}
const response = await fetch(`${process.env.FIRE_ENGINE_BETA_URL}/search`, {
const response = await fetch(`${process.env.FIRE_ENGINE_BETA_URL}/map`, {
method: "POST",
headers: {
"Content-Type": "application/json",

View File

@ -4,6 +4,7 @@ import { googleSearch } from "./googlesearch";
import { searchapi_search } from "./searchapi";
import { serper_search } from "./serper";
import { searxng_search } from "./searxng";
import { fire_engine_search } from "./fireEngine";
export async function search({
query,
@ -31,8 +32,19 @@ export async function search({
timeout?: number;
}): Promise<SearchResult[]> {
try {
if (process.env.FIRE_ENGINE_BETA_URL) {
const results = await fire_engine_search(query, {
numResults: num_results,
tbs,
filter,
lang,
country,
location,
});
if (results.length > 0) return results;
}
if (process.env.SERPER_API_KEY) {
return await serper_search(query, {
const results = await serper_search(query, {
num_results,
tbs,
filter,
@ -40,9 +52,10 @@ export async function search({
country,
location,
});
if (results.length > 0) return results;
}
if (process.env.SEARCHAPI_API_KEY) {
return await searchapi_search(query, {
const results = await searchapi_search(query, {
num_results,
tbs,
filter,
@ -50,9 +63,10 @@ export async function search({
country,
location,
});
if (results.length > 0) return results;
}
if (process.env.SEARXNG_ENDPOINT) {
return await searxng_search(query, {
const results = await searxng_search(query, {
num_results,
tbs,
filter,
@ -60,6 +74,7 @@ export async function search({
country,
location,
});
if (results.length > 0) return results;
}
return await googleSearch(
query,