@shaivpidadi/trends-js 1.0.1 → 1.0.3
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 +65 -106
- package/dist/cjs/constants.js +4 -4
- package/dist/cjs/helpers/format.d.ts +2 -0
- package/dist/cjs/helpers/format.js +16 -1
- package/dist/cjs/helpers/googleTrendsAPI.d.ts +5 -7
- package/dist/cjs/helpers/googleTrendsAPI.js +108 -132
- package/dist/cjs/helpers/request.d.ts +1 -1
- package/dist/cjs/helpers/request.js +51 -29
- package/dist/cjs/helpers/session-manager.d.ts +36 -0
- package/dist/cjs/helpers/session-manager.js +172 -0
- package/dist/cjs/index.d.ts +8 -12
- package/dist/cjs/utils/formatters.d.ts +4 -0
- package/dist/cjs/utils/formatters.js +26 -0
- package/dist/esm/constants.js +4 -4
- package/dist/esm/helpers/format.d.ts +2 -0
- package/dist/esm/helpers/format.js +13 -0
- package/dist/esm/helpers/googleTrendsAPI.d.ts +5 -7
- package/dist/esm/helpers/googleTrendsAPI.js +109 -133
- package/dist/esm/helpers/request.d.ts +1 -1
- package/dist/esm/helpers/request.js +51 -29
- package/dist/esm/helpers/session-manager.d.ts +36 -0
- package/dist/esm/helpers/session-manager.js +165 -0
- package/dist/esm/index.d.ts +8 -12
- package/dist/esm/utils/formatters.d.ts +4 -0
- package/dist/esm/utils/formatters.js +21 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -22,7 +22,6 @@ npm install @shaivpidadi/trends-js
|
|
|
22
22
|
- Get interest by region data
|
|
23
23
|
- Get related topics for any keyword
|
|
24
24
|
- Get related queries for any keyword
|
|
25
|
-
- Get combined related data (topics + queries)
|
|
26
25
|
- TypeScript support
|
|
27
26
|
- Promise-based API
|
|
28
27
|
|
|
@@ -113,8 +112,6 @@ const suggestions = await GoogleTrendsApi.autocomplete(
|
|
|
113
112
|
'bitcoin', // Keyword to get suggestions for
|
|
114
113
|
'en-US', // Language (default: 'en-US')
|
|
115
114
|
);
|
|
116
|
-
|
|
117
|
-
// Returns: string[]
|
|
118
115
|
```
|
|
119
116
|
|
|
120
117
|
### Explore
|
|
@@ -124,8 +121,8 @@ Get widget data for a keyword:
|
|
|
124
121
|
```typescript
|
|
125
122
|
const result = await GoogleTrendsApi.explore({
|
|
126
123
|
keyword: 'bitcoin',
|
|
127
|
-
geo: 'US'
|
|
128
|
-
time: 'today 12-m',
|
|
124
|
+
geo: 'US' // Default: 'US',
|
|
125
|
+
time: 'today 12-m',
|
|
129
126
|
category: 0, // Default: 0
|
|
130
127
|
property: '', // Default: ''
|
|
131
128
|
hl: 'en-US', // Default: 'en-US'
|
|
@@ -140,6 +137,7 @@ const result = await GoogleTrendsApi.explore({
|
|
|
140
137
|
// }>
|
|
141
138
|
// }
|
|
142
139
|
```
|
|
140
|
+
> **Note:** For all methods below, it is recommended to set `enableBackoff: true` in the options to automatically handle and bypass Google's rate limiting.
|
|
143
141
|
|
|
144
142
|
### Interest by Region
|
|
145
143
|
|
|
@@ -147,14 +145,15 @@ Get interest data by region:
|
|
|
147
145
|
|
|
148
146
|
```typescript
|
|
149
147
|
const result = await GoogleTrendsApi.interestByRegion({
|
|
150
|
-
keyword: 'Stock Market', // Required - string
|
|
148
|
+
keyword: 'Stock Market', // Required - string
|
|
151
149
|
startTime: new Date('2024-01-01'), // Optional - defaults to 2004-01-01
|
|
152
150
|
endTime: new Date(), // Optional - defaults to current date
|
|
153
|
-
geo: 'US', // Optional - string
|
|
151
|
+
geo: 'US', // Optional - string - defaults to 'US'
|
|
154
152
|
resolution: 'REGION', // Optional - 'COUNTRY' | 'REGION' | 'CITY' | 'DMA'
|
|
155
153
|
hl: 'en-US', // Optional - defaults to 'en-US'
|
|
156
154
|
timezone: -240, // Optional - defaults to local timezone
|
|
157
155
|
category: 0, // Optional - defaults to 0
|
|
156
|
+
enableBackoff: true // Optional - defaults to false
|
|
158
157
|
});
|
|
159
158
|
|
|
160
159
|
// Result structure:
|
|
@@ -176,30 +175,20 @@ const result = await GoogleTrendsApi.interestByRegion({
|
|
|
176
175
|
// }
|
|
177
176
|
```
|
|
178
177
|
|
|
179
|
-
Example with multiple keywords and regions:
|
|
180
|
-
|
|
181
|
-
```typescript
|
|
182
|
-
const result = await GoogleTrendsApi.interestByRegion({
|
|
183
|
-
keyword: ['wine', 'peanuts'],
|
|
184
|
-
geo: ['US-CA', 'US-VA'],
|
|
185
|
-
startTime: new Date('2024-01-01'),
|
|
186
|
-
endTime: new Date(),
|
|
187
|
-
resolution: 'CITY',
|
|
188
|
-
});
|
|
189
|
-
```
|
|
190
|
-
|
|
191
178
|
### Related Topics
|
|
192
179
|
|
|
193
180
|
Get related topics for any keyword:
|
|
194
181
|
|
|
195
182
|
```typescript
|
|
196
183
|
const result = await GoogleTrendsApi.relatedTopics({
|
|
197
|
-
keyword: 'artificial intelligence',
|
|
198
|
-
|
|
199
|
-
|
|
184
|
+
keyword: 'artificial intelligence',
|
|
185
|
+
startTime: new Date('2024-01-01'),
|
|
186
|
+
endTime: new Date(),
|
|
200
187
|
category: 0, // Optional - defaults to 0
|
|
188
|
+
geo: 'US', // Optional - defaults to 'US'
|
|
201
189
|
property: '', // Optional - defaults to ''
|
|
202
190
|
hl: 'en-US', // Optional - defaults to 'en-US'
|
|
191
|
+
enableBackoff: true // Optional - defaults to false
|
|
203
192
|
});
|
|
204
193
|
|
|
205
194
|
// Result structure:
|
|
@@ -230,12 +219,13 @@ Get related queries for any keyword:
|
|
|
230
219
|
|
|
231
220
|
```typescript
|
|
232
221
|
const result = await GoogleTrendsApi.relatedQueries({
|
|
233
|
-
keyword: 'machine learning',
|
|
234
|
-
geo: 'US',
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
hl: 'en-US',
|
|
222
|
+
keyword: 'machine learning',
|
|
223
|
+
geo: 'US',
|
|
224
|
+
startTime: new Date('2024-01-01'),
|
|
225
|
+
endTime: new Date(),
|
|
226
|
+
category: 0,
|
|
227
|
+
hl: 'en-US',
|
|
228
|
+
enableBackoff: true // Optional - defaults to false
|
|
239
229
|
});
|
|
240
230
|
|
|
241
231
|
// Result structure:
|
|
@@ -256,37 +246,14 @@ const result = await GoogleTrendsApi.relatedQueries({
|
|
|
256
246
|
// }
|
|
257
247
|
```
|
|
258
248
|
|
|
259
|
-
### Combined Related Data
|
|
260
|
-
|
|
261
|
-
Get both related topics and queries in a single call:
|
|
262
|
-
|
|
263
|
-
```typescript
|
|
264
|
-
const result = await GoogleTrendsApi.relatedData({
|
|
265
|
-
keyword: 'blockchain', // Required
|
|
266
|
-
geo: 'US', // Optional - defaults to 'US'
|
|
267
|
-
time: 'now 1-d', // Optional - defaults to 'now 1-d'
|
|
268
|
-
category: 0, // Optional - defaults to 0
|
|
269
|
-
property: '', // Optional - defaults to ''
|
|
270
|
-
hl: 'en-US', // Optional - defaults to 'en-US'
|
|
271
|
-
});
|
|
272
|
-
|
|
273
|
-
// Result structure:
|
|
274
|
-
// {
|
|
275
|
-
// data: {
|
|
276
|
-
// topics: Array<RelatedTopic>,
|
|
277
|
-
// queries: Array<RelatedQuery>
|
|
278
|
-
// }
|
|
279
|
-
// }
|
|
280
|
-
```
|
|
281
|
-
|
|
282
249
|
## API Reference
|
|
283
250
|
|
|
284
251
|
### DailyTrendsOptions
|
|
285
252
|
|
|
286
253
|
```typescript
|
|
287
254
|
interface DailyTrendsOptions {
|
|
288
|
-
geo?: string;
|
|
289
|
-
lang?: string;
|
|
255
|
+
geo?: string;
|
|
256
|
+
lang?: string;
|
|
290
257
|
}
|
|
291
258
|
```
|
|
292
259
|
|
|
@@ -295,7 +262,7 @@ interface DailyTrendsOptions {
|
|
|
295
262
|
```typescript
|
|
296
263
|
interface RealTimeTrendsOptions {
|
|
297
264
|
geo: string;
|
|
298
|
-
trendingHours?: number;
|
|
265
|
+
trendingHours?: number;
|
|
299
266
|
}
|
|
300
267
|
```
|
|
301
268
|
|
|
@@ -304,11 +271,11 @@ interface RealTimeTrendsOptions {
|
|
|
304
271
|
```typescript
|
|
305
272
|
interface ExploreOptions {
|
|
306
273
|
keyword: string;
|
|
307
|
-
geo?: string;
|
|
308
|
-
time?: string;
|
|
309
|
-
category?: number;
|
|
310
|
-
property?: string;
|
|
311
|
-
hl?: string;
|
|
274
|
+
geo?: string;
|
|
275
|
+
time?: string;
|
|
276
|
+
category?: number;
|
|
277
|
+
property?: string;
|
|
278
|
+
hl?: string;
|
|
312
279
|
}
|
|
313
280
|
```
|
|
314
281
|
|
|
@@ -316,63 +283,45 @@ interface ExploreOptions {
|
|
|
316
283
|
|
|
317
284
|
```typescript
|
|
318
285
|
interface InterestByRegionOptions {
|
|
319
|
-
keyword: string
|
|
320
|
-
startTime?: Date;
|
|
321
|
-
endTime?: Date;
|
|
322
|
-
geo?: string
|
|
323
|
-
resolution?: 'COUNTRY' | 'REGION' | 'CITY' | 'DMA';
|
|
324
|
-
hl?: string;
|
|
325
|
-
timezone?: number;
|
|
326
|
-
category?: number;
|
|
327
|
-
|
|
328
|
-
```
|
|
329
|
-
|
|
330
|
-
### RelatedTopicsResponse
|
|
331
|
-
|
|
332
|
-
```typescript
|
|
333
|
-
interface RelatedTopicsResponse {
|
|
334
|
-
default: {
|
|
335
|
-
rankedList: Array<{
|
|
336
|
-
rankedKeyword: Array<{
|
|
337
|
-
topic: {
|
|
338
|
-
mid: string;
|
|
339
|
-
title: string;
|
|
340
|
-
type: string;
|
|
341
|
-
};
|
|
342
|
-
value: number;
|
|
343
|
-
formattedValue: string;
|
|
344
|
-
hasData: boolean;
|
|
345
|
-
link: string;
|
|
346
|
-
}>;
|
|
347
|
-
}>;
|
|
348
|
-
};
|
|
286
|
+
keyword: string;
|
|
287
|
+
startTime?: Date;
|
|
288
|
+
endTime?: Date;
|
|
289
|
+
geo?: string;
|
|
290
|
+
resolution?: 'COUNTRY' | 'REGION' | 'CITY' | 'DMA';
|
|
291
|
+
hl?: string;
|
|
292
|
+
timezone?: number;
|
|
293
|
+
category?: number;
|
|
294
|
+
enableBackoff?: boolean;
|
|
349
295
|
}
|
|
350
296
|
```
|
|
351
297
|
|
|
352
|
-
###
|
|
298
|
+
### RelatedTopicsOptions
|
|
353
299
|
|
|
354
300
|
```typescript
|
|
355
|
-
interface
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
}>;
|
|
365
|
-
}>;
|
|
366
|
-
};
|
|
301
|
+
interface RelatedTopicsOptions {
|
|
302
|
+
keyword: string;
|
|
303
|
+
geo?: string;
|
|
304
|
+
startTime?: Date;
|
|
305
|
+
endTime?: Date;
|
|
306
|
+
category?: number;
|
|
307
|
+
property?: string;
|
|
308
|
+
hl?: string;
|
|
309
|
+
enableBackoff?: boolean;
|
|
367
310
|
}
|
|
368
311
|
```
|
|
369
312
|
|
|
370
|
-
###
|
|
313
|
+
### RelatedQueriesOptions
|
|
371
314
|
|
|
372
315
|
```typescript
|
|
373
|
-
interface
|
|
374
|
-
|
|
375
|
-
|
|
316
|
+
interface RelatedQueriesOptions {
|
|
317
|
+
keyword: string;
|
|
318
|
+
geo?: string;
|
|
319
|
+
startTime?: Date;
|
|
320
|
+
endTime?: Date;
|
|
321
|
+
category?: number;
|
|
322
|
+
property?: string;
|
|
323
|
+
hl?: string;
|
|
324
|
+
enableBackoff?: boolean;
|
|
376
325
|
}
|
|
377
326
|
```
|
|
378
327
|
|
|
@@ -380,6 +329,16 @@ interface RelatedData {
|
|
|
380
329
|
|
|
381
330
|
### Building
|
|
382
331
|
|
|
332
|
+
```bash
|
|
333
|
+
npm run build
|
|
383
334
|
```
|
|
384
335
|
|
|
336
|
+
### Testing
|
|
337
|
+
|
|
338
|
+
```bash
|
|
339
|
+
npm test
|
|
385
340
|
```
|
|
341
|
+
|
|
342
|
+
## License
|
|
343
|
+
|
|
344
|
+
MIT
|
package/dist/cjs/constants.js
CHANGED
|
@@ -37,17 +37,17 @@ exports.GOOGLE_TRENDS_MAPPER = {
|
|
|
37
37
|
headers: {},
|
|
38
38
|
},
|
|
39
39
|
[enums_js_1.GoogleTrendsEndpoints.relatedTopics]: {
|
|
40
|
-
path: '/trends/api/widgetdata/
|
|
40
|
+
path: '/trends/api/widgetdata/relatedsearches',
|
|
41
41
|
method: 'GET',
|
|
42
42
|
host: GOOGLE_TRENDS_BASE_URL,
|
|
43
|
-
url: `https://${GOOGLE_TRENDS_BASE_URL}/trends/api/widgetdata/
|
|
43
|
+
url: `https://${GOOGLE_TRENDS_BASE_URL}/trends/api/widgetdata/relatedsearches`,
|
|
44
44
|
headers: {},
|
|
45
45
|
},
|
|
46
46
|
[enums_js_1.GoogleTrendsEndpoints.relatedQueries]: {
|
|
47
|
-
path: '/trends/api/widgetdata/
|
|
47
|
+
path: '/trends/api/widgetdata/relatedsearches',
|
|
48
48
|
method: 'GET',
|
|
49
49
|
host: GOOGLE_TRENDS_BASE_URL,
|
|
50
|
-
url: `https://${GOOGLE_TRENDS_BASE_URL}/trends/api/widgetdata/
|
|
50
|
+
url: `https://${GOOGLE_TRENDS_BASE_URL}/trends/api/widgetdata/relatedsearches`,
|
|
51
51
|
headers: {},
|
|
52
52
|
},
|
|
53
53
|
};
|
|
@@ -1,2 +1,4 @@
|
|
|
1
1
|
import { DailyTrendingTopics } from '../types/index.js';
|
|
2
|
+
export declare const formatTrendsDate: (date: Date) => string;
|
|
3
|
+
export declare const formatDate: (date: Date) => string;
|
|
2
4
|
export declare const extractJsonFromResponse: (text: string) => DailyTrendingTopics | null;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.extractJsonFromResponse = void 0;
|
|
3
|
+
exports.extractJsonFromResponse = exports.formatDate = exports.formatTrendsDate = void 0;
|
|
4
4
|
const GoogleTrendsError_js_1 = require("../errors/GoogleTrendsError.js");
|
|
5
5
|
// For future reference and update: from google trends page rpc call response,
|
|
6
6
|
// 0 "twitter down" The main trending search term.
|
|
@@ -16,6 +16,21 @@ const GoogleTrendsError_js_1 = require("../errors/GoogleTrendsError.js");
|
|
|
16
16
|
// 10 [11] Unclear, possibly a category identifier.
|
|
17
17
|
// 11 [[3606769742, "en", "US"], [3596035008, "en", "US"]] User demographics or trending sources, with numerical IDs, language ("en" for English), and country ("US" for United States).
|
|
18
18
|
// 12 "twitter down" The original trending keyword (sometimes a duplicate of index 0).
|
|
19
|
+
const formatTrendsDate = (date) => {
|
|
20
|
+
const pad = (n) => n.toString().padStart(2, '0');
|
|
21
|
+
const yyyy = date.getFullYear();
|
|
22
|
+
const mm = pad(date.getMonth() + 1);
|
|
23
|
+
const dd = pad(date.getDate());
|
|
24
|
+
const hh = pad(date.getHours());
|
|
25
|
+
const min = pad(date.getMinutes());
|
|
26
|
+
const ss = pad(date.getSeconds());
|
|
27
|
+
return `${yyyy}-${mm}-${dd}T${hh}\\:${min}\\:${ss}`;
|
|
28
|
+
};
|
|
29
|
+
exports.formatTrendsDate = formatTrendsDate;
|
|
30
|
+
const formatDate = (date) => {
|
|
31
|
+
return date.toISOString().split('T')[0];
|
|
32
|
+
};
|
|
33
|
+
exports.formatDate = formatDate;
|
|
19
34
|
const extractJsonFromResponse = (text) => {
|
|
20
35
|
const cleanedText = text.replace(/^\)\]\}'/, '').trim();
|
|
21
36
|
try {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { DailyTrendingTopics, DailyTrendingTopicsOptions, RealTimeTrendsOptions, ExploreOptions, ExploreResponse, InterestByRegionOptions, InterestByRegionResponse, GoogleTrendsResponse, GoogleTrendsError, RelatedTopicsResponse, RelatedQueriesResponse, RelatedData } from '../types/index.js';
|
|
1
|
+
import { DailyTrendingTopics, DailyTrendingTopicsOptions, RealTimeTrendsOptions, ExploreOptions, ExploreResponse, InterestByRegionOptions, InterestByRegionResponse, GoogleTrendsResponse, GoogleTrendsError, RelatedTopicsResponse, RelatedTopicsOptions, RelatedQueriesResponse, RelatedData, RelatedQueriesOptions } from '../types/index.js';
|
|
2
2
|
export declare class GoogleTrendsApi {
|
|
3
3
|
/**
|
|
4
4
|
* Get autocomplete suggestions for a keyword
|
|
@@ -19,14 +19,12 @@ export declare class GoogleTrendsApi {
|
|
|
19
19
|
* @returns Promise with trending topics data
|
|
20
20
|
*/
|
|
21
21
|
realTimeTrends({ geo, trendingHours }: RealTimeTrendsOptions): Promise<GoogleTrendsResponse<DailyTrendingTopics>>;
|
|
22
|
-
explore({ keyword, geo, time, category, property, hl, }: ExploreOptions): Promise<ExploreResponse | {
|
|
22
|
+
explore({ keyword, geo, time, category, property, hl, enableBackoff, }: ExploreOptions): Promise<ExploreResponse | {
|
|
23
23
|
error: GoogleTrendsError;
|
|
24
24
|
}>;
|
|
25
|
-
interestByRegion({ keyword, startTime, endTime, geo, resolution, hl, timezone, category }: InterestByRegionOptions): Promise<InterestByRegionResponse
|
|
26
|
-
|
|
27
|
-
}
|
|
28
|
-
relatedTopics({ keyword, geo, time, category, property, hl, }: ExploreOptions): Promise<GoogleTrendsResponse<RelatedTopicsResponse>>;
|
|
29
|
-
relatedQueries({ keyword, geo, time, category, property, hl, }: ExploreOptions): Promise<GoogleTrendsResponse<RelatedQueriesResponse>>;
|
|
25
|
+
interestByRegion({ keyword, startTime, endTime, geo, resolution, hl, timezone, category, enableBackoff }: InterestByRegionOptions): Promise<GoogleTrendsResponse<InterestByRegionResponse>>;
|
|
26
|
+
relatedTopics({ keyword, geo, startTime, endTime, category, property, hl, enableBackoff, }: RelatedTopicsOptions): Promise<GoogleTrendsResponse<RelatedTopicsResponse>>;
|
|
27
|
+
relatedQueries({ keyword, geo, startTime, endTime, category, property, hl, enableBackoff, }: RelatedQueriesOptions): Promise<GoogleTrendsResponse<RelatedQueriesResponse>>;
|
|
30
28
|
relatedData({ keyword, geo, time, category, property, hl, }: ExploreOptions): Promise<GoogleTrendsResponse<RelatedData>>;
|
|
31
29
|
}
|
|
32
30
|
declare const _default: GoogleTrendsApi;
|