@shaivpidadi/trends-js 0.0.0-beta.5 → 0.0.0-beta.7

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.
Files changed (34) hide show
  1. package/dist/cjs/constants.d.ts +3 -0
  2. package/{lib → dist/cjs}/constants.js +5 -4
  3. package/dist/cjs/errors/GoogleTrendsError.d.ts +23 -0
  4. package/dist/cjs/errors/GoogleTrendsError.js +45 -0
  5. package/{lib → dist/cjs}/helpers/format.js +36 -9
  6. package/dist/cjs/helpers/googleTrendsAPI.d.ts +26 -0
  7. package/dist/cjs/helpers/googleTrendsAPI.js +211 -0
  8. package/{lib → dist/cjs}/helpers/request.js +14 -18
  9. package/dist/cjs/index.d.ts +9 -0
  10. package/dist/cjs/index.js +14 -0
  11. package/dist/cjs/types/enums.d.ts +12 -0
  12. package/dist/cjs/types/enums.js +17 -0
  13. package/dist/esm/constants.d.ts +3 -0
  14. package/dist/esm/constants.js +36 -0
  15. package/dist/esm/errors/GoogleTrendsError.d.ts +23 -0
  16. package/dist/esm/errors/GoogleTrendsError.js +37 -0
  17. package/dist/esm/helpers/format.d.ts +2 -0
  18. package/dist/esm/helpers/format.js +76 -0
  19. package/dist/esm/helpers/googleTrendsAPI.d.ts +26 -0
  20. package/dist/esm/helpers/googleTrendsAPI.js +207 -0
  21. package/dist/esm/helpers/request.d.ts +9 -0
  22. package/dist/esm/helpers/request.js +75 -0
  23. package/dist/esm/index.d.ts +9 -0
  24. package/dist/esm/index.js +11 -0
  25. package/dist/esm/types/enums.d.ts +12 -0
  26. package/dist/esm/types/enums.js +14 -0
  27. package/package.json +30 -36
  28. package/lib/constants.d.ts +0 -2
  29. package/lib/helpers/googleTrendsAPI.d.ts +0 -10
  30. package/lib/helpers/googleTrendsAPI.js +0 -200
  31. package/lib/index.d.ts +0 -2
  32. package/lib/index.js +0 -7
  33. /package/{lib → dist/cjs}/helpers/format.d.ts +0 -0
  34. /package/{lib → dist/cjs}/helpers/request.d.ts +0 -0
@@ -1,200 +0,0 @@
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
- Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.GoogleTrendsApi = void 0;
13
- const request_1 = require("./request");
14
- const format_1 = require("./format");
15
- const constants_1 = require("../constants");
16
- class GoogleTrendsApi {
17
- autocomplete(keyword, hl = 'en-US') {
18
- return __awaiter(this, void 0, void 0, function* () {
19
- if (!keyword) {
20
- return [];
21
- }
22
- const options = Object.assign(Object.assign({}, constants_1.GOOGLE_TRENDS_MAPPER["autocomplete" /* GoogleTrendsEndpoints.autocomplete */]), { qs: {
23
- hl,
24
- tz: '240',
25
- } });
26
- try {
27
- const response = yield (0, request_1.request)(`${options.url}/${encodeURIComponent(keyword)}`, options);
28
- const text = yield response.text();
29
- // Remove the first 5 characters (JSONP wrapper) and parse
30
- const data = JSON.parse(text.slice(5));
31
- return data.default.topics.map((topic) => topic.title);
32
- }
33
- catch (error) {
34
- console.error('Autocomplete request failed:', error);
35
- return [];
36
- }
37
- });
38
- }
39
- dailyTrends({ geo = 'US', lang = 'en' }) {
40
- return __awaiter(this, void 0, void 0, function* () {
41
- const defaultOptions = constants_1.GOOGLE_TRENDS_MAPPER["dailyTrends" /* GoogleTrendsEndpoints.dailyTrends */];
42
- const options = Object.assign(Object.assign({}, defaultOptions), { body: new URLSearchParams({
43
- 'f.req': `[[["i0OFE","[null,null,\\"${geo}\\",0,\\"${lang}\\",24,1]",null,"generic"]]]`,
44
- }).toString(), contentType: 'form' });
45
- try {
46
- const response = yield (0, request_1.request)(options.url, options);
47
- const text = yield response.text();
48
- const trendingTopics = (0, format_1.extractJsonFromResponse)(text);
49
- if (!trendingTopics) {
50
- return {
51
- allTrendingStories: [],
52
- summary: [],
53
- };
54
- }
55
- return trendingTopics;
56
- }
57
- catch (error) {
58
- console.error(error);
59
- return {
60
- allTrendingStories: [],
61
- summary: [],
62
- };
63
- }
64
- });
65
- }
66
- realTimeTrends({ geo = 'US', trendingHours = 4 }) {
67
- return __awaiter(this, void 0, void 0, function* () {
68
- const defaultOptions = constants_1.GOOGLE_TRENDS_MAPPER["dailyTrends" /* GoogleTrendsEndpoints.dailyTrends */];
69
- const options = Object.assign(Object.assign({}, defaultOptions), { body: new URLSearchParams({
70
- 'f.req': `[[["i0OFE","[null,null,\\"${geo}\\",0,\\"en\\",${trendingHours},1]",null,"generic"]]]`,
71
- }).toString(), contentType: 'form' });
72
- try {
73
- const response = yield (0, request_1.request)(options.url, options);
74
- const text = yield response.text();
75
- const trendingTopics = (0, format_1.extractJsonFromResponse)(text);
76
- if (!trendingTopics) {
77
- return {
78
- allTrendingStories: [],
79
- summary: [],
80
- };
81
- }
82
- return trendingTopics;
83
- }
84
- catch (error) {
85
- console.error(error);
86
- return {
87
- allTrendingStories: [],
88
- summary: [],
89
- };
90
- }
91
- });
92
- }
93
- explore({ keyword, geo = 'US', time = 'today 12-m', category = 0, property = '', hl = 'en-US', }) {
94
- return __awaiter(this, void 0, void 0, function* () {
95
- const options = Object.assign(Object.assign({}, constants_1.GOOGLE_TRENDS_MAPPER["explore" /* GoogleTrendsEndpoints.explore */]), { qs: {
96
- hl,
97
- tz: '240',
98
- req: JSON.stringify({
99
- comparisonItem: [
100
- {
101
- keyword,
102
- geo,
103
- time,
104
- },
105
- ],
106
- category,
107
- property,
108
- }),
109
- } });
110
- try {
111
- const response = yield (0, request_1.request)(options.url, options);
112
- const text = yield response.text();
113
- // Remove the first 5 characters (JSONP wrapper) and parse
114
- const data = JSON.parse(text.slice(5));
115
- return data;
116
- }
117
- catch (error) {
118
- console.error('Explore request failed:', error);
119
- return { widgets: [] };
120
- }
121
- });
122
- }
123
- //
124
- interestByRegion({ keyword, startTime = new Date('2004-01-01'), endTime = new Date(), geo = 'US', resolution = 'REGION', hl = 'en-US', timezone = new Date().getTimezoneOffset(), category = 0 }) {
125
- return __awaiter(this, void 0, void 0, function* () {
126
- const formatDate = (date) => {
127
- return date.toISOString().split('T')[0];
128
- };
129
- const formatTrendsDate = (date) => {
130
- const pad = (n) => n.toString().padStart(2, '0');
131
- const yyyy = date.getFullYear();
132
- const mm = pad(date.getMonth() + 1);
133
- const dd = pad(date.getDate());
134
- const hh = pad(date.getHours());
135
- const min = pad(date.getMinutes());
136
- const ss = pad(date.getSeconds());
137
- return `${yyyy}-${mm}-${dd}T${hh}\\:${min}\\:${ss}`;
138
- };
139
- const getDateRangeParam = (date) => {
140
- const yesterday = new Date(date);
141
- yesterday.setDate(date.getDate() - 1);
142
- const formattedStart = formatTrendsDate(yesterday);
143
- const formattedEnd = formatTrendsDate(date);
144
- return `${formattedStart} ${formattedEnd}`;
145
- };
146
- const exploreResponse = yield this.explore({
147
- keyword: Array.isArray(keyword) ? keyword[0] : keyword,
148
- geo: Array.isArray(geo) ? geo[0] : geo,
149
- time: `${getDateRangeParam(startTime)} ${getDateRangeParam(endTime)}`,
150
- category,
151
- hl
152
- });
153
- const widget = exploreResponse.widgets.find(w => w.id === 'GEO_MAP');
154
- if (!widget) {
155
- return { default: { geoMapData: [] } };
156
- }
157
- const options = Object.assign(Object.assign({}, constants_1.GOOGLE_TRENDS_MAPPER["interestByRegion" /* GoogleTrendsEndpoints.interestByRegion */]), { qs: {
158
- hl,
159
- tz: timezone.toString(),
160
- req: JSON.stringify({
161
- geo: {
162
- country: Array.isArray(geo) ? geo[0] : geo
163
- },
164
- comparisonItem: [{
165
- time: `${formatDate(startTime)} ${formatDate(endTime)}`,
166
- complexKeywordsRestriction: {
167
- keyword: [{
168
- type: 'BROAD',
169
- value: Array.isArray(keyword) ? keyword[0] : keyword
170
- }]
171
- }
172
- }],
173
- resolution,
174
- locale: hl,
175
- requestOptions: {
176
- property: '',
177
- backend: 'CM',
178
- category
179
- },
180
- userConfig: {
181
- userType: 'USER_TYPE_LEGIT_USER'
182
- }
183
- }),
184
- token: widget.token
185
- } });
186
- try {
187
- const response = yield (0, request_1.request)(options.url, options);
188
- const text = yield response.text();
189
- // Remove the first 5 characters (JSONP wrapper) and parse
190
- const data = JSON.parse(text.slice(5));
191
- return data;
192
- }
193
- catch (error) {
194
- return { default: { geoMapData: [] } };
195
- }
196
- });
197
- }
198
- }
199
- exports.GoogleTrendsApi = GoogleTrendsApi;
200
- exports.default = new GoogleTrendsApi();
package/lib/index.d.ts DELETED
@@ -1,2 +0,0 @@
1
- import GoogleTrendsApi from './helpers/googleTrendsAPI';
2
- export default GoogleTrendsApi;
package/lib/index.js DELETED
@@ -1,7 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- const googleTrendsAPI_1 = __importDefault(require("./helpers/googleTrendsAPI"));
7
- exports.default = googleTrendsAPI_1.default;
File without changes
File without changes