@shaivpidadi/trends-js 0.0.0-beta.2
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/LICENSE.md +7 -0
- package/README.md +0 -0
- package/lib/constants.d.ts +2 -0
- package/lib/constants.js +38 -0
- package/lib/helpers/format.d.ts +2 -0
- package/lib/helpers/format.js +53 -0
- package/lib/helpers/googleTrendsAPI.d.ts +10 -0
- package/lib/helpers/googleTrendsAPI.js +177 -0
- package/lib/helpers/request.d.ts +1 -0
- package/lib/helpers/request.js +17 -0
- package/lib/index.d.ts +2 -0
- package/lib/index.js +7 -0
- package/package.json +49 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
Copyright 2025 Hebert Cisco
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
4
|
+
|
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
6
|
+
|
|
7
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
File without changes
|
package/lib/constants.js
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.GOOGLE_TRENDS_MAPPER = void 0;
|
|
4
|
+
const GOOGLE_TRENDS_BASE_URL = 'trends.google.com';
|
|
5
|
+
exports.GOOGLE_TRENDS_MAPPER = {
|
|
6
|
+
["dailyTrends" /* GoogleTrendsEndpoints.dailyTrends */]: {
|
|
7
|
+
path: '/_/TrendsUi/data/batchexecute',
|
|
8
|
+
method: 'POST',
|
|
9
|
+
host: GOOGLE_TRENDS_BASE_URL,
|
|
10
|
+
url: `https://${GOOGLE_TRENDS_BASE_URL}/_/TrendsUi/data/batchexecute`,
|
|
11
|
+
headers: {
|
|
12
|
+
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
|
|
13
|
+
},
|
|
14
|
+
},
|
|
15
|
+
["autocomplete" /* GoogleTrendsEndpoints.autocomplete */]: {
|
|
16
|
+
path: '/trends/api/autocomplete',
|
|
17
|
+
method: 'GET',
|
|
18
|
+
host: GOOGLE_TRENDS_BASE_URL,
|
|
19
|
+
url: `https://${GOOGLE_TRENDS_BASE_URL}/trends/api/autocomplete`,
|
|
20
|
+
headers: {
|
|
21
|
+
accept: 'application/json, text/plain, */*',
|
|
22
|
+
},
|
|
23
|
+
},
|
|
24
|
+
["explore" /* GoogleTrendsEndpoints.explore */]: {
|
|
25
|
+
path: '/trends/api/explore',
|
|
26
|
+
method: 'GET',
|
|
27
|
+
host: GOOGLE_TRENDS_BASE_URL,
|
|
28
|
+
url: `https://${GOOGLE_TRENDS_BASE_URL}/trends/api/explore`,
|
|
29
|
+
headers: {},
|
|
30
|
+
},
|
|
31
|
+
["interestByRegion" /* GoogleTrendsEndpoints.interestByRegion */]: {
|
|
32
|
+
path: '/trends/api/widgetdata/comparedgeo',
|
|
33
|
+
method: 'GET',
|
|
34
|
+
host: GOOGLE_TRENDS_BASE_URL,
|
|
35
|
+
url: `https://${GOOGLE_TRENDS_BASE_URL}/trends/api/widgetdata/comparedgeo`,
|
|
36
|
+
headers: {},
|
|
37
|
+
},
|
|
38
|
+
};
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.extractJsonFromResponse = void 0;
|
|
4
|
+
// For future refrence and update: from google trends page rpc call response,
|
|
5
|
+
// 0 "twitter down" The main trending search term.
|
|
6
|
+
// 1 null Unused (reserved for future Google Trends data).
|
|
7
|
+
// 2 "US" Country code (where the trend is happening).
|
|
8
|
+
// 3 [1741599600] Unix timestamp (represents when the search started trending).
|
|
9
|
+
// 4 null Unused (reserved for future data).
|
|
10
|
+
// 5 null Unused (reserved for future data).
|
|
11
|
+
// 6 500000 Search volume index (estimated search interest for the term).
|
|
12
|
+
// 7 null Unused (reserved for future data).
|
|
13
|
+
// 8 1000 Trend ranking score (higher means more popular).
|
|
14
|
+
// 9 ["twitter down", "is twitter down", "is x down", ...] Related searches (other queries that users searched alongside this term).
|
|
15
|
+
// 10 [11] Unclear, possibly a category identifier.
|
|
16
|
+
// 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).
|
|
17
|
+
// 12 "twitter down" The original trending keyword (sometimes a duplicate of index 0).
|
|
18
|
+
const extractJsonFromResponse = (text) => {
|
|
19
|
+
const cleanedText = text.replace(/^\)\]\}'/, '').trim();
|
|
20
|
+
try {
|
|
21
|
+
const parsedResponse = JSON.parse(cleanedText);
|
|
22
|
+
if (!Array.isArray(parsedResponse) || parsedResponse.length === 0) {
|
|
23
|
+
return null;
|
|
24
|
+
}
|
|
25
|
+
const nestedJsonString = parsedResponse[0][2];
|
|
26
|
+
if (!nestedJsonString) {
|
|
27
|
+
return null;
|
|
28
|
+
}
|
|
29
|
+
const data = JSON.parse(nestedJsonString);
|
|
30
|
+
if (!data || !Array.isArray(data) || data.length < 2) {
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
return updateResponseObject(data[1]);
|
|
34
|
+
}
|
|
35
|
+
catch (e) {
|
|
36
|
+
console.error('Failed to parse response:', e);
|
|
37
|
+
return null;
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
exports.extractJsonFromResponse = extractJsonFromResponse;
|
|
41
|
+
const updateResponseObject = (data) => {
|
|
42
|
+
if (!Array.isArray(data)) {
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
const allTrendingStories = data;
|
|
46
|
+
const summary = [];
|
|
47
|
+
data.forEach((item) => {
|
|
48
|
+
if (Array.isArray(item) && typeof item[0] === 'string') {
|
|
49
|
+
summary.push(item[0]); // First element is usually the trending keyword
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
return { allTrendingStories, summary };
|
|
53
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { DailyTrendingTopics, DailyTrendingTopicsOptions, RealTimeTrendsOptions, ExploreOptions, ExploreResponse, InterestByRegionOptions, InterestByRegionResponse } from '../types/index';
|
|
2
|
+
export declare class GoogleTrendsApi {
|
|
3
|
+
autocomplete(keyword: string, hl?: string): Promise<string[]>;
|
|
4
|
+
dailyTrends({ geo, lang }: DailyTrendingTopicsOptions): Promise<DailyTrendingTopics>;
|
|
5
|
+
realTimeTrends({ geo, trendingHours }: RealTimeTrendsOptions): Promise<DailyTrendingTopics>;
|
|
6
|
+
explore({ keyword, geo, time, category, property, hl, }: ExploreOptions): Promise<ExploreResponse>;
|
|
7
|
+
interestByRegion({ keyword, geo, time, resolution, hl, }: InterestByRegionOptions): Promise<InterestByRegionResponse>;
|
|
8
|
+
}
|
|
9
|
+
declare const _default: GoogleTrendsApi;
|
|
10
|
+
export default _default;
|
|
@@ -0,0 +1,177 @@
|
|
|
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() });
|
|
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() });
|
|
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
|
+
interestByRegion({ keyword, geo = 'US', time = 'today 12-m', resolution = 'REGION', hl = 'en-US', }) {
|
|
124
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
125
|
+
// First get the widget token from explore
|
|
126
|
+
const exploreResponse = yield this.explore({ keyword, geo, time, hl });
|
|
127
|
+
const widget = exploreResponse.widgets.find((w) => w.id === 'GEO_MAP_0');
|
|
128
|
+
if (!widget) {
|
|
129
|
+
return { default: { geoMapData: [] } };
|
|
130
|
+
}
|
|
131
|
+
const options = Object.assign(Object.assign({}, constants_1.GOOGLE_TRENDS_MAPPER["interestByRegion" /* GoogleTrendsEndpoints.interestByRegion */]), { qs: {
|
|
132
|
+
hl,
|
|
133
|
+
tz: '240',
|
|
134
|
+
req: JSON.stringify({
|
|
135
|
+
geo: { country: geo },
|
|
136
|
+
comparisonItem: [
|
|
137
|
+
{
|
|
138
|
+
time,
|
|
139
|
+
complexKeywordsRestriction: {
|
|
140
|
+
keyword: [
|
|
141
|
+
{
|
|
142
|
+
type: 'BROAD',
|
|
143
|
+
value: keyword,
|
|
144
|
+
},
|
|
145
|
+
],
|
|
146
|
+
},
|
|
147
|
+
},
|
|
148
|
+
],
|
|
149
|
+
resolution,
|
|
150
|
+
locale: hl,
|
|
151
|
+
requestOptions: {
|
|
152
|
+
property: '',
|
|
153
|
+
backend: 'IZG',
|
|
154
|
+
category: 0,
|
|
155
|
+
},
|
|
156
|
+
userConfig: {
|
|
157
|
+
userType: 'USER_TYPE_SCRAPER',
|
|
158
|
+
},
|
|
159
|
+
}),
|
|
160
|
+
token: widget.token,
|
|
161
|
+
} });
|
|
162
|
+
try {
|
|
163
|
+
const response = yield (0, request_1.request)(options.url, options);
|
|
164
|
+
const text = yield response.text();
|
|
165
|
+
// Remove the first 5 characters (JSONP wrapper) and parse
|
|
166
|
+
const data = JSON.parse(text.slice(5));
|
|
167
|
+
return data;
|
|
168
|
+
}
|
|
169
|
+
catch (error) {
|
|
170
|
+
console.error('Interest by region request failed:', error);
|
|
171
|
+
return { default: { geoMapData: [] } };
|
|
172
|
+
}
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
exports.GoogleTrendsApi = GoogleTrendsApi;
|
|
177
|
+
exports.default = new GoogleTrendsApi();
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const request: (url: string, options: RequestInit) => Promise<any>;
|
|
@@ -0,0 +1,17 @@
|
|
|
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.request = void 0;
|
|
13
|
+
const request = (url, options) => __awaiter(void 0, void 0, void 0, function* () {
|
|
14
|
+
const response = yield fetch(url, options);
|
|
15
|
+
return response;
|
|
16
|
+
});
|
|
17
|
+
exports.request = request;
|
package/lib/index.d.ts
ADDED
package/lib/index.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
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;
|
package/package.json
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@shaivpidadi/trends-js",
|
|
3
|
+
"version": "0.0.0-beta.2",
|
|
4
|
+
"description": "Google Trends API for Node.js",
|
|
5
|
+
"main": "lib/index.js",
|
|
6
|
+
"types": "lib/index.d.ts",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"build": "tsc",
|
|
9
|
+
"format": "prettier --write \"src/**/*.(js|ts)\"",
|
|
10
|
+
"lint": "eslint src --ext .js,.ts",
|
|
11
|
+
"lint:fix": "eslint src --fix --ext .js,.ts",
|
|
12
|
+
"test": "jest --config jest.config.js",
|
|
13
|
+
"prepare": "npm run build",
|
|
14
|
+
"prepublishOnly": "npm test && npm run lint",
|
|
15
|
+
"preversion": "npm run lint",
|
|
16
|
+
"version": "npm run format && git add -A src",
|
|
17
|
+
"postversion": "git push && git push --tags"
|
|
18
|
+
},
|
|
19
|
+
"repository": {
|
|
20
|
+
"type": "git",
|
|
21
|
+
"url": "git+https://github.com/Shaivpidadi/trends-js.git"
|
|
22
|
+
},
|
|
23
|
+
"keywords": [
|
|
24
|
+
"boilerplate",
|
|
25
|
+
"typescript",
|
|
26
|
+
"npm",
|
|
27
|
+
"module"
|
|
28
|
+
],
|
|
29
|
+
"author": "Shaishav Pidadi",
|
|
30
|
+
"private": false,
|
|
31
|
+
"publishConfig": {
|
|
32
|
+
"access": "public"
|
|
33
|
+
},
|
|
34
|
+
"license": "MIT",
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"@types/jest": "29.5.14",
|
|
37
|
+
"@typescript-eslint/eslint-plugin": "5.62.0",
|
|
38
|
+
"@typescript-eslint/parser": "5.62.0",
|
|
39
|
+
"eslint": "8.57.1",
|
|
40
|
+
"eslint-plugin-jest": "27.9.0",
|
|
41
|
+
"jest": "29.7.0",
|
|
42
|
+
"prettier": "2.8.8",
|
|
43
|
+
"ts-jest": "29.2.5",
|
|
44
|
+
"typescript": "4.9.5"
|
|
45
|
+
},
|
|
46
|
+
"files": [
|
|
47
|
+
"lib/**/*"
|
|
48
|
+
]
|
|
49
|
+
}
|