-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
590 lines (533 loc) · 17.9 KB
/
index.js
File metadata and controls
590 lines (533 loc) · 17.9 KB
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
const { Buffer } = require('buffer');
const https = require('https');
const querystring = require('querystring');
const { toArray, removeEmpty, formatQueries } = require('./utils');
class Outscraper {
constructor(apiKey) {
this.apiKey = apiKey;
this.apiHostname = 'api.app.outscraper.com';
}
getAPIRequest(path, parameters) {
return new Promise((resolve, reject) => {
const req = https.request({
hostname: this.apiHostname,
port: '443',
path: path + '?' + querystring.stringify(removeEmpty(parameters)),
headers: { 'X-API-KEY': this.apiKey, 'client': 'Node SDK' }
}, (res) => {
res.setEncoding('utf8');
let responseBody = '';
res.on('data', (chunk) => {
responseBody += chunk;
});
res.on('end', () => {
resolve(JSON.parse(responseBody));
});
});
req.on('error', (err) => {
reject(err);
console.log('err', err);
});
req.end();
});
}
postAPIRequest(path, parameters) {
return new Promise((resolve, reject) => {
const payload = JSON.stringify(removeEmpty(parameters || {}));
const req = https.request({
hostname: this.apiHostname,
port: '443',
path,
method: 'POST',
headers: {
'X-API-KEY': this.apiKey,
'client': 'Node SDK',
'content-type': 'application/json',
'content-length': Buffer.byteLength(payload),
}
}, (res) => {
res.setEncoding('utf8');
let responseBody = '';
res.on('data', (chunk) => {
responseBody += chunk;
});
res.on('end', () => {
resolve(JSON.parse(responseBody));
});
});
req.on('error', (err) => reject(err));
req.write(payload);
req.end();
});
}
handleAsyncResponse(response, asyncRequest) {
if (!response) {
return { error: 'Empty response received', response };
}
if (response.error || response.errorMessage) {
return response;
}
if (!asyncRequest) {
return response.data ?? response;
}
if (response) {
return {
status: response.status ?? 'Pending',
id: response.id,
results_location: response.results_location
};
}
return { error: 'Invalid async response structure', response };
}
async getRequestsHistory(type = 'running') {
return await this.getAPIRequest('/requests', { type });
}
async getRequestArchive(requestId) {
return await this.getAPIRequest(`/requests/${requestId}`, {});
}
async googleSearch(query, pagesPerQuery = 1, uule = '', language = 'en', region = null, asyncRequest = false) {
const response = await this.getAPIRequest('/google-search-v3', {
query: toArray(query),
pagesPerQuery,
uule,
language,
region,
async: asyncRequest,
});
return this.handleAsyncResponse(response, asyncRequest);
}
async googleSearchNews(query, pagesPerQuery = 1, uule = '', tbs = '', language = 'en', region = null, asyncRequest = false) {
const response = await this.getAPIRequest('/google-search-news', {
query: toArray(query),
pagesPerQuery,
uule,
tbs,
language,
region,
async: asyncRequest,
});
return this.handleAsyncResponse(response, asyncRequest);
}
async googleMapsSearch(query, limit = 20, language = 'en', region = null, skip = 0, dropDuplicates = false, enrichment = null, asyncRequest = true) {
const response = await this.getAPIRequest('/maps/search-v2', {
query: toArray(query),
language,
region,
organizationsPerQueryLimit: limit,
skipPlaces: skip,
dropDuplicates,
enrichment: enrichment ? toArray(enrichment) : null,
async: asyncRequest,
});
return this.handleAsyncResponse(response, asyncRequest);
}
async googleMapsSearchV3(query, limit = 20, language = 'en', region = null, skip = 0, dropDuplicates = false, enrichment = null, asyncRequest = true) {
const response = await this.getAPIRequest('/maps/search-v3', {
query: toArray(query),
language,
region,
organizationsPerQueryLimit: limit,
skipPlaces: skip,
dropDuplicates,
enrichment: enrichment ? toArray(enrichment) : null,
async: asyncRequest,
});
return this.handleAsyncResponse(response, asyncRequest);
}
async googleMapsDirections(query, departureTime = null, finishTime = null, interval = null, travelMode = 'best', language = 'en', region = null, fields = null, asyncRequest = true) {
const response = await this.getAPIRequest('/maps/directions', {
query: query ? formatQueries(query) : null,
departure_time: departureTime,
finish_time: finishTime,
interval: interval,
travel_mode: travelMode,
language: language,
region: region,
async: asyncRequest,
fields: fields ? toArray(fields) : null,
});
return this.handleAsyncResponse(response, asyncRequest);
}
async googleMapsReviews(query, reviewsLimit = 100, reviewsQuery = null, limit = 1, sort = 'most_relevant', lastPaginationId = null, start = null, cutoff = null, cutoffRating = null, ignoreEmpty = false, source = 'google', language = 'en', region = null, fields = '', asyncRequest = false) {
const response = await this.getAPIRequest('/maps/reviews-v3', {
query: toArray(query),
reviewsLimit,
reviewsQuery,
limit,
sort,
lastPaginationId,
start,
cutoff,
cutoffRating,
ignoreEmpty,
source,
language,
region,
fields: fields,
async: asyncRequest,
});
return this.handleAsyncResponse(response, asyncRequest);
}
async getGoogleMapsPhotos(query, options = {}) {
const response = await this.getAPIRequest('/maps/photos-v3', {
query: toArray(query),
photosLimit: options.photosLimit || 100,
limit: options.limit || 1,
tag: options.tag || 'all',
language: options.language || 'en',
region: options.region || undefined,
fields: options.fields || undefined,
async: options.async !== undefined ? options.async : true,
ui: options.ui || false,
webhook: options.webhook || undefined,
});
return this.handleAsyncResponse(response, options.async);
}
async googlePlayReviews(query, reviewsLimit = 100, sort = 'most_relevant', cutoff = null, rating = null, language = 'en', fields = null, asyncRequest = false) {
const response = await this.getAPIRequest('/google-play/reviews', {
query: toArray(query),
limit: reviewsLimit,
sort: sort,
cutoff: cutoff,
rating: rating,
language: language,
async: asyncRequest,
fields: fields ? toArray(fields) : null,
});
return this.handleAsyncResponse(response, asyncRequest);
}
async contactsAndLeads(
query,
fields = null,
asyncRequest = true,
preferredContacts = null,
contactsPerCompany = 3,
emailsPerContact = 1,
skipContacts = 0,
generalEmails = false,
ui = false,
webhook = null
) {
const response = await this.getAPIRequest('/contacts-and-leads', {
query: toArray(query),
fields: fields ? toArray(fields) : null,
async: asyncRequest,
preferred_contacts: preferredContacts ? toArray(preferredContacts) : null,
contacts_per_company: contactsPerCompany,
emails_per_contact: emailsPerContact,
skip_contacts: skipContacts,
general_emails: generalEmails,
ui,
webhook,
});
return this.handleAsyncResponse(response, asyncRequest);
}
async emailsAndContacts(query, preferredContacts = null, asyncRequest = false) {
const response = await this.getAPIRequest('/emails-and-contacts', {
query: toArray(query),
preferredContacts: preferredContacts ? toArray(preferredContacts) : null,
async: asyncRequest,
});
return this.handleAsyncResponse(response, asyncRequest);
}
async phonesEnricher(query, asyncRequest = false) {
const response = await this.getAPIRequest('/phones-enricher', {
query: toArray(query),
async: asyncRequest,
});
return this.handleAsyncResponse(response, asyncRequest);
}
async amazonProducts(query, limit = 24, domain = 'amazon.com', postalCode = '11201', fields = null, asyncRequest = false) {
const response = await this.getAPIRequest('/amazon/products-v2', {
query: toArray(query),
limit: limit,
domain: domain,
postal_code: postalCode,
async: asyncRequest,
fields: fields ? toArray(fields) : null,
});
return this.handleAsyncResponse(response, asyncRequest);
}
async amazonReviews(query, limit = 10, sort = 'helpful', filterByReviewer = 'all_reviews', filterByStar = 'all_stars', domain = null, fields = null, asyncRequest = false) {
const response = await this.getAPIRequest('/amazon/reviews', {
query: toArray(query),
limit: limit,
sort: sort,
filterByReviewer: filterByReviewer,
filterByStar: filterByStar,
domain: domain,
async: asyncRequest,
fields: fields ? toArray(fields) : null,
});
return this.handleAsyncResponse(response, asyncRequest);
}
async yelpSearch(query, limit = 100, asyncRequest = false) {
const response = await this.getAPIRequest('/yelp-search', {
query: toArray(query),
limit: limit,
async: asyncRequest,
});
return this.handleAsyncResponse(response, asyncRequest);
}
async yelpReviews(query, limit = 100, cursor = '', sort = 'relevance_desc', cutoff = '', fields = '', asyncRequest = false) {
const response = await this.getAPIRequest('/yelp/reviews', {
query: toArray(query),
limit,
cursor,
sort,
cutoff,
fields,
async: asyncRequest,
});
return this.handleAsyncResponse(response, asyncRequest);
}
async tripadvisorReviews(query, limit = 100, asyncRequest = false) {
const response = await this.getAPIRequest('/tripadvisor-reviews', {
query: toArray(query),
limit: limit,
async: asyncRequest,
});
return this.handleAsyncResponse(response, asyncRequest);
}
async appStoreReviews(query, limit = 100, sort = 'mosthelpful', cutoff = null, fields = '', asyncRequest = false) {
const response = await this.getAPIRequest('/appstore/reviews', {
query: toArray(query),
limit,
sort,
cutoff,
fields,
async: asyncRequest,
});
return this.handleAsyncResponse(response, asyncRequest);
}
async youtubeComments(query, perQuery = 100, language = 'en', region = '', fields = '', asyncRequest = false) {
const response = await this.getAPIRequest('/youtube-comments', {
query: toArray(query),
perQuery,
language,
region,
fields,
async: asyncRequest,
});
return this.handleAsyncResponse(response, asyncRequest);
}
async g2Reviews(query, limit = 100, sort = '', cutoff = null, fields = null, asyncRequest = false) {
const response = await this.getAPIRequest('/g2/reviews', {
query: toArray(query),
limit: limit,
sort: sort,
cutoff: cutoff,
async: asyncRequest,
fields: fields ? toArray(fields) : null,
});
return this.handleAsyncResponse(response, asyncRequest);
}
async trustpilotReviews(query, limit = 100, languages = 'default', sort = '', cutoff = null, fields = '', asyncRequest = false) {
const response = await this.getAPIRequest('/trustpilot/reviews', {
query: toArray(query),
limit,
languages,
sort,
cutoff,
fields,
async: asyncRequest,
});
return this.handleAsyncResponse(response, asyncRequest);
}
async getGlassdoorReviews(query, limit = 100, sort = 'DATE', cutoff = null, asyncRequest = false) {
const response = await this.getAPIRequest('/glassdoor/reviews', {
query: toArray(query),
limit: limit,
sort: sort,
cutoff: cutoff,
async: asyncRequest
});
return this.handleAsyncResponse(response, asyncRequest);
}
async capterraReviews(query, limit = 100, sort = '', cutoff = null, language = 'en', region = null, fields = null, asyncRequest = false) {
const response = await this.getAPIRequest('/capterra-reviews', {
query: toArray(query),
limit: limit,
sort: sort,
cutoff: cutoff,
language: language,
region: region,
async: asyncRequest,
fields: fields ? toArray(fields) : null,
});
return this.handleAsyncResponse(response, asyncRequest);
}
async geocoding(query, asyncRequest = false) {
const response = await this.getAPIRequest('/geocoding', {
query: Array.isArray(query) ? query : [query],
async: asyncRequest
});
return this.handleAsyncResponse(response, asyncRequest);
}
async reverseGeocoding(query, asyncRequest = false) {
const response = await this.getAPIRequest('/reverse-geocoding', {
query: Array.isArray(query) ? query : [query],
async: asyncRequest
});
return this.handleAsyncResponse(response, asyncRequest);
}
async phoneIdentityFinder(query, asyncRequest = false) {
const response = await this.getAPIRequest('/whitepages-phones', {
query: Array.isArray(query) ? query : [query],
async: asyncRequest
});
return this.handleAsyncResponse(response, asyncRequest);
}
async addressScraper(query, asyncRequest = false) {
const response = await this.getAPIRequest('/whitepages-addresses', {
query: Array.isArray(query) ? query : [query],
async: asyncRequest
});
return this.handleAsyncResponse(response, asyncRequest);
}
async companyInsights(query, fields = '', asyncRequest = false, enrichments = []) {
const response = await this.getAPIRequest('/company-insights', {
query: toArray(query),
fields,
enrichments: toArray(enrichments),
async: asyncRequest,
});
return this.handleAsyncResponse(response, asyncRequest);
}
async validateEmails(query, asyncRequest = false) {
const response = await this.getAPIRequest('/email-validator', {
query: toArray(query),
async: asyncRequest,
});
return this.handleAsyncResponse(response, asyncRequest);
}
async trustpilot(query, enrichment = [], fields = '', asyncRequest = false) {
const response = await this.getAPIRequest('/trustpilot', {
query: toArray(query),
enrichment: enrichment ? toArray(enrichment) : [],
fields,
async: asyncRequest,
});
return this.handleAsyncResponse(response, asyncRequest);
}
async trustpilotSearch(query, limit = 100, skip = 0, enrichment = [], fields = '', asyncRequest = false) {
const response = await this.getAPIRequest('/trustpilot', {
query: toArray(query),
limit,
skip,
enrichment: enrichment.length ? enrichment : [],
fields,
async: asyncRequest,
});
return this.handleAsyncResponse(response, asyncRequest);
}
async similarweb(query, fields = null, asyncRequest = false, ui = null, webhook = null) {
const response = await this.getAPIRequest('/similarweb', {
query: toArray(query),
fields: fields ? toArray(fields) : null,
async: asyncRequest,
ui: ui,
webhook: webhook,
});
return this.handleAsyncResponse(response, asyncRequest);
}
async companyWebsitesFinder(query, fields = null, asyncRequest = false, ui = null, webhook = null) {
const response = await this.getAPIRequest('/company-website-finder', {
query: toArray(query),
fields: fields ? toArray(fields) : null,
async: asyncRequest,
ui: ui,
webhook: webhook,
});
return this.handleAsyncResponse(response, asyncRequest);
}
async yellowpagesSearch(query, location = 'New York, NY', limit = 100, region = null, enrichment = null, fields = null, asyncRequest = true, ui = null, webhook = null) {
const response = await this.getAPIRequest('/yellowpages-search', {
query: toArray(query),
location: location,
limit: limit,
region: region,
enrichment: enrichment ? toArray(enrichment) : null,
fields: fields ? toArray(fields) : null,
async: asyncRequest,
ui: ui,
webhook: webhook,
});
return this.handleAsyncResponse(response, asyncRequest);
}
async businessesSearch(
filters = {},
limit = 10,
includeTotal = false,
cursor = null,
fields = null,
asyncRequest = false,
ui = false,
webhook = null,
query = null,
enrichments = null
) {
const payload = {
filters: filters || {},
limit,
include_total: includeTotal,
cursor,
fields: fields ? toArray(fields) : null,
query,
enrichments,
async: asyncRequest,
ui,
webhook,
};
const response = await this.postAPIRequest('/businesses', payload);
return this.handleAsyncResponse(response, asyncRequest);
}
async *businessesIterSearch(filters = {}, limit = 10, fields = null, includeTotal = false, query = null, enrichments = null) {
let cursor = null;
while (true) {
const response = await this.businessesSearch(
filters,
limit,
includeTotal,
cursor,
fields,
false,
false,
null,
query,
enrichments
);
const items = Array.isArray(response.items) ? response.items : [];
for (const item of items) {
yield item;
}
if (!response['has_more'] || !response['next_cursor'] || items.length === 0) {
break;
}
cursor = response['next_cursor'];
}
}
async businessesGet(
businessId,
fields = null,
asyncRequest = false,
ui = false,
webhook = null
) {
if (!businessId) {
throw new Error('businessId is required');
}
const params = {
fields: Array.isArray(fields) ? fields.join(',') : fields,
async: asyncRequest,
ui,
webhook,
};
const response = await this.getAPIRequest(
`/businesses/${encodeURIComponent(String(businessId))}`,
params
);
return this.handleAsyncResponse(response, asyncRequest);
}
}
module.exports = Outscraper;