@shaivpidadi/trends-js 0.0.0-beta.4 → 0.0.0-beta.6
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/lib/errors/GoogleTrendsError.d.ts +23 -0
- package/lib/errors/GoogleTrendsError.js +45 -0
- package/lib/helpers/format.js +36 -9
- package/lib/helpers/googleTrendsAPI.d.ts +20 -4
- package/lib/helpers/googleTrendsAPI.js +39 -28
- package/lib/helpers/request.d.ts +6 -2
- package/lib/helpers/request.js +32 -28
- package/package.json +1 -1
|
@@ -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;
|
package/lib/helpers/format.js
CHANGED
|
@@ -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 };
|
|
@@ -1,8 +1,24 @@
|
|
|
1
|
-
import { DailyTrendingTopics, DailyTrendingTopicsOptions, RealTimeTrendsOptions, ExploreOptions, ExploreResponse, InterestByRegionOptions, InterestByRegionResponse } from '../types/index';
|
|
1
|
+
import { DailyTrendingTopics, DailyTrendingTopicsOptions, RealTimeTrendsOptions, ExploreOptions, ExploreResponse, InterestByRegionOptions, InterestByRegionResponse, GoogleTrendsResponse } from '../types/index';
|
|
2
2
|
export declare class GoogleTrendsApi {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
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>>;
|
|
6
22
|
explore({ keyword, geo, time, category, property, hl, }: ExploreOptions): Promise<ExploreResponse>;
|
|
7
23
|
interestByRegion({ keyword, startTime, endTime, geo, resolution, hl, timezone, category }: InterestByRegionOptions): Promise<InterestByRegionResponse>;
|
|
8
24
|
}
|
|
@@ -13,11 +13,18 @@ exports.GoogleTrendsApi = void 0;
|
|
|
13
13
|
const request_1 = require("./request");
|
|
14
14
|
const format_1 = require("./format");
|
|
15
15
|
const constants_1 = require("../constants");
|
|
16
|
+
const GoogleTrendsError_1 = require("../errors/GoogleTrendsError");
|
|
16
17
|
class GoogleTrendsApi {
|
|
18
|
+
/**
|
|
19
|
+
* Get autocomplete suggestions for a keyword
|
|
20
|
+
* @param keyword - The keyword to get suggestions for
|
|
21
|
+
* @param hl - Language code (default: 'en-US')
|
|
22
|
+
* @returns Promise with array of suggestion strings
|
|
23
|
+
*/
|
|
17
24
|
autocomplete(keyword, hl = 'en-US') {
|
|
18
25
|
return __awaiter(this, void 0, void 0, function* () {
|
|
19
26
|
if (!keyword) {
|
|
20
|
-
return [];
|
|
27
|
+
return { data: [] };
|
|
21
28
|
}
|
|
22
29
|
const options = Object.assign(Object.assign({}, constants_1.GOOGLE_TRENDS_MAPPER["autocomplete" /* GoogleTrendsEndpoints.autocomplete */]), { qs: {
|
|
23
30
|
hl,
|
|
@@ -28,69 +35,73 @@ class GoogleTrendsApi {
|
|
|
28
35
|
const text = yield response.text();
|
|
29
36
|
// Remove the first 5 characters (JSONP wrapper) and parse
|
|
30
37
|
const data = JSON.parse(text.slice(5));
|
|
31
|
-
return data.default.topics.map((topic) => topic.title);
|
|
38
|
+
return { data: data.default.topics.map((topic) => topic.title) };
|
|
32
39
|
}
|
|
33
40
|
catch (error) {
|
|
34
|
-
|
|
35
|
-
|
|
41
|
+
if (error instanceof Error) {
|
|
42
|
+
return { error: new GoogleTrendsError_1.NetworkError(error.message) };
|
|
43
|
+
}
|
|
44
|
+
return { error: new GoogleTrendsError_1.UnknownError() };
|
|
36
45
|
}
|
|
37
46
|
});
|
|
38
47
|
}
|
|
48
|
+
/**
|
|
49
|
+
* Get daily trending topics
|
|
50
|
+
* @param options - Options for daily trends request
|
|
51
|
+
* @returns Promise with trending topics data
|
|
52
|
+
*/
|
|
39
53
|
dailyTrends({ geo = 'US', lang = 'en' }) {
|
|
40
54
|
return __awaiter(this, void 0, void 0, function* () {
|
|
41
55
|
const defaultOptions = constants_1.GOOGLE_TRENDS_MAPPER["dailyTrends" /* GoogleTrendsEndpoints.dailyTrends */];
|
|
42
56
|
const options = Object.assign(Object.assign({}, defaultOptions), { body: new URLSearchParams({
|
|
43
57
|
'f.req': `[[["i0OFE","[null,null,\\"${geo}\\",0,\\"${lang}\\",24,1]",null,"generic"]]]`,
|
|
44
|
-
}).toString() });
|
|
58
|
+
}).toString(), contentType: 'form' });
|
|
45
59
|
try {
|
|
46
60
|
const response = yield (0, request_1.request)(options.url, options);
|
|
47
61
|
const text = yield response.text();
|
|
48
62
|
const trendingTopics = (0, format_1.extractJsonFromResponse)(text);
|
|
49
63
|
if (!trendingTopics) {
|
|
50
|
-
return {
|
|
51
|
-
allTrendingStories: [],
|
|
52
|
-
summary: [],
|
|
53
|
-
};
|
|
64
|
+
return { error: new GoogleTrendsError_1.ParseError() };
|
|
54
65
|
}
|
|
55
|
-
return trendingTopics;
|
|
66
|
+
return { data: trendingTopics };
|
|
56
67
|
}
|
|
57
68
|
catch (error) {
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
};
|
|
69
|
+
if (error instanceof Error) {
|
|
70
|
+
return { error: new GoogleTrendsError_1.NetworkError(error.message) };
|
|
71
|
+
}
|
|
72
|
+
return { error: new GoogleTrendsError_1.UnknownError() };
|
|
63
73
|
}
|
|
64
74
|
});
|
|
65
75
|
}
|
|
76
|
+
/**
|
|
77
|
+
* Get real-time trending topics
|
|
78
|
+
* @param options - Options for real-time trends request
|
|
79
|
+
* @returns Promise with trending topics data
|
|
80
|
+
*/
|
|
66
81
|
realTimeTrends({ geo = 'US', trendingHours = 4 }) {
|
|
67
82
|
return __awaiter(this, void 0, void 0, function* () {
|
|
68
83
|
const defaultOptions = constants_1.GOOGLE_TRENDS_MAPPER["dailyTrends" /* GoogleTrendsEndpoints.dailyTrends */];
|
|
69
84
|
const options = Object.assign(Object.assign({}, defaultOptions), { body: new URLSearchParams({
|
|
70
85
|
'f.req': `[[["i0OFE","[null,null,\\"${geo}\\",0,\\"en\\",${trendingHours},1]",null,"generic"]]]`,
|
|
71
|
-
}).toString() });
|
|
86
|
+
}).toString(), contentType: 'form' });
|
|
72
87
|
try {
|
|
73
88
|
const response = yield (0, request_1.request)(options.url, options);
|
|
74
89
|
const text = yield response.text();
|
|
75
90
|
const trendingTopics = (0, format_1.extractJsonFromResponse)(text);
|
|
76
91
|
if (!trendingTopics) {
|
|
77
|
-
return {
|
|
78
|
-
allTrendingStories: [],
|
|
79
|
-
summary: [],
|
|
80
|
-
};
|
|
92
|
+
return { error: new GoogleTrendsError_1.ParseError() };
|
|
81
93
|
}
|
|
82
|
-
return trendingTopics;
|
|
94
|
+
return { data: trendingTopics };
|
|
83
95
|
}
|
|
84
96
|
catch (error) {
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
};
|
|
97
|
+
if (error instanceof Error) {
|
|
98
|
+
return { error: new GoogleTrendsError_1.NetworkError(error.message) };
|
|
99
|
+
}
|
|
100
|
+
return { error: new GoogleTrendsError_1.UnknownError() };
|
|
90
101
|
}
|
|
91
102
|
});
|
|
92
103
|
}
|
|
93
|
-
explore({ keyword, geo = 'US', time = '
|
|
104
|
+
explore({ keyword, geo = 'US', time = 'now 1-d', category = 0, property = '', hl = 'en-US', }) {
|
|
94
105
|
return __awaiter(this, void 0, void 0, function* () {
|
|
95
106
|
const options = Object.assign(Object.assign({}, constants_1.GOOGLE_TRENDS_MAPPER["explore" /* GoogleTrendsEndpoints.explore */]), { qs: {
|
|
96
107
|
hl,
|
|
@@ -106,7 +117,7 @@ class GoogleTrendsApi {
|
|
|
106
117
|
category,
|
|
107
118
|
property,
|
|
108
119
|
}),
|
|
109
|
-
} });
|
|
120
|
+
}, contentType: 'form' });
|
|
110
121
|
try {
|
|
111
122
|
const response = yield (0, request_1.request)(options.url, options);
|
|
112
123
|
const text = yield response.text();
|
package/lib/helpers/request.d.ts
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
export declare const request: (url: string, options: {
|
|
2
2
|
method?: string;
|
|
3
3
|
qs?: Record<string, any>;
|
|
4
|
-
body?: string
|
|
5
|
-
|
|
4
|
+
body?: string | Record<string, any>;
|
|
5
|
+
headers?: Record<string, string>;
|
|
6
|
+
contentType?: 'json' | 'form';
|
|
7
|
+
}) => Promise<{
|
|
8
|
+
text: () => Promise<string>;
|
|
9
|
+
}>;
|
package/lib/helpers/request.js
CHANGED
|
@@ -16,46 +16,53 @@ exports.request = void 0;
|
|
|
16
16
|
const https_1 = __importDefault(require("https"));
|
|
17
17
|
const querystring_1 = __importDefault(require("querystring"));
|
|
18
18
|
let cookieVal;
|
|
19
|
-
function rereq(options) {
|
|
19
|
+
function rereq(options, body) {
|
|
20
20
|
return new Promise((resolve, reject) => {
|
|
21
21
|
const req = https_1.default.request(options, (res) => {
|
|
22
22
|
let chunk = '';
|
|
23
|
-
res.on('data', (data) => {
|
|
24
|
-
|
|
25
|
-
});
|
|
26
|
-
res.on('end', () => {
|
|
27
|
-
resolve(chunk.toString());
|
|
28
|
-
});
|
|
29
|
-
});
|
|
30
|
-
req.on('error', (e) => {
|
|
31
|
-
reject(e);
|
|
23
|
+
res.on('data', (data) => { chunk += data; });
|
|
24
|
+
res.on('end', () => resolve(chunk));
|
|
32
25
|
});
|
|
26
|
+
req.on('error', reject);
|
|
27
|
+
if (body)
|
|
28
|
+
req.write(body);
|
|
33
29
|
req.end();
|
|
34
30
|
});
|
|
35
31
|
}
|
|
36
32
|
const request = (url, options) => __awaiter(void 0, void 0, void 0, function* () {
|
|
37
33
|
const parsedUrl = new URL(url);
|
|
34
|
+
const method = options.method || 'POST';
|
|
35
|
+
// Prepare body
|
|
36
|
+
let bodyString = '';
|
|
37
|
+
const contentType = options.contentType || 'json';
|
|
38
|
+
if (typeof options.body === 'string') {
|
|
39
|
+
bodyString = options.body;
|
|
40
|
+
}
|
|
41
|
+
else if (contentType === 'form') {
|
|
42
|
+
bodyString = querystring_1.default.stringify(options.body || {});
|
|
43
|
+
}
|
|
44
|
+
else if (options.body) {
|
|
45
|
+
bodyString = JSON.stringify(options.body);
|
|
46
|
+
}
|
|
38
47
|
const requestOptions = {
|
|
39
|
-
|
|
40
|
-
|
|
48
|
+
hostname: parsedUrl.hostname,
|
|
49
|
+
port: parsedUrl.port || 443,
|
|
41
50
|
path: `${parsedUrl.pathname}${options.qs ? '?' + querystring_1.default.stringify(options.qs) : ''}`,
|
|
42
|
-
|
|
51
|
+
method,
|
|
52
|
+
headers: Object.assign(Object.assign(Object.assign(Object.assign({}, (options.headers || {})), (contentType === 'form'
|
|
53
|
+
? { 'Content-Type': 'application/x-www-form-urlencoded' }
|
|
54
|
+
: { 'Content-Type': 'application/json' })), (bodyString ? { 'Content-Length': Buffer.byteLength(bodyString).toString() } : {})), (cookieVal ? { cookie: cookieVal } : {}))
|
|
43
55
|
};
|
|
44
|
-
if (cookieVal) {
|
|
45
|
-
requestOptions.headers = { 'cookie': cookieVal };
|
|
46
|
-
}
|
|
47
56
|
const response = yield new Promise((resolve, reject) => {
|
|
48
57
|
const req = https_1.default.request(requestOptions, (res) => {
|
|
49
58
|
let chunk = '';
|
|
50
|
-
res.on('data', (data) => {
|
|
51
|
-
chunk += data;
|
|
52
|
-
});
|
|
59
|
+
res.on('data', (data) => { chunk += data; });
|
|
53
60
|
res.on('end', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
54
61
|
if (res.statusCode === 429 && res.headers['set-cookie']) {
|
|
55
62
|
cookieVal = res.headers['set-cookie'][0].split(';')[0];
|
|
56
|
-
requestOptions.headers
|
|
63
|
+
requestOptions.headers['cookie'] = cookieVal;
|
|
57
64
|
try {
|
|
58
|
-
const retryResponse = yield rereq(requestOptions);
|
|
65
|
+
const retryResponse = yield rereq(requestOptions, bodyString);
|
|
59
66
|
resolve(retryResponse);
|
|
60
67
|
}
|
|
61
68
|
catch (err) {
|
|
@@ -63,16 +70,13 @@ const request = (url, options) => __awaiter(void 0, void 0, void 0, function* ()
|
|
|
63
70
|
}
|
|
64
71
|
}
|
|
65
72
|
else {
|
|
66
|
-
resolve(chunk
|
|
73
|
+
resolve(chunk);
|
|
67
74
|
}
|
|
68
75
|
}));
|
|
69
76
|
});
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
req.on('error', (e) => {
|
|
74
|
-
reject(e);
|
|
75
|
-
});
|
|
77
|
+
req.on('error', reject);
|
|
78
|
+
if (bodyString)
|
|
79
|
+
req.write(bodyString);
|
|
76
80
|
req.end();
|
|
77
81
|
});
|
|
78
82
|
return {
|