@shaivpidadi/trends-js 0.0.0-beta.6 → 0.0.0-beta.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +183 -38
- package/dist/cjs/constants.d.ts +3 -0
- package/{lib → dist/cjs}/constants.js +19 -4
- package/{lib → dist/cjs}/helpers/googleTrendsAPI.d.ts +4 -1
- package/dist/cjs/helpers/googleTrendsAPI.js +339 -0
- package/{lib → dist/cjs}/helpers/request.d.ts +1 -1
- package/{lib → dist/cjs}/helpers/request.js +14 -18
- package/dist/cjs/index.d.ts +19 -0
- package/dist/cjs/index.js +24 -0
- package/dist/cjs/types/enums.d.ts +14 -0
- package/dist/cjs/types/enums.js +19 -0
- package/dist/esm/constants.d.ts +3 -0
- package/dist/esm/constants.js +50 -0
- package/dist/esm/errors/GoogleTrendsError.d.ts +23 -0
- package/dist/esm/errors/GoogleTrendsError.js +37 -0
- package/dist/esm/helpers/format.d.ts +2 -0
- package/dist/esm/helpers/format.js +76 -0
- package/dist/esm/helpers/googleTrendsAPI.d.ts +29 -0
- package/dist/esm/helpers/googleTrendsAPI.js +335 -0
- package/dist/esm/helpers/request.d.ts +9 -0
- package/dist/esm/helpers/request.js +75 -0
- package/dist/esm/index.d.ts +19 -0
- package/dist/esm/index.js +21 -0
- package/dist/esm/types/enums.d.ts +14 -0
- package/dist/esm/types/enums.js +16 -0
- package/package.json +29 -35
- package/lib/constants.d.ts +0 -2
- package/lib/helpers/googleTrendsAPI.js +0 -211
- package/lib/index.d.ts +0 -2
- package/lib/index.js +0 -7
- /package/{lib → dist/cjs}/errors/GoogleTrendsError.d.ts +0 -0
- /package/{lib → dist/cjs}/errors/GoogleTrendsError.js +0 -0
- /package/{lib → dist/cjs}/helpers/format.d.ts +0 -0
- /package/{lib → dist/cjs}/helpers/format.js +0 -0
|
@@ -0,0 +1,339 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.GoogleTrendsApi = void 0;
|
|
4
|
+
const enums_1 = require("../types/enums");
|
|
5
|
+
const request_1 = require("./request");
|
|
6
|
+
const format_1 = require("./format");
|
|
7
|
+
const constants_1 = require("../constants");
|
|
8
|
+
const GoogleTrendsError_1 = require("../errors/GoogleTrendsError");
|
|
9
|
+
class GoogleTrendsApi {
|
|
10
|
+
/**
|
|
11
|
+
* Get autocomplete suggestions for a keyword
|
|
12
|
+
* @param keyword - The keyword to get suggestions for
|
|
13
|
+
* @param hl - Language code (default: 'en-US')
|
|
14
|
+
* @returns Promise with array of suggestion strings
|
|
15
|
+
*/
|
|
16
|
+
async autocomplete(keyword, hl = 'en-US') {
|
|
17
|
+
if (!keyword) {
|
|
18
|
+
return { data: [] };
|
|
19
|
+
}
|
|
20
|
+
const options = {
|
|
21
|
+
...constants_1.GOOGLE_TRENDS_MAPPER[enums_1.GoogleTrendsEndpoints.autocomplete],
|
|
22
|
+
qs: {
|
|
23
|
+
hl,
|
|
24
|
+
tz: '240',
|
|
25
|
+
},
|
|
26
|
+
};
|
|
27
|
+
try {
|
|
28
|
+
const response = await (0, request_1.request)(`${options.url}/${encodeURIComponent(keyword)}`, options);
|
|
29
|
+
const text = await response.text();
|
|
30
|
+
// Remove the first 5 characters (JSONP wrapper) and parse
|
|
31
|
+
const data = JSON.parse(text.slice(5));
|
|
32
|
+
return { data: data.default.topics.map((topic) => topic.title) };
|
|
33
|
+
}
|
|
34
|
+
catch (error) {
|
|
35
|
+
if (error instanceof Error) {
|
|
36
|
+
return { error: new GoogleTrendsError_1.NetworkError(error.message) };
|
|
37
|
+
}
|
|
38
|
+
return { error: new GoogleTrendsError_1.UnknownError() };
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Get daily trending topics
|
|
43
|
+
* @param options - Options for daily trends request
|
|
44
|
+
* @returns Promise with trending topics data
|
|
45
|
+
*/
|
|
46
|
+
async dailyTrends({ geo = 'US', lang = 'en' }) {
|
|
47
|
+
const defaultOptions = constants_1.GOOGLE_TRENDS_MAPPER[enums_1.GoogleTrendsEndpoints.dailyTrends];
|
|
48
|
+
const options = {
|
|
49
|
+
...defaultOptions,
|
|
50
|
+
body: new URLSearchParams({
|
|
51
|
+
'f.req': `[[["i0OFE","[null,null,\\"${geo}\\",0,\\"${lang}\\",24,1]",null,"generic"]]]`,
|
|
52
|
+
}).toString(),
|
|
53
|
+
contentType: 'form'
|
|
54
|
+
};
|
|
55
|
+
try {
|
|
56
|
+
const response = await (0, request_1.request)(options.url, options);
|
|
57
|
+
const text = await response.text();
|
|
58
|
+
const trendingTopics = (0, format_1.extractJsonFromResponse)(text);
|
|
59
|
+
if (!trendingTopics) {
|
|
60
|
+
return { error: new GoogleTrendsError_1.ParseError() };
|
|
61
|
+
}
|
|
62
|
+
return { data: trendingTopics };
|
|
63
|
+
}
|
|
64
|
+
catch (error) {
|
|
65
|
+
if (error instanceof Error) {
|
|
66
|
+
return { error: new GoogleTrendsError_1.NetworkError(error.message) };
|
|
67
|
+
}
|
|
68
|
+
return { error: new GoogleTrendsError_1.UnknownError() };
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Get real-time trending topics
|
|
73
|
+
* @param options - Options for real-time trends request
|
|
74
|
+
* @returns Promise with trending topics data
|
|
75
|
+
*/
|
|
76
|
+
async realTimeTrends({ geo = 'US', trendingHours = 4 }) {
|
|
77
|
+
const defaultOptions = constants_1.GOOGLE_TRENDS_MAPPER[enums_1.GoogleTrendsEndpoints.dailyTrends];
|
|
78
|
+
const options = {
|
|
79
|
+
...defaultOptions,
|
|
80
|
+
body: new URLSearchParams({
|
|
81
|
+
'f.req': `[[["i0OFE","[null,null,\\"${geo}\\",0,\\"en\\",${trendingHours},1]",null,"generic"]]]`,
|
|
82
|
+
}).toString(),
|
|
83
|
+
contentType: 'form'
|
|
84
|
+
};
|
|
85
|
+
try {
|
|
86
|
+
const response = await (0, request_1.request)(options.url, options);
|
|
87
|
+
const text = await response.text();
|
|
88
|
+
const trendingTopics = (0, format_1.extractJsonFromResponse)(text);
|
|
89
|
+
if (!trendingTopics) {
|
|
90
|
+
return { error: new GoogleTrendsError_1.ParseError() };
|
|
91
|
+
}
|
|
92
|
+
return { data: trendingTopics };
|
|
93
|
+
}
|
|
94
|
+
catch (error) {
|
|
95
|
+
if (error instanceof Error) {
|
|
96
|
+
return { error: new GoogleTrendsError_1.NetworkError(error.message) };
|
|
97
|
+
}
|
|
98
|
+
return { error: new GoogleTrendsError_1.UnknownError() };
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
async explore({ keyword, geo = 'US', time = 'now 1-d', category = 0, property = '', hl = 'en-US', }) {
|
|
102
|
+
const options = {
|
|
103
|
+
...constants_1.GOOGLE_TRENDS_MAPPER[enums_1.GoogleTrendsEndpoints.explore],
|
|
104
|
+
qs: {
|
|
105
|
+
hl,
|
|
106
|
+
tz: '240',
|
|
107
|
+
req: JSON.stringify({
|
|
108
|
+
comparisonItem: [
|
|
109
|
+
{
|
|
110
|
+
keyword,
|
|
111
|
+
geo,
|
|
112
|
+
time,
|
|
113
|
+
},
|
|
114
|
+
],
|
|
115
|
+
category,
|
|
116
|
+
property,
|
|
117
|
+
}),
|
|
118
|
+
},
|
|
119
|
+
contentType: 'form'
|
|
120
|
+
};
|
|
121
|
+
try {
|
|
122
|
+
const response = await (0, request_1.request)(options.url, options);
|
|
123
|
+
const text = await response.text();
|
|
124
|
+
// Check if response is HTML (error page)
|
|
125
|
+
if (text.includes('<html') || text.includes('<!DOCTYPE')) {
|
|
126
|
+
console.error('Explore request returned HTML instead of JSON');
|
|
127
|
+
return { widgets: [] };
|
|
128
|
+
}
|
|
129
|
+
// Try to parse as JSON
|
|
130
|
+
try {
|
|
131
|
+
// Remove the first 5 characters (JSONP wrapper) and parse
|
|
132
|
+
const data = JSON.parse(text.slice(5));
|
|
133
|
+
return data;
|
|
134
|
+
}
|
|
135
|
+
catch (parseError) {
|
|
136
|
+
console.error('Failed to parse explore response as JSON:', parseError instanceof Error ? parseError.message : 'Unknown parse error');
|
|
137
|
+
console.error('Response preview:', text.substring(0, 200));
|
|
138
|
+
return { widgets: [] };
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
catch (error) {
|
|
142
|
+
console.error('Explore request failed:', error);
|
|
143
|
+
return { widgets: [] };
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
//
|
|
147
|
+
async interestByRegion({ keyword, startTime = new Date('2004-01-01'), endTime = new Date(), geo = 'US', resolution = 'REGION', hl = 'en-US', timezone = new Date().getTimezoneOffset(), category = 0 }) {
|
|
148
|
+
const formatDate = (date) => {
|
|
149
|
+
return date.toISOString().split('T')[0];
|
|
150
|
+
};
|
|
151
|
+
const formatTrendsDate = (date) => {
|
|
152
|
+
const pad = (n) => n.toString().padStart(2, '0');
|
|
153
|
+
const yyyy = date.getFullYear();
|
|
154
|
+
const mm = pad(date.getMonth() + 1);
|
|
155
|
+
const dd = pad(date.getDate());
|
|
156
|
+
const hh = pad(date.getHours());
|
|
157
|
+
const min = pad(date.getMinutes());
|
|
158
|
+
const ss = pad(date.getSeconds());
|
|
159
|
+
return `${yyyy}-${mm}-${dd}T${hh}\\:${min}\\:${ss}`;
|
|
160
|
+
};
|
|
161
|
+
const getDateRangeParam = (date) => {
|
|
162
|
+
const yesterday = new Date(date);
|
|
163
|
+
yesterday.setDate(date.getDate() - 1);
|
|
164
|
+
const formattedStart = formatTrendsDate(yesterday);
|
|
165
|
+
const formattedEnd = formatTrendsDate(date);
|
|
166
|
+
return `${formattedStart} ${formattedEnd}`;
|
|
167
|
+
};
|
|
168
|
+
const exploreResponse = await this.explore({
|
|
169
|
+
keyword: Array.isArray(keyword) ? keyword[0] : keyword,
|
|
170
|
+
geo: Array.isArray(geo) ? geo[0] : geo,
|
|
171
|
+
time: `${getDateRangeParam(startTime)} ${getDateRangeParam(endTime)}`,
|
|
172
|
+
category,
|
|
173
|
+
hl
|
|
174
|
+
});
|
|
175
|
+
const widget = exploreResponse.widgets.find(w => w.id === 'GEO_MAP');
|
|
176
|
+
if (!widget) {
|
|
177
|
+
return { default: { geoMapData: [] } };
|
|
178
|
+
}
|
|
179
|
+
const options = {
|
|
180
|
+
...constants_1.GOOGLE_TRENDS_MAPPER[enums_1.GoogleTrendsEndpoints.interestByRegion],
|
|
181
|
+
qs: {
|
|
182
|
+
hl,
|
|
183
|
+
tz: timezone.toString(),
|
|
184
|
+
req: JSON.stringify({
|
|
185
|
+
geo: {
|
|
186
|
+
country: Array.isArray(geo) ? geo[0] : geo
|
|
187
|
+
},
|
|
188
|
+
comparisonItem: [{
|
|
189
|
+
time: `${formatDate(startTime)} ${formatDate(endTime)}`,
|
|
190
|
+
complexKeywordsRestriction: {
|
|
191
|
+
keyword: [{
|
|
192
|
+
type: 'BROAD', //'ENTITY',
|
|
193
|
+
value: Array.isArray(keyword) ? keyword[0] : keyword
|
|
194
|
+
}]
|
|
195
|
+
}
|
|
196
|
+
}],
|
|
197
|
+
resolution,
|
|
198
|
+
locale: hl,
|
|
199
|
+
requestOptions: {
|
|
200
|
+
property: '',
|
|
201
|
+
backend: 'CM', //'IZG',
|
|
202
|
+
category
|
|
203
|
+
},
|
|
204
|
+
userConfig: {
|
|
205
|
+
userType: 'USER_TYPE_LEGIT_USER'
|
|
206
|
+
}
|
|
207
|
+
}),
|
|
208
|
+
token: widget.token
|
|
209
|
+
}
|
|
210
|
+
};
|
|
211
|
+
try {
|
|
212
|
+
const response = await (0, request_1.request)(options.url, options);
|
|
213
|
+
const text = await response.text();
|
|
214
|
+
// Remove the first 5 characters (JSONP wrapper) and parse
|
|
215
|
+
const data = JSON.parse(text.slice(5));
|
|
216
|
+
return data;
|
|
217
|
+
}
|
|
218
|
+
catch (error) {
|
|
219
|
+
return { default: { geoMapData: [] } };
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
async relatedTopics({ keyword, geo = 'US', time = 'now 1-d', category = 0, property = '', hl = 'en-US', }) {
|
|
223
|
+
try {
|
|
224
|
+
// Validate keyword
|
|
225
|
+
if (!keyword || keyword.trim() === '') {
|
|
226
|
+
return { error: new GoogleTrendsError_1.ParseError() };
|
|
227
|
+
}
|
|
228
|
+
const autocompleteResult = await this.autocomplete(keyword, hl);
|
|
229
|
+
if (autocompleteResult.error) {
|
|
230
|
+
return { error: autocompleteResult.error };
|
|
231
|
+
}
|
|
232
|
+
const relatedTopics = autocompleteResult.data?.slice(0, 10).map((suggestion, index) => ({
|
|
233
|
+
topic: {
|
|
234
|
+
mid: `/m/${index}`,
|
|
235
|
+
title: suggestion,
|
|
236
|
+
type: 'Topic'
|
|
237
|
+
},
|
|
238
|
+
value: 100 - index * 10,
|
|
239
|
+
formattedValue: (100 - index * 10).toString(),
|
|
240
|
+
hasData: true,
|
|
241
|
+
link: `/trends/explore?q=${encodeURIComponent(suggestion)}&date=${time}&geo=${geo}`
|
|
242
|
+
})) || [];
|
|
243
|
+
return {
|
|
244
|
+
data: {
|
|
245
|
+
default: {
|
|
246
|
+
rankedList: [{
|
|
247
|
+
rankedKeyword: relatedTopics
|
|
248
|
+
}]
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
};
|
|
252
|
+
}
|
|
253
|
+
catch (error) {
|
|
254
|
+
if (error instanceof Error) {
|
|
255
|
+
return { error: new GoogleTrendsError_1.NetworkError(error.message) };
|
|
256
|
+
}
|
|
257
|
+
return { error: new GoogleTrendsError_1.UnknownError() };
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
async relatedQueries({ keyword, geo = 'US', time = 'now 1-d', category = 0, property = '', hl = 'en-US', }) {
|
|
261
|
+
try {
|
|
262
|
+
// Validate keyword
|
|
263
|
+
if (!keyword || keyword.trim() === '') {
|
|
264
|
+
return { error: new GoogleTrendsError_1.ParseError() };
|
|
265
|
+
}
|
|
266
|
+
const autocompleteResult = await this.autocomplete(keyword, hl);
|
|
267
|
+
if (autocompleteResult.error) {
|
|
268
|
+
return { error: autocompleteResult.error };
|
|
269
|
+
}
|
|
270
|
+
const relatedQueries = autocompleteResult.data?.slice(0, 10).map((suggestion, index) => ({
|
|
271
|
+
query: suggestion,
|
|
272
|
+
value: 100 - index * 10,
|
|
273
|
+
formattedValue: (100 - index * 10).toString(),
|
|
274
|
+
hasData: true,
|
|
275
|
+
link: `/trends/explore?q=${encodeURIComponent(suggestion)}&date=${time}&geo=${geo}`
|
|
276
|
+
})) || [];
|
|
277
|
+
return {
|
|
278
|
+
data: {
|
|
279
|
+
default: {
|
|
280
|
+
rankedList: [{
|
|
281
|
+
rankedKeyword: relatedQueries
|
|
282
|
+
}]
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
};
|
|
286
|
+
}
|
|
287
|
+
catch (error) {
|
|
288
|
+
if (error instanceof Error) {
|
|
289
|
+
return { error: new GoogleTrendsError_1.NetworkError(error.message) };
|
|
290
|
+
}
|
|
291
|
+
return { error: new GoogleTrendsError_1.UnknownError() };
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
async relatedData({ keyword, geo = 'US', time = 'now 1-d', category = 0, property = '', hl = 'en-US', }) {
|
|
295
|
+
try {
|
|
296
|
+
// Validate keyword
|
|
297
|
+
if (!keyword || keyword.trim() === '') {
|
|
298
|
+
return { error: new GoogleTrendsError_1.ParseError() };
|
|
299
|
+
}
|
|
300
|
+
const autocompleteResult = await this.autocomplete(keyword, hl);
|
|
301
|
+
if (autocompleteResult.error) {
|
|
302
|
+
return { error: autocompleteResult.error };
|
|
303
|
+
}
|
|
304
|
+
const suggestions = autocompleteResult.data?.slice(0, 10) || [];
|
|
305
|
+
const topics = suggestions.map((suggestion, index) => ({
|
|
306
|
+
topic: {
|
|
307
|
+
mid: `/m/${index}`,
|
|
308
|
+
title: suggestion,
|
|
309
|
+
type: 'Topic'
|
|
310
|
+
},
|
|
311
|
+
value: 100 - index * 10,
|
|
312
|
+
formattedValue: (100 - index * 10).toString(),
|
|
313
|
+
hasData: true,
|
|
314
|
+
link: `/trends/explore?q=${encodeURIComponent(suggestion)}&date=${time}&geo=${geo}`
|
|
315
|
+
}));
|
|
316
|
+
const queries = suggestions.map((suggestion, index) => ({
|
|
317
|
+
query: suggestion,
|
|
318
|
+
value: 100 - index * 10,
|
|
319
|
+
formattedValue: (100 - index * 10).toString(),
|
|
320
|
+
hasData: true,
|
|
321
|
+
link: `/trends/explore?q=${encodeURIComponent(suggestion)}&date=${time}&geo=${geo}`
|
|
322
|
+
}));
|
|
323
|
+
return {
|
|
324
|
+
data: {
|
|
325
|
+
topics,
|
|
326
|
+
queries
|
|
327
|
+
}
|
|
328
|
+
};
|
|
329
|
+
}
|
|
330
|
+
catch (error) {
|
|
331
|
+
if (error instanceof Error) {
|
|
332
|
+
return { error: new GoogleTrendsError_1.NetworkError(error.message) };
|
|
333
|
+
}
|
|
334
|
+
return { error: new GoogleTrendsError_1.UnknownError() };
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
exports.GoogleTrendsApi = GoogleTrendsApi;
|
|
339
|
+
exports.default = new GoogleTrendsApi();
|
|
@@ -3,7 +3,7 @@ export declare const request: (url: string, options: {
|
|
|
3
3
|
qs?: Record<string, any>;
|
|
4
4
|
body?: string | Record<string, any>;
|
|
5
5
|
headers?: Record<string, string>;
|
|
6
|
-
contentType?:
|
|
6
|
+
contentType?: "json" | "form";
|
|
7
7
|
}) => Promise<{
|
|
8
8
|
text: () => Promise<string>;
|
|
9
9
|
}>;
|
|
@@ -1,13 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
4
|
};
|
|
@@ -29,7 +20,7 @@ function rereq(options, body) {
|
|
|
29
20
|
req.end();
|
|
30
21
|
});
|
|
31
22
|
}
|
|
32
|
-
const request = (url, options) =>
|
|
23
|
+
const request = async (url, options) => {
|
|
33
24
|
const parsedUrl = new URL(url);
|
|
34
25
|
const method = options.method || 'POST';
|
|
35
26
|
// Prepare body
|
|
@@ -49,20 +40,25 @@ const request = (url, options) => __awaiter(void 0, void 0, void 0, function* ()
|
|
|
49
40
|
port: parsedUrl.port || 443,
|
|
50
41
|
path: `${parsedUrl.pathname}${options.qs ? '?' + querystring_1.default.stringify(options.qs) : ''}`,
|
|
51
42
|
method,
|
|
52
|
-
headers:
|
|
53
|
-
|
|
54
|
-
|
|
43
|
+
headers: {
|
|
44
|
+
...(options.headers || {}),
|
|
45
|
+
...(contentType === 'form'
|
|
46
|
+
? { 'Content-Type': 'application/x-www-form-urlencoded' }
|
|
47
|
+
: { 'Content-Type': 'application/json' }),
|
|
48
|
+
...(bodyString ? { 'Content-Length': Buffer.byteLength(bodyString).toString() } : {}),
|
|
49
|
+
...(cookieVal ? { cookie: cookieVal } : {})
|
|
50
|
+
}
|
|
55
51
|
};
|
|
56
|
-
const response =
|
|
52
|
+
const response = await new Promise((resolve, reject) => {
|
|
57
53
|
const req = https_1.default.request(requestOptions, (res) => {
|
|
58
54
|
let chunk = '';
|
|
59
55
|
res.on('data', (data) => { chunk += data; });
|
|
60
|
-
res.on('end', () =>
|
|
56
|
+
res.on('end', async () => {
|
|
61
57
|
if (res.statusCode === 429 && res.headers['set-cookie']) {
|
|
62
58
|
cookieVal = res.headers['set-cookie'][0].split(';')[0];
|
|
63
59
|
requestOptions.headers['cookie'] = cookieVal;
|
|
64
60
|
try {
|
|
65
|
-
const retryResponse =
|
|
61
|
+
const retryResponse = await rereq(requestOptions, bodyString);
|
|
66
62
|
resolve(retryResponse);
|
|
67
63
|
}
|
|
68
64
|
catch (err) {
|
|
@@ -72,7 +68,7 @@ const request = (url, options) => __awaiter(void 0, void 0, void 0, function* ()
|
|
|
72
68
|
else {
|
|
73
69
|
resolve(chunk);
|
|
74
70
|
}
|
|
75
|
-
})
|
|
71
|
+
});
|
|
76
72
|
});
|
|
77
73
|
req.on('error', reject);
|
|
78
74
|
if (bodyString)
|
|
@@ -82,5 +78,5 @@ const request = (url, options) => __awaiter(void 0, void 0, void 0, function* ()
|
|
|
82
78
|
return {
|
|
83
79
|
text: () => Promise.resolve(response)
|
|
84
80
|
};
|
|
85
|
-
}
|
|
81
|
+
};
|
|
86
82
|
exports.request = request;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export declare const dailyTrends: ({ geo, lang }: import("./types").DailyTrendingTopicsOptions) => Promise<import("./types").GoogleTrendsResponse<import("./types").DailyTrendingTopics>>;
|
|
2
|
+
export declare const realTimeTrends: ({ geo, trendingHours }: import("./types").RealTimeTrendsOptions) => Promise<import("./types").GoogleTrendsResponse<import("./types").DailyTrendingTopics>>;
|
|
3
|
+
export declare const autocomplete: (keyword: string, hl?: string) => Promise<import("./types").GoogleTrendsResponse<string[]>>;
|
|
4
|
+
export declare const explore: ({ keyword, geo, time, category, property, hl, }: import("./types").ExploreOptions) => Promise<import("./types").ExploreResponse>;
|
|
5
|
+
export declare const interestByRegion: ({ keyword, startTime, endTime, geo, resolution, hl, timezone, category }: import("./types").InterestByRegionOptions) => Promise<import("./types").InterestByRegionResponse>;
|
|
6
|
+
export declare const relatedTopics: ({ keyword, geo, time, category, property, hl, }: import("./types").ExploreOptions) => Promise<import("./types").GoogleTrendsResponse<import("./types").RelatedTopicsResponse>>;
|
|
7
|
+
export declare const relatedQueries: ({ keyword, geo, time, category, property, hl, }: import("./types").ExploreOptions) => Promise<import("./types").GoogleTrendsResponse<import("./types").RelatedQueriesResponse>>;
|
|
8
|
+
export declare const relatedData: ({ keyword, geo, time, category, property, hl, }: import("./types").ExploreOptions) => Promise<import("./types").GoogleTrendsResponse<import("./types").RelatedData>>;
|
|
9
|
+
declare const _default: {
|
|
10
|
+
dailyTrends: ({ geo, lang }: import("./types").DailyTrendingTopicsOptions) => Promise<import("./types").GoogleTrendsResponse<import("./types").DailyTrendingTopics>>;
|
|
11
|
+
realTimeTrends: ({ geo, trendingHours }: import("./types").RealTimeTrendsOptions) => Promise<import("./types").GoogleTrendsResponse<import("./types").DailyTrendingTopics>>;
|
|
12
|
+
autocomplete: (keyword: string, hl?: string) => Promise<import("./types").GoogleTrendsResponse<string[]>>;
|
|
13
|
+
explore: ({ keyword, geo, time, category, property, hl, }: import("./types").ExploreOptions) => Promise<import("./types").ExploreResponse>;
|
|
14
|
+
interestByRegion: ({ keyword, startTime, endTime, geo, resolution, hl, timezone, category }: import("./types").InterestByRegionOptions) => Promise<import("./types").InterestByRegionResponse>;
|
|
15
|
+
relatedTopics: ({ keyword, geo, time, category, property, hl, }: import("./types").ExploreOptions) => Promise<import("./types").GoogleTrendsResponse<import("./types").RelatedTopicsResponse>>;
|
|
16
|
+
relatedQueries: ({ keyword, geo, time, category, property, hl, }: import("./types").ExploreOptions) => Promise<import("./types").GoogleTrendsResponse<import("./types").RelatedQueriesResponse>>;
|
|
17
|
+
relatedData: ({ keyword, geo, time, category, property, hl, }: import("./types").ExploreOptions) => Promise<import("./types").GoogleTrendsResponse<import("./types").RelatedData>>;
|
|
18
|
+
};
|
|
19
|
+
export default _default;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.relatedData = exports.relatedQueries = exports.relatedTopics = exports.interestByRegion = exports.explore = exports.autocomplete = exports.realTimeTrends = exports.dailyTrends = void 0;
|
|
4
|
+
const googleTrendsAPI_1 = require("./helpers/googleTrendsAPI");
|
|
5
|
+
const api = new googleTrendsAPI_1.GoogleTrendsApi();
|
|
6
|
+
exports.dailyTrends = api.dailyTrends.bind(api);
|
|
7
|
+
exports.realTimeTrends = api.realTimeTrends.bind(api);
|
|
8
|
+
exports.autocomplete = api.autocomplete.bind(api);
|
|
9
|
+
exports.explore = api.explore.bind(api);
|
|
10
|
+
exports.interestByRegion = api.interestByRegion.bind(api);
|
|
11
|
+
exports.relatedTopics = api.relatedTopics.bind(api);
|
|
12
|
+
exports.relatedQueries = api.relatedQueries.bind(api);
|
|
13
|
+
exports.relatedData = api.relatedData.bind(api);
|
|
14
|
+
// Default export for CommonJS compatibility
|
|
15
|
+
exports.default = {
|
|
16
|
+
dailyTrends: exports.dailyTrends,
|
|
17
|
+
realTimeTrends: exports.realTimeTrends,
|
|
18
|
+
autocomplete: exports.autocomplete,
|
|
19
|
+
explore: exports.explore,
|
|
20
|
+
interestByRegion: exports.interestByRegion,
|
|
21
|
+
relatedTopics: exports.relatedTopics,
|
|
22
|
+
relatedQueries: exports.relatedQueries,
|
|
23
|
+
relatedData: exports.relatedData
|
|
24
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export declare enum GoogleTrendsEndpoints {
|
|
2
|
+
dailyTrends = "dailyTrends",
|
|
3
|
+
autocomplete = "autocomplete",
|
|
4
|
+
explore = "explore",
|
|
5
|
+
interestByRegion = "interestByRegion",
|
|
6
|
+
relatedTopics = "relatedTopics",
|
|
7
|
+
relatedQueries = "relatedQueries"
|
|
8
|
+
}
|
|
9
|
+
export declare enum GoogleTrendsTrendingHours {
|
|
10
|
+
fourHrs = 4,
|
|
11
|
+
oneDay = 24,
|
|
12
|
+
twoDays = 48,
|
|
13
|
+
sevenDays = 168
|
|
14
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.GoogleTrendsTrendingHours = exports.GoogleTrendsEndpoints = void 0;
|
|
4
|
+
var GoogleTrendsEndpoints;
|
|
5
|
+
(function (GoogleTrendsEndpoints) {
|
|
6
|
+
GoogleTrendsEndpoints["dailyTrends"] = "dailyTrends";
|
|
7
|
+
GoogleTrendsEndpoints["autocomplete"] = "autocomplete";
|
|
8
|
+
GoogleTrendsEndpoints["explore"] = "explore";
|
|
9
|
+
GoogleTrendsEndpoints["interestByRegion"] = "interestByRegion";
|
|
10
|
+
GoogleTrendsEndpoints["relatedTopics"] = "relatedTopics";
|
|
11
|
+
GoogleTrendsEndpoints["relatedQueries"] = "relatedQueries";
|
|
12
|
+
})(GoogleTrendsEndpoints || (exports.GoogleTrendsEndpoints = GoogleTrendsEndpoints = {}));
|
|
13
|
+
var GoogleTrendsTrendingHours;
|
|
14
|
+
(function (GoogleTrendsTrendingHours) {
|
|
15
|
+
GoogleTrendsTrendingHours[GoogleTrendsTrendingHours["fourHrs"] = 4] = "fourHrs";
|
|
16
|
+
GoogleTrendsTrendingHours[GoogleTrendsTrendingHours["oneDay"] = 24] = "oneDay";
|
|
17
|
+
GoogleTrendsTrendingHours[GoogleTrendsTrendingHours["twoDays"] = 48] = "twoDays";
|
|
18
|
+
GoogleTrendsTrendingHours[GoogleTrendsTrendingHours["sevenDays"] = 168] = "sevenDays";
|
|
19
|
+
})(GoogleTrendsTrendingHours || (exports.GoogleTrendsTrendingHours = GoogleTrendsTrendingHours = {}));
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { GoogleTrendsEndpoints } from './types/enums';
|
|
2
|
+
const GOOGLE_TRENDS_BASE_URL = 'trends.google.com';
|
|
3
|
+
export const GOOGLE_TRENDS_MAPPER = {
|
|
4
|
+
[GoogleTrendsEndpoints.dailyTrends]: {
|
|
5
|
+
path: '/_/TrendsUi/data/batchexecute',
|
|
6
|
+
method: 'POST',
|
|
7
|
+
host: GOOGLE_TRENDS_BASE_URL,
|
|
8
|
+
url: `https://${GOOGLE_TRENDS_BASE_URL}/_/TrendsUi/data/batchexecute`,
|
|
9
|
+
headers: {
|
|
10
|
+
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
|
|
11
|
+
},
|
|
12
|
+
},
|
|
13
|
+
[GoogleTrendsEndpoints.autocomplete]: {
|
|
14
|
+
path: '/trends/api/autocomplete',
|
|
15
|
+
method: 'GET',
|
|
16
|
+
host: GOOGLE_TRENDS_BASE_URL,
|
|
17
|
+
url: `https://${GOOGLE_TRENDS_BASE_URL}/trends/api/autocomplete`,
|
|
18
|
+
headers: {
|
|
19
|
+
accept: 'application/json, text/plain, */*',
|
|
20
|
+
},
|
|
21
|
+
},
|
|
22
|
+
[GoogleTrendsEndpoints.explore]: {
|
|
23
|
+
path: '/trends/api/explore',
|
|
24
|
+
method: 'POST',
|
|
25
|
+
host: GOOGLE_TRENDS_BASE_URL,
|
|
26
|
+
url: `https://${GOOGLE_TRENDS_BASE_URL}/trends/api/explore`,
|
|
27
|
+
headers: {},
|
|
28
|
+
},
|
|
29
|
+
[GoogleTrendsEndpoints.interestByRegion]: {
|
|
30
|
+
path: '/trends/api/widgetdata/comparedgeo',
|
|
31
|
+
method: 'GET',
|
|
32
|
+
host: GOOGLE_TRENDS_BASE_URL,
|
|
33
|
+
url: `https://${GOOGLE_TRENDS_BASE_URL}/trends/api/widgetdata/comparedgeo`,
|
|
34
|
+
headers: {},
|
|
35
|
+
},
|
|
36
|
+
[GoogleTrendsEndpoints.relatedTopics]: {
|
|
37
|
+
path: '/trends/api/widgetdata/relatedtopics',
|
|
38
|
+
method: 'GET',
|
|
39
|
+
host: GOOGLE_TRENDS_BASE_URL,
|
|
40
|
+
url: `https://${GOOGLE_TRENDS_BASE_URL}/trends/api/widgetdata/relatedtopics`,
|
|
41
|
+
headers: {},
|
|
42
|
+
},
|
|
43
|
+
[GoogleTrendsEndpoints.relatedQueries]: {
|
|
44
|
+
path: '/trends/api/widgetdata/relatedqueries',
|
|
45
|
+
method: 'GET',
|
|
46
|
+
host: GOOGLE_TRENDS_BASE_URL,
|
|
47
|
+
url: `https://${GOOGLE_TRENDS_BASE_URL}/trends/api/widgetdata/relatedqueries`,
|
|
48
|
+
headers: {},
|
|
49
|
+
},
|
|
50
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { GoogleTrendsError } from '../types';
|
|
2
|
+
export declare class RateLimitError extends Error implements GoogleTrendsError {
|
|
3
|
+
code: string;
|
|
4
|
+
statusCode: number;
|
|
5
|
+
constructor(message?: string);
|
|
6
|
+
}
|
|
7
|
+
export declare class InvalidRequestError extends Error implements GoogleTrendsError {
|
|
8
|
+
code: string;
|
|
9
|
+
statusCode: number;
|
|
10
|
+
constructor(message?: string);
|
|
11
|
+
}
|
|
12
|
+
export declare class NetworkError extends Error implements GoogleTrendsError {
|
|
13
|
+
code: string;
|
|
14
|
+
constructor(message?: string);
|
|
15
|
+
}
|
|
16
|
+
export declare class ParseError extends Error implements GoogleTrendsError {
|
|
17
|
+
code: string;
|
|
18
|
+
constructor(message?: string);
|
|
19
|
+
}
|
|
20
|
+
export declare class UnknownError extends Error implements GoogleTrendsError {
|
|
21
|
+
code: string;
|
|
22
|
+
constructor(message?: string);
|
|
23
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
export class RateLimitError extends Error {
|
|
2
|
+
constructor(message = 'Rate limit exceeded') {
|
|
3
|
+
super(message);
|
|
4
|
+
this.code = 'RATE_LIMIT_EXCEEDED';
|
|
5
|
+
this.statusCode = 429;
|
|
6
|
+
this.name = 'RateLimitError';
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
export class InvalidRequestError extends Error {
|
|
10
|
+
constructor(message = 'Invalid request parameters') {
|
|
11
|
+
super(message);
|
|
12
|
+
this.code = 'INVALID_REQUEST';
|
|
13
|
+
this.statusCode = 400;
|
|
14
|
+
this.name = 'InvalidRequestError';
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
export class NetworkError extends Error {
|
|
18
|
+
constructor(message = 'Network request failed') {
|
|
19
|
+
super(message);
|
|
20
|
+
this.code = 'NETWORK_ERROR';
|
|
21
|
+
this.name = 'NetworkError';
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
export class ParseError extends Error {
|
|
25
|
+
constructor(message = 'Failed to parse response') {
|
|
26
|
+
super(message);
|
|
27
|
+
this.code = 'PARSE_ERROR';
|
|
28
|
+
this.name = 'ParseError';
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
export class UnknownError extends Error {
|
|
32
|
+
constructor(message = 'An unknown error occurred') {
|
|
33
|
+
super(message);
|
|
34
|
+
this.code = 'UNKNOWN_ERROR';
|
|
35
|
+
this.name = 'UnknownError';
|
|
36
|
+
}
|
|
37
|
+
}
|