src / image_search / providers / SerpApiImageProvider.ts
src / image_search / providers / SerpApiImageProvider.ts
import { HTTPClient } from "../../http/HTTPClient";
import {
HTTPClientError,
KeyAuthOrQuotaError,
NoAPIKeysError,
ProviderError,
} from "../../models/errors";
import { SearchOptions } from "../../types/search";
import { KeyManager } from "../../utils/KeyManager";
import { ImageSearchResult } from "../models";
import { BaseImageProvider } from "./BaseImageProvider";
export const SERPAPI_SEARCH_URL = "https://serpapi.com/search.json";
const ROTATION_STATUS_CODES = [401, 402, 403, 429];
export interface SerpApiImageProviderOptions {
keys?: string[];
keyManager?: KeyManager;
httpClient?: HTTPClient;
httpTimeoutSeconds?: number;
cooldownHours?: number;
}
/**
* SerpApi Google Images search provider implementation.
* Queries SerpApi Google Images search endpoint and normalizes response payloads.
* Requires SERPAPI_KEYS configuration and supports multi-key rotation via KeyManager.
* Mapped from Python `image_search/providers/serpapi.py` SerpApiImageProvider.
*/
export class SerpApiImageProvider extends BaseImageProvider {
public static readonly PROVIDER_NAME = "serpapi";
private keyManager: KeyManager;
constructor(options: SerpApiImageProviderOptions = {}) {
super({
providerName: SerpApiImageProvider.PROVIDER_NAME,
httpClient: options.httpClient,
httpTimeoutSeconds: options.httpTimeoutSeconds,
});
this.keyManager =
options.keyManager ??
new KeyManager({
providerName: SerpApiImageProvider.PROVIDER_NAME,
keys: options.keys,
defaultCooldownHours: options.cooldownHours ?? 24.0,
});
}
public override setKeys(keys: string[]): void {
this.keyManager.setKeys(keys);
}
/**
* Execute image search using SerpApi Google Images API with automatic multi-key rotation.
*
* @param query Non-empty search query string.
* @param options Search options (numResults, extra kwargs).
* @returns List of normalized ImageSearchResult objects.
* @throws NoAPIKeysError if no API keys are configured for SerpApi.
* @throws ProviderError if all API keys fail authentication/quota or request fails.
*/
public async searchImages(
query: string,
options: SearchOptions = {}
): Promise<ImageSearchResult[]> {
if (!query || !query.trim()) {
return [];
}
const cleanQuery = query.trim();
const totalKeys = this.keyManager.keyCount();
if (totalKeys === 0) {
throw new NoAPIKeysError(`No API keys configured for provider '${this.providerName}'`);
}
let lastError: Error | null = null;
for (let attempt = 0; attempt < totalKeys; attempt++) {
const activeKey = this.keyManager.currentKey();
try {
const results = await this.doSearch(activeKey, cleanQuery, options);
this.keyManager.markKeySuccess(activeKey);
return results;
} catch (err: unknown) {
if (err instanceof KeyAuthOrQuotaError) {
lastError = err as Error;
this.keyManager.markKeyExhausted(activeKey, (err as KeyAuthOrQuotaError).retryAfterSeconds);
if (attempt < totalKeys - 1) {
this.keyManager.nextKey();
}
} else {
throw err;
}
}
}
const msg = `All ${totalKeys} configured API keys for provider '${this.providerName}' failed authentication or quota limits. Last error: ${lastError?.message}`;
throw new ProviderError(msg, { originalError: lastError ?? undefined });
}
private async doSearch(
apiKey: string,
query: string,
options: SearchOptions
): Promise<ImageSearchResult[]> {
const numResults = options.numResults ?? 10;
const { apiKey: _k, numResults: _n, provider: _p, ...extraKwargs } = options;
const params: Record<string, string | number | boolean> = {
engine: "google_images",
q: query,
api_key: apiKey,
num: numResults,
...(extraKwargs as Record<string, string | number | boolean>),
};
let response;
try {
response = await this._httpClient.get(SERPAPI_SEARCH_URL, { params });
} catch (exc: unknown) {
const err = exc instanceof Error ? exc : new Error(String(exc));
throw new ProviderError(`SerpApi image search HTTP request failed: ${err.message}`, {
originalError: err,
});
}
const data = this.parseJsonResponse(response);
const rawResults = data.images_results;
if (!rawResults || !Array.isArray(rawResults)) {
return [];
}
return rawResults.slice(0, numResults).map((item) =>
this.normalizeResult((item as Record<string, unknown>) || {})
);
}
private parseJsonResponse(response: { statusCode: number; data?: unknown; text?: string }): Record<string, unknown> {
if (ROTATION_STATUS_CODES.includes(response.statusCode)) {
throw new KeyAuthOrQuotaError(
`SerpApi image search failed with status code ${response.statusCode}: ${response.text || ""}`,
response.statusCode
);
}
if (response.statusCode !== 200) {
throw new ProviderError(
`SerpApi image search failed with status code ${response.statusCode}: ${response.text || ""}`
);
}
if (!response.data || typeof response.data !== "object") {
throw new ProviderError("Invalid response payload from SerpApi image search API: expected JSON object");
}
const data = response.data as Record<string, unknown>;
if (data.error) {
const errMsg = String(data.error);
const errLower = errMsg.toLowerCase();
if (
errLower.includes("invalid api key") ||
errLower.includes("out of searches") ||
errLower.includes("quota") ||
errLower.includes("unauthorized") ||
errLower.includes("exhausted") ||
errLower.includes("api_key")
) {
throw new KeyAuthOrQuotaError(
`SerpApi API error: ${errMsg}`,
errLower.includes("key") ? 401 : 429
);
}
throw new ProviderError(`SerpApi API error: ${errMsg}`);
}
return data;
}
private normalizeResult(rawItem: Record<string, unknown>): ImageSearchResult {
const title = String(rawItem.title || "");
const imageUrl = String(rawItem.original || rawItem.image || "");
const sourceUrl = String(rawItem.link || rawItem.url || "");
const thumbnailUrl = typeof rawItem.thumbnail === "string" ? rawItem.thumbnail : undefined;
let width: number | undefined = undefined;
if (typeof rawItem.original_width === "number") width = rawItem.original_width;
else if (typeof rawItem.width === "number") width = rawItem.width;
let height: number | undefined = undefined;
if (typeof rawItem.original_height === "number") height = rawItem.original_height;
else if (typeof rawItem.height === "number") height = rawItem.height;
let sourceDomain: string | undefined = undefined;
if (rawItem.source) {
sourceDomain = String(rawItem.source).trim();
} else if (sourceUrl) {
try {
const parsed = new URL(sourceUrl);
sourceDomain = parsed.hostname;
} catch {
sourceDomain = undefined;
}
}
return {
title,
imageUrl,
sourceUrl,
sourceDomain,
width,
height,
thumbnailUrl,
raw: rawItem,
};
}
}
import { HTTPClient } from "../../http/HTTPClient";
import {
HTTPClientError,
KeyAuthOrQuotaError,
NoAPIKeysError,
ProviderError,
} from "../../models/errors";
import { SearchOptions } from "../../types/search";
import { KeyManager } from "../../utils/KeyManager";
import { ImageSearchResult } from "../models";
import { BaseImageProvider } from "./BaseImageProvider";
export const SERPAPI_SEARCH_URL = "https://serpapi.com/search.json";
const ROTATION_STATUS_CODES = [401, 402, 403, 429];
export interface SerpApiImageProviderOptions {
keys?: string[];
keyManager?: KeyManager;
httpClient?: HTTPClient;
httpTimeoutSeconds?: number;
cooldownHours?: number;
}
/**
* SerpApi Google Images search provider implementation.
* Queries SerpApi Google Images search endpoint and normalizes response payloads.
* Requires SERPAPI_KEYS configuration and supports multi-key rotation via KeyManager.
* Mapped from Python `image_search/providers/serpapi.py` SerpApiImageProvider.
*/
export class SerpApiImageProvider extends BaseImageProvider {
public static readonly PROVIDER_NAME = "serpapi";
private keyManager: KeyManager;
constructor(options: SerpApiImageProviderOptions = {}) {
super({
providerName: SerpApiImageProvider.PROVIDER_NAME,
httpClient: options.httpClient,
httpTimeoutSeconds: options.httpTimeoutSeconds,
});
this.keyManager =
options.keyManager ??
new KeyManager({
providerName: SerpApiImageProvider.PROVIDER_NAME,
keys: options.keys,
defaultCooldownHours: options.cooldownHours ?? 24.0,
});
}
public override setKeys(keys: string[]): void {
this.keyManager.setKeys(keys);
}
/**
* Execute image search using SerpApi Google Images API with automatic multi-key rotation.
*
* @param query Non-empty search query string.
* @param options Search options (numResults, extra kwargs).
* @returns List of normalized ImageSearchResult objects.
* @throws NoAPIKeysError if no API keys are configured for SerpApi.
* @throws ProviderError if all API keys fail authentication/quota or request fails.
*/
public async searchImages(
query: string,
options: SearchOptions = {}
): Promise<ImageSearchResult[]> {
if (!query || !query.trim()) {
return [];
}
const cleanQuery = query.trim();
const totalKeys = this.keyManager.keyCount();
if (totalKeys === 0) {
throw new NoAPIKeysError(`No API keys configured for provider '${this.providerName}'`);
}
let lastError: Error | null = null;
for (let attempt = 0; attempt < totalKeys; attempt++) {
const activeKey = this.keyManager.currentKey();
try {
const results = await this.doSearch(activeKey, cleanQuery, options);
this.keyManager.markKeySuccess(activeKey);
return results;
} catch (err: unknown) {
if (err instanceof KeyAuthOrQuotaError) {
lastError = err as Error;
this.keyManager.markKeyExhausted(activeKey, (err as KeyAuthOrQuotaError).retryAfterSeconds);
if (attempt < totalKeys - 1) {
this.keyManager.nextKey();
}
} else {
throw err;
}
}
}
const msg = `All ${totalKeys} configured API keys for provider '${this.providerName}' failed authentication or quota limits. Last error: ${lastError?.message}`;
throw new ProviderError(msg, { originalError: lastError ?? undefined });
}
private async doSearch(
apiKey: string,
query: string,
options: SearchOptions
): Promise<ImageSearchResult[]> {
const numResults = options.numResults ?? 10;
const { apiKey: _k, numResults: _n, provider: _p, ...extraKwargs } = options;
const params: Record<string, string | number | boolean> = {
engine: "google_images",
q: query,
api_key: apiKey,
num: numResults,
...(extraKwargs as Record<string, string | number | boolean>),
};
let response;
try {
response = await this._httpClient.get(SERPAPI_SEARCH_URL, { params });
} catch (exc: unknown) {
const err = exc instanceof Error ? exc : new Error(String(exc));
throw new ProviderError(`SerpApi image search HTTP request failed: ${err.message}`, {
originalError: err,
});
}
const data = this.parseJsonResponse(response);
const rawResults = data.images_results;
if (!rawResults || !Array.isArray(rawResults)) {
return [];
}
return rawResults.slice(0, numResults).map((item) =>
this.normalizeResult((item as Record<string, unknown>) || {})
);
}
private parseJsonResponse(response: { statusCode: number; data?: unknown; text?: string }): Record<string, unknown> {
if (ROTATION_STATUS_CODES.includes(response.statusCode)) {
throw new KeyAuthOrQuotaError(
`SerpApi image search failed with status code ${response.statusCode}: ${response.text || ""}`,
response.statusCode
);
}
if (response.statusCode !== 200) {
throw new ProviderError(
`SerpApi image search failed with status code ${response.statusCode}: ${response.text || ""}`
);
}
if (!response.data || typeof response.data !== "object") {
throw new ProviderError("Invalid response payload from SerpApi image search API: expected JSON object");
}
const data = response.data as Record<string, unknown>;
if (data.error) {
const errMsg = String(data.error);
const errLower = errMsg.toLowerCase();
if (
errLower.includes("invalid api key") ||
errLower.includes("out of searches") ||
errLower.includes("quota") ||
errLower.includes("unauthorized") ||
errLower.includes("exhausted") ||
errLower.includes("api_key")
) {
throw new KeyAuthOrQuotaError(
`SerpApi API error: ${errMsg}`,
errLower.includes("key") ? 401 : 429
);
}
throw new ProviderError(`SerpApi API error: ${errMsg}`);
}
return data;
}
private normalizeResult(rawItem: Record<string, unknown>): ImageSearchResult {
const title = String(rawItem.title || "");
const imageUrl = String(rawItem.original || rawItem.image || "");
const sourceUrl = String(rawItem.link || rawItem.url || "");
const thumbnailUrl = typeof rawItem.thumbnail === "string" ? rawItem.thumbnail : undefined;
let width: number | undefined = undefined;
if (typeof rawItem.original_width === "number") width = rawItem.original_width;
else if (typeof rawItem.width === "number") width = rawItem.width;
let height: number | undefined = undefined;
if (typeof rawItem.original_height === "number") height = rawItem.original_height;
else if (typeof rawItem.height === "number") height = rawItem.height;
let sourceDomain: string | undefined = undefined;
if (rawItem.source) {
sourceDomain = String(rawItem.source).trim();
} else if (sourceUrl) {
try {
const parsed = new URL(sourceUrl);
sourceDomain = parsed.hostname;
} catch {
sourceDomain = undefined;
}
}
return {
title,
imageUrl,
sourceUrl,
sourceDomain,
width,
height,
thumbnailUrl,
raw: rawItem,
};
}
}