@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.
- package/dist/cjs/constants.d.ts +3 -0
- package/{lib → dist/cjs}/constants.js +5 -4
- package/dist/cjs/errors/GoogleTrendsError.d.ts +23 -0
- package/dist/cjs/errors/GoogleTrendsError.js +45 -0
- package/{lib → dist/cjs}/helpers/format.js +36 -9
- package/dist/cjs/helpers/googleTrendsAPI.d.ts +26 -0
- package/dist/cjs/helpers/googleTrendsAPI.js +211 -0
- package/{lib → dist/cjs}/helpers/request.js +14 -18
- package/dist/cjs/index.d.ts +9 -0
- package/dist/cjs/index.js +14 -0
- package/dist/cjs/types/enums.d.ts +12 -0
- package/dist/cjs/types/enums.js +17 -0
- package/dist/esm/constants.d.ts +3 -0
- package/dist/esm/constants.js +36 -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 +26 -0
- package/dist/esm/helpers/googleTrendsAPI.js +207 -0
- package/dist/esm/helpers/request.d.ts +9 -0
- package/dist/esm/helpers/request.js +75 -0
- package/dist/esm/index.d.ts +9 -0
- package/dist/esm/index.js +11 -0
- package/dist/esm/types/enums.d.ts +12 -0
- package/dist/esm/types/enums.js +14 -0
- package/package.json +30 -36
- package/lib/constants.d.ts +0 -2
- package/lib/helpers/googleTrendsAPI.d.ts +0 -10
- package/lib/helpers/googleTrendsAPI.js +0 -200
- package/lib/index.d.ts +0 -2
- package/lib/index.js +0 -7
- /package/{lib → dist/cjs}/helpers/format.d.ts +0 -0
- /package/{lib → dist/cjs}/helpers/request.d.ts +0 -0
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.GOOGLE_TRENDS_MAPPER = void 0;
|
|
4
|
+
const enums_1 = require("./types/enums");
|
|
4
5
|
const GOOGLE_TRENDS_BASE_URL = 'trends.google.com';
|
|
5
6
|
exports.GOOGLE_TRENDS_MAPPER = {
|
|
6
|
-
[
|
|
7
|
+
[enums_1.GoogleTrendsEndpoints.dailyTrends]: {
|
|
7
8
|
path: '/_/TrendsUi/data/batchexecute',
|
|
8
9
|
method: 'POST',
|
|
9
10
|
host: GOOGLE_TRENDS_BASE_URL,
|
|
@@ -12,7 +13,7 @@ exports.GOOGLE_TRENDS_MAPPER = {
|
|
|
12
13
|
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
|
|
13
14
|
},
|
|
14
15
|
},
|
|
15
|
-
[
|
|
16
|
+
[enums_1.GoogleTrendsEndpoints.autocomplete]: {
|
|
16
17
|
path: '/trends/api/autocomplete',
|
|
17
18
|
method: 'GET',
|
|
18
19
|
host: GOOGLE_TRENDS_BASE_URL,
|
|
@@ -21,14 +22,14 @@ exports.GOOGLE_TRENDS_MAPPER = {
|
|
|
21
22
|
accept: 'application/json, text/plain, */*',
|
|
22
23
|
},
|
|
23
24
|
},
|
|
24
|
-
[
|
|
25
|
+
[enums_1.GoogleTrendsEndpoints.explore]: {
|
|
25
26
|
path: '/trends/api/explore',
|
|
26
27
|
method: 'POST',
|
|
27
28
|
host: GOOGLE_TRENDS_BASE_URL,
|
|
28
29
|
url: `https://${GOOGLE_TRENDS_BASE_URL}/trends/api/explore`,
|
|
29
30
|
headers: {},
|
|
30
31
|
},
|
|
31
|
-
[
|
|
32
|
+
[enums_1.GoogleTrendsEndpoints.interestByRegion]: {
|
|
32
33
|
path: '/trends/api/widgetdata/comparedgeo',
|
|
33
34
|
method: 'GET',
|
|
34
35
|
host: GOOGLE_TRENDS_BASE_URL,
|
|
@@ -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,45 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.UnknownError = exports.ParseError = exports.NetworkError = exports.InvalidRequestError = exports.RateLimitError = void 0;
|
|
4
|
+
class RateLimitError extends Error {
|
|
5
|
+
constructor(message = 'Rate limit exceeded') {
|
|
6
|
+
super(message);
|
|
7
|
+
this.code = 'RATE_LIMIT_EXCEEDED';
|
|
8
|
+
this.statusCode = 429;
|
|
9
|
+
this.name = 'RateLimitError';
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
exports.RateLimitError = RateLimitError;
|
|
13
|
+
class InvalidRequestError extends Error {
|
|
14
|
+
constructor(message = 'Invalid request parameters') {
|
|
15
|
+
super(message);
|
|
16
|
+
this.code = 'INVALID_REQUEST';
|
|
17
|
+
this.statusCode = 400;
|
|
18
|
+
this.name = 'InvalidRequestError';
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
exports.InvalidRequestError = InvalidRequestError;
|
|
22
|
+
class NetworkError extends Error {
|
|
23
|
+
constructor(message = 'Network request failed') {
|
|
24
|
+
super(message);
|
|
25
|
+
this.code = 'NETWORK_ERROR';
|
|
26
|
+
this.name = 'NetworkError';
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
exports.NetworkError = NetworkError;
|
|
30
|
+
class ParseError extends Error {
|
|
31
|
+
constructor(message = 'Failed to parse response') {
|
|
32
|
+
super(message);
|
|
33
|
+
this.code = 'PARSE_ERROR';
|
|
34
|
+
this.name = 'ParseError';
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
exports.ParseError = ParseError;
|
|
38
|
+
class UnknownError extends Error {
|
|
39
|
+
constructor(message = 'An unknown error occurred') {
|
|
40
|
+
super(message);
|
|
41
|
+
this.code = 'UNKNOWN_ERROR';
|
|
42
|
+
this.name = 'UnknownError';
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
exports.UnknownError = UnknownError;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.extractJsonFromResponse = void 0;
|
|
4
|
+
const GoogleTrendsError_1 = require("../errors/GoogleTrendsError");
|
|
4
5
|
// For future refrence and update: from google trends page rpc call response,
|
|
5
6
|
// 0 "twitter down" The main trending search term.
|
|
6
7
|
// 1 null Unused (reserved for future Google Trends data).
|
|
@@ -20,33 +21,59 @@ const extractJsonFromResponse = (text) => {
|
|
|
20
21
|
try {
|
|
21
22
|
const parsedResponse = JSON.parse(cleanedText);
|
|
22
23
|
if (!Array.isArray(parsedResponse) || parsedResponse.length === 0) {
|
|
23
|
-
|
|
24
|
+
throw new GoogleTrendsError_1.ParseError('Invalid response format: empty array');
|
|
24
25
|
}
|
|
25
26
|
const nestedJsonString = parsedResponse[0][2];
|
|
26
27
|
if (!nestedJsonString) {
|
|
27
|
-
|
|
28
|
+
throw new GoogleTrendsError_1.ParseError('Invalid response format: missing nested JSON');
|
|
28
29
|
}
|
|
29
30
|
const data = JSON.parse(nestedJsonString);
|
|
30
31
|
if (!data || !Array.isArray(data) || data.length < 2) {
|
|
31
|
-
|
|
32
|
+
throw new GoogleTrendsError_1.ParseError('Invalid response format: missing data array');
|
|
32
33
|
}
|
|
33
34
|
return updateResponseObject(data[1]);
|
|
34
35
|
}
|
|
35
36
|
catch (e) {
|
|
36
|
-
|
|
37
|
-
|
|
37
|
+
if (e instanceof GoogleTrendsError_1.ParseError) {
|
|
38
|
+
throw e;
|
|
39
|
+
}
|
|
40
|
+
throw new GoogleTrendsError_1.ParseError('Failed to parse response');
|
|
38
41
|
}
|
|
39
42
|
};
|
|
40
43
|
exports.extractJsonFromResponse = extractJsonFromResponse;
|
|
41
44
|
const updateResponseObject = (data) => {
|
|
42
45
|
if (!Array.isArray(data)) {
|
|
43
|
-
|
|
46
|
+
throw new GoogleTrendsError_1.ParseError('Invalid data format: expected array');
|
|
44
47
|
}
|
|
45
|
-
const allTrendingStories =
|
|
48
|
+
const allTrendingStories = [];
|
|
46
49
|
const summary = [];
|
|
47
50
|
data.forEach((item) => {
|
|
48
|
-
if (Array.isArray(item)
|
|
49
|
-
|
|
51
|
+
if (Array.isArray(item)) {
|
|
52
|
+
const story = {
|
|
53
|
+
title: String(item[0] || ''),
|
|
54
|
+
traffic: String(item[6] || '0'),
|
|
55
|
+
articles: Array.isArray(item[9]) ? item[9].map((article) => ({
|
|
56
|
+
title: String(article[0] || ''),
|
|
57
|
+
url: String(article[1] || ''),
|
|
58
|
+
source: String(article[2] || ''),
|
|
59
|
+
time: String(article[3] || ''),
|
|
60
|
+
snippet: String(article[4] || '')
|
|
61
|
+
})) : [],
|
|
62
|
+
shareUrl: String(item[12] || '')
|
|
63
|
+
};
|
|
64
|
+
if (item[1]) {
|
|
65
|
+
story.image = {
|
|
66
|
+
newsUrl: String(item[1][0] || ''),
|
|
67
|
+
source: String(item[1][1] || ''),
|
|
68
|
+
imageUrl: String(item[1][2] || '')
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
allTrendingStories.push(story);
|
|
72
|
+
summary.push({
|
|
73
|
+
title: story.title,
|
|
74
|
+
traffic: story.traffic,
|
|
75
|
+
articles: story.articles
|
|
76
|
+
});
|
|
50
77
|
}
|
|
51
78
|
});
|
|
52
79
|
return { allTrendingStories, summary };
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { DailyTrendingTopics, DailyTrendingTopicsOptions, RealTimeTrendsOptions, ExploreOptions, ExploreResponse, InterestByRegionOptions, InterestByRegionResponse, GoogleTrendsResponse } from '../types/index';
|
|
2
|
+
export declare class GoogleTrendsApi {
|
|
3
|
+
/**
|
|
4
|
+
* Get autocomplete suggestions for a keyword
|
|
5
|
+
* @param keyword - The keyword to get suggestions for
|
|
6
|
+
* @param hl - Language code (default: 'en-US')
|
|
7
|
+
* @returns Promise with array of suggestion strings
|
|
8
|
+
*/
|
|
9
|
+
autocomplete(keyword: string, hl?: string): Promise<GoogleTrendsResponse<string[]>>;
|
|
10
|
+
/**
|
|
11
|
+
* Get daily trending topics
|
|
12
|
+
* @param options - Options for daily trends request
|
|
13
|
+
* @returns Promise with trending topics data
|
|
14
|
+
*/
|
|
15
|
+
dailyTrends({ geo, lang }: DailyTrendingTopicsOptions): Promise<GoogleTrendsResponse<DailyTrendingTopics>>;
|
|
16
|
+
/**
|
|
17
|
+
* Get real-time trending topics
|
|
18
|
+
* @param options - Options for real-time trends request
|
|
19
|
+
* @returns Promise with trending topics data
|
|
20
|
+
*/
|
|
21
|
+
realTimeTrends({ geo, trendingHours }: RealTimeTrendsOptions): Promise<GoogleTrendsResponse<DailyTrendingTopics>>;
|
|
22
|
+
explore({ keyword, geo, time, category, property, hl, }: ExploreOptions): Promise<ExploreResponse>;
|
|
23
|
+
interestByRegion({ keyword, startTime, endTime, geo, resolution, hl, timezone, category }: InterestByRegionOptions): Promise<InterestByRegionResponse>;
|
|
24
|
+
}
|
|
25
|
+
declare const _default: GoogleTrendsApi;
|
|
26
|
+
export default _default;
|
|
@@ -0,0 +1,211 @@
|
|
|
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
|
+
// Remove the first 5 characters (JSONP wrapper) and parse
|
|
125
|
+
const data = JSON.parse(text.slice(5));
|
|
126
|
+
return data;
|
|
127
|
+
}
|
|
128
|
+
catch (error) {
|
|
129
|
+
console.error('Explore request failed:', error);
|
|
130
|
+
return { widgets: [] };
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
//
|
|
134
|
+
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 }) {
|
|
135
|
+
const formatDate = (date) => {
|
|
136
|
+
return date.toISOString().split('T')[0];
|
|
137
|
+
};
|
|
138
|
+
const formatTrendsDate = (date) => {
|
|
139
|
+
const pad = (n) => n.toString().padStart(2, '0');
|
|
140
|
+
const yyyy = date.getFullYear();
|
|
141
|
+
const mm = pad(date.getMonth() + 1);
|
|
142
|
+
const dd = pad(date.getDate());
|
|
143
|
+
const hh = pad(date.getHours());
|
|
144
|
+
const min = pad(date.getMinutes());
|
|
145
|
+
const ss = pad(date.getSeconds());
|
|
146
|
+
return `${yyyy}-${mm}-${dd}T${hh}\\:${min}\\:${ss}`;
|
|
147
|
+
};
|
|
148
|
+
const getDateRangeParam = (date) => {
|
|
149
|
+
const yesterday = new Date(date);
|
|
150
|
+
yesterday.setDate(date.getDate() - 1);
|
|
151
|
+
const formattedStart = formatTrendsDate(yesterday);
|
|
152
|
+
const formattedEnd = formatTrendsDate(date);
|
|
153
|
+
return `${formattedStart} ${formattedEnd}`;
|
|
154
|
+
};
|
|
155
|
+
const exploreResponse = await this.explore({
|
|
156
|
+
keyword: Array.isArray(keyword) ? keyword[0] : keyword,
|
|
157
|
+
geo: Array.isArray(geo) ? geo[0] : geo,
|
|
158
|
+
time: `${getDateRangeParam(startTime)} ${getDateRangeParam(endTime)}`,
|
|
159
|
+
category,
|
|
160
|
+
hl
|
|
161
|
+
});
|
|
162
|
+
const widget = exploreResponse.widgets.find(w => w.id === 'GEO_MAP');
|
|
163
|
+
if (!widget) {
|
|
164
|
+
return { default: { geoMapData: [] } };
|
|
165
|
+
}
|
|
166
|
+
const options = {
|
|
167
|
+
...constants_1.GOOGLE_TRENDS_MAPPER[enums_1.GoogleTrendsEndpoints.interestByRegion],
|
|
168
|
+
qs: {
|
|
169
|
+
hl,
|
|
170
|
+
tz: timezone.toString(),
|
|
171
|
+
req: JSON.stringify({
|
|
172
|
+
geo: {
|
|
173
|
+
country: Array.isArray(geo) ? geo[0] : geo
|
|
174
|
+
},
|
|
175
|
+
comparisonItem: [{
|
|
176
|
+
time: `${formatDate(startTime)} ${formatDate(endTime)}`,
|
|
177
|
+
complexKeywordsRestriction: {
|
|
178
|
+
keyword: [{
|
|
179
|
+
type: 'BROAD',
|
|
180
|
+
value: Array.isArray(keyword) ? keyword[0] : keyword
|
|
181
|
+
}]
|
|
182
|
+
}
|
|
183
|
+
}],
|
|
184
|
+
resolution,
|
|
185
|
+
locale: hl,
|
|
186
|
+
requestOptions: {
|
|
187
|
+
property: '',
|
|
188
|
+
backend: 'CM',
|
|
189
|
+
category
|
|
190
|
+
},
|
|
191
|
+
userConfig: {
|
|
192
|
+
userType: 'USER_TYPE_LEGIT_USER'
|
|
193
|
+
}
|
|
194
|
+
}),
|
|
195
|
+
token: widget.token
|
|
196
|
+
}
|
|
197
|
+
};
|
|
198
|
+
try {
|
|
199
|
+
const response = await (0, request_1.request)(options.url, options);
|
|
200
|
+
const text = await response.text();
|
|
201
|
+
// Remove the first 5 characters (JSONP wrapper) and parse
|
|
202
|
+
const data = JSON.parse(text.slice(5));
|
|
203
|
+
return data;
|
|
204
|
+
}
|
|
205
|
+
catch (error) {
|
|
206
|
+
return { default: { geoMapData: [] } };
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
exports.GoogleTrendsApi = GoogleTrendsApi;
|
|
211
|
+
exports.default = new GoogleTrendsApi();
|
|
@@ -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,9 @@
|
|
|
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
|
+
declare const _default: {
|
|
5
|
+
dailyTrends: ({ geo, lang }: import("./types").DailyTrendingTopicsOptions) => Promise<import("./types").GoogleTrendsResponse<import("./types").DailyTrendingTopics>>;
|
|
6
|
+
realTimeTrends: ({ geo, trendingHours }: import("./types").RealTimeTrendsOptions) => Promise<import("./types").GoogleTrendsResponse<import("./types").DailyTrendingTopics>>;
|
|
7
|
+
autocomplete: (keyword: string, hl?: string) => Promise<import("./types").GoogleTrendsResponse<string[]>>;
|
|
8
|
+
};
|
|
9
|
+
export default _default;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
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
|
+
// Default export for CommonJS compatibility
|
|
10
|
+
exports.default = {
|
|
11
|
+
dailyTrends: exports.dailyTrends,
|
|
12
|
+
realTimeTrends: exports.realTimeTrends,
|
|
13
|
+
autocomplete: exports.autocomplete
|
|
14
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export declare enum GoogleTrendsEndpoints {
|
|
2
|
+
dailyTrends = "dailyTrends",
|
|
3
|
+
autocomplete = "autocomplete",
|
|
4
|
+
explore = "explore",
|
|
5
|
+
interestByRegion = "interestByRegion"
|
|
6
|
+
}
|
|
7
|
+
export declare enum GoogleTrendsTrendingHours {
|
|
8
|
+
fourHrs = 4,
|
|
9
|
+
oneDay = 24,
|
|
10
|
+
twoDays = 48,
|
|
11
|
+
sevenDays = 168
|
|
12
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
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 = exports.GoogleTrendsEndpoints || (exports.GoogleTrendsEndpoints = {}));
|
|
11
|
+
var GoogleTrendsTrendingHours;
|
|
12
|
+
(function (GoogleTrendsTrendingHours) {
|
|
13
|
+
GoogleTrendsTrendingHours[GoogleTrendsTrendingHours["fourHrs"] = 4] = "fourHrs";
|
|
14
|
+
GoogleTrendsTrendingHours[GoogleTrendsTrendingHours["oneDay"] = 24] = "oneDay";
|
|
15
|
+
GoogleTrendsTrendingHours[GoogleTrendsTrendingHours["twoDays"] = 48] = "twoDays";
|
|
16
|
+
GoogleTrendsTrendingHours[GoogleTrendsTrendingHours["sevenDays"] = 168] = "sevenDays";
|
|
17
|
+
})(GoogleTrendsTrendingHours = exports.GoogleTrendsTrendingHours || (exports.GoogleTrendsTrendingHours = {}));
|
|
@@ -0,0 +1,36 @@
|
|
|
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
|
+
};
|
|
@@ -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
|
+
}
|