@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 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', // Default: 'US'
128
- time: 'today 12-m', // Default: '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 or 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 or string[] - defaults to 'US'
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', // Required
198
- geo: 'US', // Optional - defaults to 'US'
199
- time: 'now 1-d', // Optional - defaults to 'now 1-d'
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', // Required
234
- geo: 'US', // Optional - defaults to 'US'
235
- time: 'now 1-d', // Optional - defaults to 'now 1-d'
236
- category: 0, // Optional - defaults to 0
237
- property: '', // Optional - defaults to ''
238
- hl: 'en-US', // Optional - defaults to '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; // Default: 'US'
289
- lang?: string; // Default: 'en'
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; // Default: 4
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; // Default: 'US'
308
- time?: string; // Default: 'today 12-m'
309
- category?: number; // Default: 0
310
- property?: string; // Default: ''
311
- hl?: string; // Default: 'en-US'
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 | string[]; // Required - search term(s)
320
- startTime?: Date; // Optional - start date
321
- endTime?: Date; // Optional - end date
322
- geo?: string | string[]; // Optional - geocode(s)
323
- resolution?: 'COUNTRY' | 'REGION' | 'CITY' | 'DMA'; // Optional
324
- hl?: string; // Optional - language code
325
- timezone?: number; // Optional - timezone offset
326
- category?: number; // Optional - 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
- ### RelatedQueriesResponse
298
+ ### RelatedTopicsOptions
353
299
 
354
300
  ```typescript
355
- interface RelatedQueriesResponse {
356
- default: {
357
- rankedList: Array<{
358
- rankedKeyword: Array<{
359
- query: string;
360
- value: number;
361
- formattedValue: string;
362
- hasData: boolean;
363
- link: string;
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
- ### RelatedData
313
+ ### RelatedQueriesOptions
371
314
 
372
315
  ```typescript
373
- interface RelatedData {
374
- topics: Array<RelatedTopic>;
375
- queries: Array<RelatedQuery>;
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
@@ -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/relatedtopics',
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/relatedtopics`,
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/relatedqueries',
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/relatedqueries`,
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
- error: GoogleTrendsError;
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;