-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
dataforseo_api_search.ts
378 lines (341 loc) Β· 10.8 KB
/
dataforseo_api_search.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
import { getEnvironmentVariable } from "@langchain/core/utils/env";
import { Tool } from "@langchain/core/tools";
/**
* @interface DataForSeoApiConfig
* @description Represents the configuration object used to set up a DataForSeoAPISearch instance.
*/
export interface DataForSeoApiConfig {
/**
* @property apiLogin
* @type {string}
* @description The API login credential for DataForSEO. If not provided, it will be fetched from environment variables.
*/
apiLogin?: string;
/**
* @property apiPassword
* @type {string}
* @description The API password credential for DataForSEO. If not provided, it will be fetched from environment variables.
*/
apiPassword?: string;
/**
* @property params
* @type {Record<string, string | number | boolean>}
* @description Additional parameters to customize the API request.
*/
params?: Record<string, string | number | boolean>;
/**
* @property useJsonOutput
* @type {boolean}
* @description Determines if the output should be in JSON format.
*/
useJsonOutput?: boolean;
/**
* @property jsonResultTypes
* @type {Array<string>}
* @description Specifies the types of results to include in the output.
*/
jsonResultTypes?: Array<string>;
/**
* @property jsonResultFields
* @type {Array<string>}
* @description Specifies the fields to include in each result object.
*/
jsonResultFields?: Array<string>;
/**
* @property topCount
* @type {number}
* @description Specifies the maximum number of results to return.
*/
topCount?: number;
}
/**
* Represents a task in the API response.
*/
type Task = {
id: string;
status_code: number;
status_message: string;
time: string;
result: Result[];
};
/**
* Represents a result in the API response.
*/
type Result = {
keyword: string;
check_url: string;
datetime: string;
spell?: string;
item_types: string[];
se_results_count: number;
items_count: number;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
items: any[];
};
/**
* Represents the API response.
*/
type ApiResponse = {
status_code: number;
status_message: string;
tasks: Task[];
};
/**
* @class DataForSeoAPISearch
* @extends {Tool}
* @description Represents a wrapper class to work with DataForSEO SERP API.
*/
export class DataForSeoAPISearch extends Tool {
static lc_name() {
return "DataForSeoAPISearch";
}
name = "dataforseo-api-wrapper";
description =
"A robust Google Search API provided by DataForSeo. This tool is handy when you need information about trending topics or current events.";
protected apiLogin: string;
protected apiPassword: string;
/**
* @property defaultParams
* @type {Record<string, string | number | boolean>}
* @description These are the default parameters to be used when making an API request.
*/
protected defaultParams: Record<string, string | number | boolean> = {
location_name: "United States",
language_code: "en",
depth: 10,
se_name: "google",
se_type: "organic",
};
protected params: Record<string, string | number | boolean> = {};
protected jsonResultTypes: Array<string> | undefined;
protected jsonResultFields: Array<string> | undefined;
protected topCount: number | undefined;
protected useJsonOutput = false;
/**
* @constructor
* @param {DataForSeoApiConfig} config
* @description Sets up the class, throws an error if the API login/password isn't provided.
*/
constructor(config: DataForSeoApiConfig = {}) {
super();
const apiLogin =
config.apiLogin ?? getEnvironmentVariable("DATAFORSEO_LOGIN");
const apiPassword =
config.apiPassword ?? getEnvironmentVariable("DATAFORSEO_PASSWORD");
const params = config.params ?? {};
if (!apiLogin || !apiPassword) {
throw new Error(
"DataForSEO login or password not set. You can set it as DATAFORSEO_LOGIN and DATAFORSEO_PASSWORD in your .env file, or pass it to DataForSeoAPISearch."
);
}
this.params = { ...this.defaultParams, ...params };
this.apiLogin = apiLogin;
this.apiPassword = apiPassword;
this.jsonResultTypes = config.jsonResultTypes;
this.jsonResultFields = config.jsonResultFields;
this.useJsonOutput = config.useJsonOutput ?? false;
this.topCount = config.topCount;
}
/**
* @method _call
* @param {string} keyword
* @returns {Promise<string>}
* @description Initiates a call to the API and processes the response.
*/
async _call(keyword: string): Promise<string> {
return this.useJsonOutput
? JSON.stringify(await this.results(keyword))
: this.processResponse(await this.getResponseJson(keyword));
}
/**
* @method results
* @param {string} keyword
* @returns {Promise<Array<any>>}
* @description Fetches the results from the API for the given keyword.
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
async results(keyword: string): Promise<Array<any>> {
const res = await this.getResponseJson(keyword);
return this.filterResults(res, this.jsonResultTypes);
}
/**
* @method prepareRequest
* @param {string} keyword
* @returns {{url: string; headers: HeadersInit; data: BodyInit}}
* @description Prepares the request details for the API call.
*/
protected prepareRequest(keyword: string): {
url: string;
headers: HeadersInit;
data: BodyInit;
} {
if (this.apiLogin === undefined || this.apiPassword === undefined) {
throw new Error("api_login or api_password is not provided");
}
const credentials = Buffer.from(
`${this.apiLogin}:${this.apiPassword}`,
"utf-8"
).toString("base64");
const headers = {
Authorization: `Basic ${credentials}`,
"Content-Type": "application/json",
};
const params = { ...this.params };
params.keyword ??= keyword;
const data = [params];
return {
url: `https://api.dataforseo.com/v3/serp/${params.se_name}/${params.se_type}/live/advanced`,
headers,
data: JSON.stringify(data),
};
}
/**
* @method getResponseJson
* @param {string} keyword
* @returns {Promise<ApiResponse>}
* @description Executes a POST request to the provided URL and returns a parsed JSON response.
*/
protected async getResponseJson(keyword: string): Promise<ApiResponse> {
const requestDetails = this.prepareRequest(keyword);
const response = await fetch(requestDetails.url, {
method: "POST",
headers: requestDetails.headers,
body: requestDetails.data,
});
if (!response.ok) {
throw new Error(
`Got ${response.status} error from DataForSEO: ${response.statusText}`
);
}
const result: ApiResponse = await response.json();
return this.checkResponse(result);
}
/**
* @method checkResponse
* @param {ApiResponse} response
* @returns {ApiResponse}
* @description Checks the response status code.
*/
private checkResponse(response: ApiResponse): ApiResponse {
if (response.status_code !== 20000) {
throw new Error(
`Got error from DataForSEO SERP API: ${response.status_message}`
);
}
for (const task of response.tasks) {
if (task.status_code !== 20000) {
throw new Error(
`Got error from DataForSEO SERP API: ${task.status_message}`
);
}
}
return response;
}
/* eslint-disable @typescript-eslint/no-explicit-any */
/**
* @method filterResults
* @param {ApiResponse} res
* @param {Array<string> | undefined} types
* @returns {Array<any>}
* @description Filters the results based on the specified result types.
*/
private filterResults(
res: ApiResponse,
types: Array<string> | undefined
): Array<any> {
const output: Array<any> = [];
for (const task of res.tasks || []) {
for (const result of task.result || []) {
for (const item of result.items || []) {
if (
types === undefined ||
types.length === 0 ||
types.includes(item.type)
) {
const newItem = this.cleanupUnnecessaryItems(item);
if (Object.keys(newItem).length !== 0) {
output.push(newItem);
}
}
if (this.topCount !== undefined && output.length >= this.topCount) {
break;
}
}
}
}
return output;
}
/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable no-param-reassign */
/**
* @method cleanupUnnecessaryItems
* @param {any} d
* @description Removes unnecessary items from the response.
*/
private cleanupUnnecessaryItems(d: any): any {
if (Array.isArray(d)) {
return d.map((item) => this.cleanupUnnecessaryItems(item));
}
const toRemove = ["xpath", "position", "rectangle"];
if (typeof d === "object" && d !== null) {
return Object.keys(d).reduce((newObj: any, key: string) => {
if (
(this.jsonResultFields === undefined ||
this.jsonResultFields.includes(key)) &&
!toRemove.includes(key)
) {
if (typeof d[key] === "object" && d[key] !== null) {
newObj[key] = this.cleanupUnnecessaryItems(d[key]);
} else {
newObj[key] = d[key];
}
}
return newObj;
}, {});
}
return d;
}
/**
* @method processResponse
* @param {ApiResponse} res
* @returns {string}
* @description Processes the response to extract meaningful data.
*/
protected processResponse(res: ApiResponse): string {
let returnValue = "No good search result found";
for (const task of res.tasks || []) {
for (const result of task.result || []) {
const { item_types } = result;
const items = result.items || [];
if (item_types.includes("answer_box")) {
returnValue = items.find(
(item: { type: string; text: string }) => item.type === "answer_box"
).text;
} else if (item_types.includes("knowledge_graph")) {
returnValue = items.find(
(item: { type: string; description: string }) =>
item.type === "knowledge_graph"
).description;
} else if (item_types.includes("featured_snippet")) {
returnValue = items.find(
(item: { type: string; description: string }) =>
item.type === "featured_snippet"
).description;
} else if (item_types.includes("shopping")) {
returnValue = items.find(
(item: { type: string; price: string }) => item.type === "shopping"
).price;
} else if (item_types.includes("organic")) {
returnValue = items.find(
(item: { type: string; description: string }) =>
item.type === "organic"
).description;
}
if (returnValue) {
break;
}
}
}
return returnValue;
}
}