-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
233 lines (199 loc) · 5.84 KB
/
index.js
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
import { gotScraping as got } from "got-scraping";
import { JSDOM } from "jsdom";
import { HeaderGenerator } from "header-generator";
export const getHeaders = () => {
const header = new HeaderGenerator().getHeaders({
browsers: ["chrome"],
devices: ["desktop"],
operatingSystems: ["windows"],
});
return {
accept: "*/*",
"sec-ch-ua-mobile": header["sec-ch-ua-mobile"],
"sec-ch-ua-platform": header["sec-ch-ua-platform"],
"sec-ch-ua": header["sec-ch-ua"],
"user-agent": header["user-agent"],
};
};
export const browserInfo = async ({ options }) => {
const headers = options?.headers || getHeaders();
const response = await got("https://api.apify.com/v2/browser-info", {
headers,
...options,
responseType: "json",
});
return response.body;
};
export const googleSearch = async ({ query, options }) => {
const start = performance.now();
const headers = options?.headers || getHeaders();
const sendHtml = options?.html || "false";
delete options?.html;
const response = await got({
url: "https://www.google.com/search",
searchParams: {
q: query,
num: 100,
},
headers,
...options,
responseType: "text",
});
if (sendHtml === "true") {
return {
code: 200,
status: "success",
message: "HTML response",
query,
body: response.body,
};
}
if (response.statusCode !== 200)
return {
code: response.statusCode,
status: "error",
message: "Captcha or too many requests.",
query,
body: response.body,
};
const dom = new JSDOM(response.body);
const document = dom.window.document;
const searchResults = dom.window.document.querySelectorAll(".g");
const results = [];
searchResults.forEach((result) => {
const title = result.querySelector("h3");
const url = result.querySelector("a");
const description = result.querySelector(".VwiC3b");
if (title && url && description) {
results.push({
title: title.textContent,
url: url.href,
description: description.textContent,
});
}
});
let snippet = {
title:
document.querySelector(".co8aDb")?.textContent ||
document.querySelector(".LC20lb")?.textContent,
description:
document.querySelector(".hgKElc")?.textContent.trim() ||
document
.querySelector(".i8Z77e")
?.innerHTML.split("</li>")
.filter((item) => item !== "")
.map((item) => item.slice(item.indexOf(">") + 1))
.join("\n") ||
document
.querySelector(".X5LH0c")
?.innerHTML.split("</li>")
.filter((item) => item !== "")
.map(
(item, index) => index + 1 + ". " + item.slice(item.indexOf(">") + 1)
)
.join("\n"),
};
if (!snippet.description) snippet = null;
return {
code: 200,
status: "success",
message: `Found ${results.length} results in ${formatTime(
(performance.now() - start) / 1000
)}`,
query,
data: { snippet, results },
};
};
export const amazonSearch = async ({ query, options }) => {
const start = performance.now();
const headers = options?.headers || getHeaders();
const sendHtml = options?.html || "false";
delete options?.html;
const response = await got({
url: "https://www.amazon.in/s",
searchParams: {
k: query,
rh: "p_85:10440599031,p_72:1318476031",
s: "review-rank",
},
headers,
...options,
responseType: "text",
});
if (sendHtml === "true") {
return {
code: 200,
status: "success",
message: "HTML response",
query,
body: response.body,
};
}
if (response.statusCode !== 200)
return {
code: response.statusCode,
status: "error",
message: "Captcha or too many requests.",
query,
body: response.body,
};
const dom = new JSDOM(response.body);
const searchResults = dom.window.document.querySelectorAll(".s-result-item");
let results = [];
searchResults.forEach((result) => {
const image = result.querySelector(".s-image")?.src;
const title = result.querySelector("h2")?.textContent;
let rating = result.querySelector(".a-icon-alt")?.textContent.split(" ")[0];
let reviews = result.querySelector(
".a-size-base.s-underline-text"
)?.textContent;
let price = result.querySelector(".a-price-whole")?.textContent;
if (title && rating && reviews && price) {
rating = Number(rating);
reviews = Number(reviews.replaceAll(",", ""));
price = Number(price.replaceAll(",", ""));
let factor = 0;
let gmt = rating / reviews;
if (rating === 4.9) factor = 10;
else if (rating === 4.8) factor = 20;
else if (rating === 4.7) factor = 30;
else if (rating === 4.6) factor = 40;
else if (rating === 4.5) factor = 50;
else if (rating === 4.4) factor = 60;
else if (rating === 4.3) factor = 70;
else if (rating === 4.2) factor = 80;
else if (rating === 4.1) factor = 90;
else if (rating === 4.0) factor = 100;
factor *= 10;
gmt = gmt + (gmt * factor) / 100;
results.push({
image,
title,
rating,
reviews,
price,
gmt,
});
}
});
results = results
.sort((a, b) => a.gmt - b.gmt)
.filter((item) => typeof item.gmt === "number");
return {
code: 200,
status: "success",
message: `Found ${results.length} results in ${formatTime(
(performance.now() - start) / 1000
)}`,
query,
data: { results },
};
};
const formatTime = (time) => {
const hours = Math.floor(time / 3600);
const minutes = Math.floor((time - hours * 3600) / 60);
const seconds = Math.floor(time - hours * 3600 - minutes * 60);
if (hours === 0 && minutes === 0) return `${seconds}s`;
if (hours === 0) return `${minutes}m ${seconds}s`;
return `${hours}h ${minutes}m ${seconds}s`;
};