@dsottimano/trendspy-js
Version:
ALPHA VERSION: JavaScript port of trendspy - A library for analyzing Google Trends data
863 lines (846 loc) • 67 kB
JavaScript
"use strict";
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
function _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _iterableToArray(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); }
function _arrayWithoutHoles(r) { if (Array.isArray(r)) return _arrayLikeToArray(r); }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
function _regeneratorRuntime() { "use strict"; /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */ _regeneratorRuntime = function _regeneratorRuntime() { return e; }; var t, e = {}, r = Object.prototype, n = r.hasOwnProperty, o = Object.defineProperty || function (t, e, r) { t[e] = r.value; }, i = "function" == typeof Symbol ? Symbol : {}, a = i.iterator || "@@iterator", c = i.asyncIterator || "@@asyncIterator", u = i.toStringTag || "@@toStringTag"; function define(t, e, r) { return Object.defineProperty(t, e, { value: r, enumerable: !0, configurable: !0, writable: !0 }), t[e]; } try { define({}, ""); } catch (t) { define = function define(t, e, r) { return t[e] = r; }; } function wrap(t, e, r, n) { var i = e && e.prototype instanceof Generator ? e : Generator, a = Object.create(i.prototype), c = new Context(n || []); return o(a, "_invoke", { value: makeInvokeMethod(t, r, c) }), a; } function tryCatch(t, e, r) { try { return { type: "normal", arg: t.call(e, r) }; } catch (t) { return { type: "throw", arg: t }; } } e.wrap = wrap; var h = "suspendedStart", l = "suspendedYield", f = "executing", s = "completed", y = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} var p = {}; define(p, a, function () { return this; }); var d = Object.getPrototypeOf, v = d && d(d(values([]))); v && v !== r && n.call(v, a) && (p = v); var g = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(p); function defineIteratorMethods(t) { ["next", "throw", "return"].forEach(function (e) { define(t, e, function (t) { return this._invoke(e, t); }); }); } function AsyncIterator(t, e) { function invoke(r, o, i, a) { var c = tryCatch(t[r], t, o); if ("throw" !== c.type) { var u = c.arg, h = u.value; return h && "object" == _typeof(h) && n.call(h, "__await") ? e.resolve(h.__await).then(function (t) { invoke("next", t, i, a); }, function (t) { invoke("throw", t, i, a); }) : e.resolve(h).then(function (t) { u.value = t, i(u); }, function (t) { return invoke("throw", t, i, a); }); } a(c.arg); } var r; o(this, "_invoke", { value: function value(t, n) { function callInvokeWithMethodAndArg() { return new e(function (e, r) { invoke(t, n, e, r); }); } return r = r ? r.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg(); } }); } function makeInvokeMethod(e, r, n) { var o = h; return function (i, a) { if (o === f) throw Error("Generator is already running"); if (o === s) { if ("throw" === i) throw a; return { value: t, done: !0 }; } for (n.method = i, n.arg = a;;) { var c = n.delegate; if (c) { var u = maybeInvokeDelegate(c, n); if (u) { if (u === y) continue; return u; } } if ("next" === n.method) n.sent = n._sent = n.arg;else if ("throw" === n.method) { if (o === h) throw o = s, n.arg; n.dispatchException(n.arg); } else "return" === n.method && n.abrupt("return", n.arg); o = f; var p = tryCatch(e, r, n); if ("normal" === p.type) { if (o = n.done ? s : l, p.arg === y) continue; return { value: p.arg, done: n.done }; } "throw" === p.type && (o = s, n.method = "throw", n.arg = p.arg); } }; } function maybeInvokeDelegate(e, r) { var n = r.method, o = e.iterator[n]; if (o === t) return r.delegate = null, "throw" === n && e.iterator["return"] && (r.method = "return", r.arg = t, maybeInvokeDelegate(e, r), "throw" === r.method) || "return" !== n && (r.method = "throw", r.arg = new TypeError("The iterator does not provide a '" + n + "' method")), y; var i = tryCatch(o, e.iterator, r.arg); if ("throw" === i.type) return r.method = "throw", r.arg = i.arg, r.delegate = null, y; var a = i.arg; return a ? a.done ? (r[e.resultName] = a.value, r.next = e.nextLoc, "return" !== r.method && (r.method = "next", r.arg = t), r.delegate = null, y) : a : (r.method = "throw", r.arg = new TypeError("iterator result is not an object"), r.delegate = null, y); } function pushTryEntry(t) { var e = { tryLoc: t[0] }; 1 in t && (e.catchLoc = t[1]), 2 in t && (e.finallyLoc = t[2], e.afterLoc = t[3]), this.tryEntries.push(e); } function resetTryEntry(t) { var e = t.completion || {}; e.type = "normal", delete e.arg, t.completion = e; } function Context(t) { this.tryEntries = [{ tryLoc: "root" }], t.forEach(pushTryEntry, this), this.reset(!0); } function values(e) { if (e || "" === e) { var r = e[a]; if (r) return r.call(e); if ("function" == typeof e.next) return e; if (!isNaN(e.length)) { var o = -1, i = function next() { for (; ++o < e.length;) if (n.call(e, o)) return next.value = e[o], next.done = !1, next; return next.value = t, next.done = !0, next; }; return i.next = i; } } throw new TypeError(_typeof(e) + " is not iterable"); } return GeneratorFunction.prototype = GeneratorFunctionPrototype, o(g, "constructor", { value: GeneratorFunctionPrototype, configurable: !0 }), o(GeneratorFunctionPrototype, "constructor", { value: GeneratorFunction, configurable: !0 }), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, u, "GeneratorFunction"), e.isGeneratorFunction = function (t) { var e = "function" == typeof t && t.constructor; return !!e && (e === GeneratorFunction || "GeneratorFunction" === (e.displayName || e.name)); }, e.mark = function (t) { return Object.setPrototypeOf ? Object.setPrototypeOf(t, GeneratorFunctionPrototype) : (t.__proto__ = GeneratorFunctionPrototype, define(t, u, "GeneratorFunction")), t.prototype = Object.create(g), t; }, e.awrap = function (t) { return { __await: t }; }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, c, function () { return this; }), e.AsyncIterator = AsyncIterator, e.async = function (t, r, n, o, i) { void 0 === i && (i = Promise); var a = new AsyncIterator(wrap(t, r, n, o), i); return e.isGeneratorFunction(r) ? a : a.next().then(function (t) { return t.done ? t.value : a.next(); }); }, defineIteratorMethods(g), define(g, u, "Generator"), define(g, a, function () { return this; }), define(g, "toString", function () { return "[object Generator]"; }), e.keys = function (t) { var e = Object(t), r = []; for (var n in e) r.push(n); return r.reverse(), function next() { for (; r.length;) { var t = r.pop(); if (t in e) return next.value = t, next.done = !1, next; } return next.done = !0, next; }; }, e.values = values, Context.prototype = { constructor: Context, reset: function reset(e) { if (this.prev = 0, this.next = 0, this.sent = this._sent = t, this.done = !1, this.delegate = null, this.method = "next", this.arg = t, this.tryEntries.forEach(resetTryEntry), !e) for (var r in this) "t" === r.charAt(0) && n.call(this, r) && !isNaN(+r.slice(1)) && (this[r] = t); }, stop: function stop() { this.done = !0; var t = this.tryEntries[0].completion; if ("throw" === t.type) throw t.arg; return this.rval; }, dispatchException: function dispatchException(e) { if (this.done) throw e; var r = this; function handle(n, o) { return a.type = "throw", a.arg = e, r.next = n, o && (r.method = "next", r.arg = t), !!o; } for (var o = this.tryEntries.length - 1; o >= 0; --o) { var i = this.tryEntries[o], a = i.completion; if ("root" === i.tryLoc) return handle("end"); if (i.tryLoc <= this.prev) { var c = n.call(i, "catchLoc"), u = n.call(i, "finallyLoc"); if (c && u) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } else if (c) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); } else { if (!u) throw Error("try statement without catch or finally"); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } } } }, abrupt: function abrupt(t, e) { for (var r = this.tryEntries.length - 1; r >= 0; --r) { var o = this.tryEntries[r]; if (o.tryLoc <= this.prev && n.call(o, "finallyLoc") && this.prev < o.finallyLoc) { var i = o; break; } } i && ("break" === t || "continue" === t) && i.tryLoc <= e && e <= i.finallyLoc && (i = null); var a = i ? i.completion : {}; return a.type = t, a.arg = e, i ? (this.method = "next", this.next = i.finallyLoc, y) : this.complete(a); }, complete: function complete(t, e) { if ("throw" === t.type) throw t.arg; return "break" === t.type || "continue" === t.type ? this.next = t.arg : "return" === t.type ? (this.rval = this.arg = t.arg, this.method = "return", this.next = "end") : "normal" === t.type && e && (this.next = e), y; }, finish: function finish(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.finallyLoc === t) return this.complete(r.completion, r.afterLoc), resetTryEntry(r), y; } }, "catch": function _catch(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.tryLoc === t) { var n = r.completion; if ("throw" === n.type) { var o = n.arg; resetTryEntry(r); } return o; } } throw Error("illegal catch attempt"); }, delegateYield: function delegateYield(e, r, n) { return this.delegate = { iterator: values(e), resultName: r, nextLoc: n }, "next" === this.method && (this.arg = t), y; } }, e; }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function asyncGeneratorStep(n, t, e, r, o, a, c) { try { var i = n[a](c), u = i.value; } catch (n) { return void e(n); } i.done ? t(u) : Promise.resolve(u).then(r, o); }
function _asyncToGenerator(n) { return function () { var t = this, e = arguments; return new Promise(function (r, o) { var a = n.apply(t, e); function _next(n) { asyncGeneratorStep(a, r, o, _next, _throw, "next", n); } function _throw(n) { asyncGeneratorStep(a, r, o, _next, _throw, "throw", n); } _next(void 0); }); }; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); }
function _possibleConstructorReturn(t, e) { if (e && ("object" == _typeof(e) || "function" == typeof e)) return e; if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined"); return _assertThisInitialized(t); }
function _assertThisInitialized(e) { if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); return e; }
function _inherits(t, e) { if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function"); t.prototype = Object.create(e && e.prototype, { constructor: { value: t, writable: !0, configurable: !0 } }), Object.defineProperty(t, "prototype", { writable: !1 }), e && _setPrototypeOf(t, e); }
function _wrapNativeSuper(t) { var r = "function" == typeof Map ? new Map() : void 0; return _wrapNativeSuper = function _wrapNativeSuper(t) { if (null === t || !_isNativeFunction(t)) return t; if ("function" != typeof t) throw new TypeError("Super expression must either be null or a function"); if (void 0 !== r) { if (r.has(t)) return r.get(t); r.set(t, Wrapper); } function Wrapper() { return _construct(t, arguments, _getPrototypeOf(this).constructor); } return Wrapper.prototype = Object.create(t.prototype, { constructor: { value: Wrapper, enumerable: !1, writable: !0, configurable: !0 } }), _setPrototypeOf(Wrapper, t); }, _wrapNativeSuper(t); }
function _construct(t, e, r) { if (_isNativeReflectConstruct()) return Reflect.construct.apply(null, arguments); var o = [null]; o.push.apply(o, e); var p = new (t.bind.apply(t, o))(); return r && _setPrototypeOf(p, r.prototype), p; }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
function _isNativeFunction(t) { try { return -1 !== Function.toString.call(t).indexOf("[native code]"); } catch (n) { return "function" == typeof t; } }
function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); }
function _getPrototypeOf(t) { return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) { return t.__proto__ || Object.getPrototypeOf(t); }, _getPrototypeOf(t); }
var axios = require('axios');
var _require = require('luxon'),
DateTime = _require.DateTime;
var _require2 = require('url'),
URLSearchParams = _require2.URLSearchParams;
var _require3 = require('./utils'),
ensureList = _require3.ensureList,
decodeEscapeText = _require3.decodeEscapeText,
convertTimeframe = _require3.convertTimeframe,
checkTimeframeResolution = _require3.checkTimeframeResolution;
var TrendsDataConverter = require('./converter');
var tough = require('tough-cookie');
var _require4 = require('axios-cookiejar-support'),
wrapper = _require4.wrapper;
// Constants
var BATCH_URL = 'https://trends.google.com/_/TrendsUi/data/batchexecute';
var HOT_TRENDS_URL = 'https://trends.google.com/trends/hottrends/visualize/internal/data';
// API Links
var API_URL = 'https://trends.google.com/trends/api';
var API_EXPLORE_URL = "".concat(API_URL, "/explore");
var API_GEO_DATA_URL = "".concat(API_URL, "/explore/pickers/geo");
var API_CATEGORY_URL = "".concat(API_URL, "/explore/pickers/category");
var API_TOPCHARTS_URL = "".concat(API_URL, "/topcharts");
var API_AUTOCOMPLETE = "".concat(API_URL, "/autocomplete/");
var DAILY_SEARCHES_URL = "".concat(API_URL, "/dailytrends");
var REALTIME_SEARCHES_URL = "".concat(API_URL, "/realtimetrends");
var API_TOKEN_URL = 'https://trends.google.com/trends/api/widgetdata';
var API_TIMELINE_URL = "".concat(API_TOKEN_URL, "/multiline");
var API_MULTIRANGE_URL = "".concat(API_TOKEN_URL, "/multirange");
var API_GEO_URL = "".concat(API_TOKEN_URL, "/comparedgeo");
var API_RELATED_QUERIES_URL = "".concat(API_TOKEN_URL, "/relatedsearches");
// Embed Links
var EMBED_URL = 'https://trends.google.com/trends/embed/explore';
var EMBED_GEO_URL = "".concat(EMBED_URL, "/GEO_MAP");
var EMBED_TOPICS_URL = "".concat(EMBED_URL, "/RELATED_TOPICS");
var EMBED_QUERIES_URL = "".concat(EMBED_URL, "/RELATED_QUERIES");
var EMBED_TIMESERIES_URL = "".concat(EMBED_URL, "/TIMESERIES");
// RSS Links
var DAILY_RSS = 'https://trends.google.com/trends/trendingsearches/daily/rss';
var REALTIME_RSS = 'https://trends.google.com/trending/rss';
// Batch periods enum
var BatchPeriod = {
Past4H: 2,
// 31 points (new points every 8 min)
Past24H: 3,
// 91 points (every 16 min)
Past48H: 5,
// 181 points (every 16 min)
Past7D: 4 // 43 points (every 4 hours)
};
var TrendsQuotaExceededError = /*#__PURE__*/function (_Error) {
function TrendsQuotaExceededError() {
var _this;
_classCallCheck(this, TrendsQuotaExceededError);
_this = _callSuper(this, TrendsQuotaExceededError, ["API quota exceeded for related queries/topics. " + "To resolve this, you can try:\n" + "1. Use a different referer in request headers:\n" + " tr.relatedQueries(keyword, { headers: { referer: 'https://www.google.com/' } })\n" + "2. Use a different IP address by configuring a proxy:\n" + " tr.setProxy('http://proxy:port')\n" + " # or\n" + " new Trends({ proxy: { http: 'http://proxy:port', https: 'https://proxy:port' } })\n" + "3. Wait before making additional requests"]);
_this.name = 'TrendsQuotaExceededError';
return _this;
}
_inherits(TrendsQuotaExceededError, _Error);
return _createClass(TrendsQuotaExceededError);
}(/*#__PURE__*/_wrapNativeSuper(Error));
var Trends = /*#__PURE__*/function () {
/**
* Initialize the Trends client
* @param {Object} options Configuration options
* @param {string} [options.language='en'] Language code (e.g., 'en', 'es', 'fr')
* @param {number} [options.tzs=360] Timezone offset in minutes
* @param {number} [options.requestDelay=5.0] Minimum time interval between requests in seconds
* @param {number} [options.maxRetries=3] Maximum number of retry attempts for failed requests
* @param {boolean} [options.useEntityNames=false] Whether to use entity names instead of keywords
* @param {Object|string} [options.proxy=null] Proxy configuration
*/
function Trends() {
var _this$_headers;
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
_classCallCheck(this, Trends);
var _options$language = options.language,
language = _options$language === void 0 ? 'en' : _options$language,
_options$tzs = options.tzs,
tzs = _options$tzs === void 0 ? 360 : _options$tzs,
_options$requestDelay = options.requestDelay,
requestDelay = _options$requestDelay === void 0 ? 5.0 : _options$requestDelay,
_options$maxRetries = options.maxRetries,
maxRetries = _options$maxRetries === void 0 ? 3 : _options$maxRetries,
_options$useEntityNam = options.useEntityNames,
useEntityNames = _options$useEntityNam === void 0 ? false : _options$useEntityNam,
_options$proxy = options.proxy,
proxy = _options$proxy === void 0 ? null : _options$proxy;
this.language = this._validateLanguage(language);
this.tzs = tzs || -DateTime.local().offset;
this._defaultParams = {
hl: this.language,
tz: this.tzs
};
this.useEntityNames = useEntityNames;
this._headers = (_this$_headers = {
'accept-language': this.language,
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36',
'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8',
'accept-encoding': 'gzip, deflate, br'
}, _defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_this$_headers, "accept-language", 'en-US,en;q=0.9'), 'cache-control', 'no-cache'), 'pragma', 'no-cache'), 'sec-ch-ua', '"Chromium";v="122", "Not(A:Brand";v="24", "Google Chrome";v="122"'), 'sec-ch-ua-mobile', '?0'), 'sec-ch-ua-platform', '"windows"'), 'sec-fetch-dest', 'document'), 'sec-fetch-mode', 'navigate'), 'sec-fetch-site', 'none'), 'sec-fetch-user', '?1'), _defineProperty(_this$_headers, 'upgrade-insecure-requests', '1'));
this._geoCache = new Map();
this._categoryCache = new Map();
this.requestDelay = requestDelay;
this.maxRetries = maxRetries;
this.lastRequestTimes = new Set([0, 1]);
// Setup cookie jar
this.cookieJar = new tough.CookieJar();
// Initialize axios instance with default config
this.session = wrapper(axios.create({
headers: this._headers,
jar: this.cookieJar,
withCredentials: true
}));
// Set proxy if provided
if (proxy) {
this.setProxy(proxy);
}
}
/**
* Validate and normalize language code
* @private
*/
return _createClass(Trends, [{
key: "_validateLanguage",
value: function _validateLanguage(language) {
if (typeof language === 'string' && language.length >= 2) {
return language.slice(0, 2).toLowerCase();
}
return 'en';
}
/**
* Set or update proxy configuration
* @param {Object|string} proxy Proxy configuration
*/
}, {
key: "setProxy",
value: function setProxy(proxy) {
if (typeof proxy === 'string') {
proxy = {
http: proxy,
https: proxy
};
}
if (proxy) {
this.session.defaults.proxy = proxy;
} else {
delete this.session.defaults.proxy;
}
}
/**
* Extract keywords from token
* @private
*/
}, {
key: "_extractKeywordsFromToken",
value: function _extractKeywordsFromToken(token) {
if (this.useEntityNames) {
return token.bullets.map(function (item) {
return item.text;
});
}
return token.request.comparisonItem.map(function (item) {
return item.complexKeywordsRestriction.keyword[0].value;
});
}
/**
* Parse protected JSON response
* @private
*/
}, {
key: "_initSession",
value: (
/**
* Initialize session with Google Trends
* @private
*/
function () {
var _initSession2 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
return _regeneratorRuntime().wrap(function _callee$(_context) {
while (1) switch (_context.prev = _context.next) {
case 0:
_context.prev = 0;
_context.next = 3;
return this.session.get('https://trends.google.com/', {
headers: _objectSpread(_objectSpread({}, this._headers), {}, {
'referer': 'https://www.google.com/'
})
});
case 3:
_context.next = 5;
return new Promise(function (resolve) {
return setTimeout(resolve, 2000);
});
case 5:
_context.next = 7;
return this.session.get('https://trends.google.com/trends/explore', {
headers: _objectSpread(_objectSpread({}, this._headers), {}, {
'referer': 'https://trends.google.com/'
})
});
case 7:
return _context.abrupt("return", true);
case 10:
_context.prev = 10;
_context.t0 = _context["catch"](0);
console.error('Failed to initialize session:', _context.t0.message);
return _context.abrupt("return", false);
case 14:
case "end":
return _context.stop();
}
}, _callee, this, [[0, 10]]);
}));
function _initSession() {
return _initSession2.apply(this, arguments);
}
return _initSession;
}()
/**
* Make HTTP GET request with retry logic
* @private
*/
)
}, {
key: "_get",
value: (function () {
var _get2 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee2(url) {
var _this$cookieJar$store,
_this2 = this;
var params,
headers,
retries,
responseCodes,
lastResponse,
_loop,
_ret,
currentDelay,
_args3 = arguments;
return _regeneratorRuntime().wrap(function _callee2$(_context3) {
while (1) switch (_context3.prev = _context3.next) {
case 0:
params = _args3.length > 1 && _args3[1] !== undefined ? _args3[1] : null;
headers = _args3.length > 2 && _args3[2] !== undefined ? _args3[2] : null;
if ((_this$cookieJar$store = this.cookieJar.store.idx) !== null && _this$cookieJar$store !== void 0 && _this$cookieJar$store['trends.google.com']) {
_context3.next = 5;
break;
}
_context3.next = 5;
return this._initSession();
case 5:
retries = this.maxRetries;
responseCodes = [];
lastResponse = null;
_loop = /*#__PURE__*/_regeneratorRuntime().mark(function _loop() {
var minTime, sleepTime, requestHeaders, response, waitTime, _error$response, _error$response2;
return _regeneratorRuntime().wrap(function _loop$(_context2) {
while (1) switch (_context2.prev = _context2.next) {
case 0:
_context2.prev = 0;
if (!_this2.requestDelay) {
_context2.next = 8;
break;
}
minTime = Math.min.apply(Math, _toConsumableArray(_this2.lastRequestTimes));
sleepTime = Math.max(0, _this2.requestDelay * 1000 - (Date.now() - minTime));
if (!(sleepTime > 0)) {
_context2.next = 7;
break;
}
_context2.next = 7;
return new Promise(function (resolve) {
return setTimeout(resolve, sleepTime);
});
case 7:
_this2.lastRequestTimes = new Set([].concat(_toConsumableArray(Array.from(_this2.lastRequestTimes).filter(function (t) {
return t !== minTime;
})), [Date.now()]));
case 8:
requestHeaders = _objectSpread(_objectSpread(_objectSpread({}, _this2._headers), headers), {}, {
'referer': 'https://trends.google.com/trends/explore'
});
_context2.next = 11;
return _this2.session.get(url, {
params: params,
headers: requestHeaders,
maxRedirects: 5
});
case 11:
response = _context2.sent;
lastResponse = response;
responseCodes.push(response.status);
if (!(response.status === 200)) {
_context2.next = 16;
break;
}
return _context2.abrupt("return", {
v: response
});
case 16:
if (![429, 302].includes(response.status)) {
_context2.next = 21;
break;
}
waitTime = Math.pow(2, _this2.maxRetries - retries) * 5;
console.log("Rate limit hit, waiting ".concat(waitTime, " seconds before retry"));
_context2.next = 21;
return new Promise(function (resolve) {
return setTimeout(resolve, waitTime * 1000);
});
case 21:
retries--;
_context2.next = 34;
break;
case 24:
_context2.prev = 24;
_context2.t0 = _context2["catch"](0);
console.error('Request error:', _context2.t0.message);
if (_context2.t0.response) {
console.error('Response status:', _context2.t0.response.status);
console.error('Response data:', _context2.t0.response.data);
}
if (!(retries === 0)) {
_context2.next = 30;
break;
}
throw _context2.t0;
case 30:
retries--;
// If session seems invalid, try to reinitialize
if (!(((_error$response = _context2.t0.response) === null || _error$response === void 0 ? void 0 : _error$response.status) === 401 || ((_error$response2 = _context2.t0.response) === null || _error$response2 === void 0 ? void 0 : _error$response2.status) === 403)) {
_context2.next = 34;
break;
}
_context2.next = 34;
return _this2._initSession();
case 34:
case "end":
return _context2.stop();
}
}, _loop, null, [[0, 24]]);
});
case 9:
if (!(retries > 0)) {
_context3.next = 16;
break;
}
return _context3.delegateYield(_loop(), "t0", 11);
case 11:
_ret = _context3.t0;
if (!_ret) {
_context3.next = 14;
break;
}
return _context3.abrupt("return", _ret.v);
case 14:
_context3.next = 9;
break;
case 16:
if (responseCodes.filter(function (code) {
return code === 429;
}).length > responseCodes.length / 2) {
currentDelay = this.requestDelay || 1;
console.warn("\nWarning: Too many rate limit errors (429). Consider increasing requestDelay " + "to new Trends({ requestDelay: ".concat(currentDelay * 2, " }) before Google implements ") + "a long-term rate limit!");
}
throw new Error("Failed after ".concat(this.maxRetries, " retries"));
case 18:
case "end":
return _context3.stop();
}
}, _callee2, this);
}));
function _get(_x) {
return _get2.apply(this, arguments);
}
return _get;
}()
/**
* Encode request items for API calls
* @private
*/
)
}, {
key: "_encodeItems",
value: function _encodeItems(keywords) {
var timeframe = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "today 12-m";
var geo = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '';
var data = [keywords, timeframe, geo].map(function (item) {
return Array.isArray(item) ? item : [item];
});
var lengths = data.map(function (arr) {
return arr.length;
});
var maxLen = Math.max.apply(Math, _toConsumableArray(lengths));
if (!lengths.every(function (len) {
return maxLen % len === 0;
})) {
throw new Error("Ambiguous input sizes: unable to determine how to combine " + "inputs of lengths ".concat(lengths.join(', ')));
}
data.forEach(function (arr, i) {
while (arr.length < maxLen) {
arr.push.apply(arr, _toConsumableArray(arr));
}
});
return data[0].map(function (_, i) {
return {
keyword: data[0][i],
time: data[1][i],
geo: data[2][i]
};
});
}
/**
* Encode request parameters for API calls
* @private
*/
}, {
key: "_encodeRequest",
value: function _encodeRequest(params) {
if ('keyword' in params) {
var keywords = Array.isArray(params.keyword) ? params.keyword : [params.keyword];
if (keywords.length !== 1) {
throw new Error("This endpoint only supports a single keyword");
}
delete params.keyword;
params.keywords = keywords;
}
var items = this._encodeItems(params.keywords, params.timeframe || "today 12-m", params.geo || '');
var req = {
req: JSON.stringify({
comparisonItem: items,
category: params.cat || 0,
property: params.gprop || ''
})
};
return _objectSpread(_objectSpread({}, req), this._defaultParams);
}
/**
* Extract embedded data from response
* @private
*/
}, {
key: "_extractEmbeddedData",
value: function _extractEmbeddedData(text) {
var pattern = /JSON\.parse\('([^']+)'\)/;
var matches = text.match(pattern);
if (matches) {
return JSON.parse(decodeEscapeText(matches[1]));
}
console.warn("Failed to extract JSON data");
return null;
}
/**
* Convert token to data using appropriate API endpoint
* @private
*/
}, {
key: "_tokenToData",
value: (function () {
var _tokenToData2 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee3(token) {
var urlMap, url, params, response;
return _regeneratorRuntime().wrap(function _callee3$(_context4) {
while (1) switch (_context4.prev = _context4.next) {
case 0:
urlMap = {
'fe_line_chart': API_TIMELINE_URL,
'fe_multi_range_chart': API_MULTIRANGE_URL,
'fe_multi_heat_map': API_GEO_URL,
'fe_geo_chart_explore': API_GEO_URL,
'fe_related_searches': API_RELATED_QUERIES_URL
};
url = urlMap[token.type];
params = _objectSpread({
req: JSON.stringify(token.request),
token: token.token
}, this._defaultParams);
_context4.next = 5;
return this._get(url, params);
case 5:
response = _context4.sent;
return _context4.abrupt("return", Trends._parseProtectedJson(response));
case 7:
case "end":
return _context4.stop();
}
}, _callee3, this);
}));
function _tokenToData(_x2) {
return _tokenToData2.apply(this, arguments);
}
return _tokenToData;
}()
/**
* Get token data from Google Trends API
* @private
*/
)
}, {
key: "_getTokenData",
value: (function () {
var _getTokenData2 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee4(url) {
var params,
requestFix,
headers,
raiseQuotaError,
encodedParams,
response,
token,
_token$request,
userType,
data,
_args5 = arguments;
return _regeneratorRuntime().wrap(function _callee4$(_context5) {
while (1) switch (_context5.prev = _context5.next) {
case 0:
params = _args5.length > 1 && _args5[1] !== undefined ? _args5[1] : null;
requestFix = _args5.length > 2 && _args5[2] !== undefined ? _args5[2] : null;
headers = _args5.length > 3 && _args5[3] !== undefined ? _args5[3] : null;
raiseQuotaError = _args5.length > 4 && _args5[4] !== undefined ? _args5[4] : false;
encodedParams = this._encodeRequest(params);
_context5.next = 7;
return this._get(url, encodedParams, headers);
case 7:
response = _context5.sent;
token = this._extractEmbeddedData(response.data);
if (requestFix) {
token.request = _objectSpread(_objectSpread({}, token.request), requestFix);
}
if (!raiseQuotaError) {
_context5.next = 14;
break;
}
userType = token === null || token === void 0 || (_token$request = token.request) === null || _token$request === void 0 || (_token$request = _token$request.userConfig) === null || _token$request === void 0 ? void 0 : _token$request.userType;
if (!(userType === "USER_TYPE_EMBED_OVER_QUOTA")) {
_context5.next = 14;
break;
}
throw new TrendsQuotaExceededError();
case 14:
_context5.next = 16;
return this._tokenToData(token);
case 16:
data = _context5.sent;
return _context5.abrupt("return", [token, data]);
case 18:
case "end":
return _context5.stop();
}
}, _callee4, this);
}));
function _getTokenData(_x3) {
return _getTokenData2.apply(this, arguments);
}
return _getTokenData;
}()
/**
* Make batch request to Google Trends API
* @private
*/
)
}, {
key: "_getBatch",
value: (function () {
var _getBatch2 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee5(reqId, data) {
var reqData, postData, headers, response;
return _regeneratorRuntime().wrap(function _callee5$(_context6) {
while (1) switch (_context6.prev = _context6.next) {
case 0:
reqData = JSON.stringify([[[reqId, JSON.stringify(data), null, "generic"]]]);
postData = "f.req=".concat(encodeURIComponent(reqData));
headers = {
"content-type": "application/x-www-form-urlencoded;charset=UTF-8"
};
_context6.next = 5;
return this.session.post(BATCH_URL, postData, {
headers: headers
});
case 5:
response = _context6.sent;
return _context6.abrupt("return", response);
case 7:
case "end":
return _context6.stop();
}
}, _callee5, this);
}));
function _getBatch(_x4, _x5) {
return _getBatch2.apply(this, arguments);
}
return _getBatch;
}()
/**
* Get interest over time data
* @param {string|string[]} keywords Keywords to analyze
* @param {string} [timeframe="today 12-m"] Time range for analysis
* @param {string} [geo=""] Geographic location code
* @param {number} [cat=0] Category ID
* @param {string} [gprop=""] Google property filter
* @param {boolean} [returnRaw=false] Return raw API response
* @param {Object} [headers=null] Custom request headers
*/
)
}, {
key: "interestOverTime",
value: (function () {
var _interestOverTime = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee6(keywords) {
var options,
_options$timeframe,
timeframe,
_options$geo,
geo,
_options$cat,
cat,
_options$gprop,
gprop,
_options$returnRaw,
returnRaw,
_options$headers,
headers,
timeframes,
_yield$this$_getToken,
_yield$this$_getToken2,
token,
data,
extractedKeywords,
bullets,
_args7 = arguments;
return _regeneratorRuntime().wrap(function _callee6$(_context7) {
while (1) switch (_context7.prev = _context7.next) {
case 0:
options = _args7.length > 1 && _args7[1] !== undefined ? _args7[1] : {};
_options$timeframe = options.timeframe, timeframe = _options$timeframe === void 0 ? "today 12-m" : _options$timeframe, _options$geo = options.geo, geo = _options$geo === void 0 ? "" : _options$geo, _options$cat = options.cat, cat = _options$cat === void 0 ? 0 : _options$cat, _options$gprop = options.gprop, gprop = _options$gprop === void 0 ? "" : _options$gprop, _options$returnRaw = options.returnRaw, returnRaw = _options$returnRaw === void 0 ? false : _options$returnRaw, _options$headers = options.headers, headers = _options$headers === void 0 ? null : _options$headers;
checkTimeframeResolution(timeframe);
timeframes = ensureList(timeframe).map(convertTimeframe);
_context7.next = 6;
return this._getTokenData(EMBED_TIMESERIES_URL, {
keywords: keywords,
timeframe: timeframes,
geo: geo,
cat: cat,
gprop: gprop
}, null, headers);
case 6:
_yield$this$_getToken = _context7.sent;
_yield$this$_getToken2 = _slicedToArray(_yield$this$_getToken, 2);
token = _yield$this$_getToken2[0];
data = _yield$this$_getToken2[1];
if (!returnRaw) {
_context7.next = 12;
break;
}
return _context7.abrupt("return", [token, data]);
case 12:
if (!(token.type === 'fe_line_chart')) {
_context7.next = 15;
break;
}
extractedKeywords = this._extractKeywordsFromToken(token);
return _context7.abrupt("return", TrendsDataConverter.interestOverTime(data, extractedKeywords));
case 15:
if (!(token.type === 'fe_multi_range_chart')) {
_context7.next = 18;
break;
}
bullets = TrendsDataConverter.tokenToBullets(token);
return _context7.abrupt("return", TrendsDataConverter.multirangeInterestOverTime(data, bullets));
case 18:
return _context7.abrupt("return", data);
case 19:
case "end":
return _context7.stop();
}
}, _callee6, this);
}));
function interestOverTime(_x6) {
return _interestOverTime.apply(this, arguments);
}
return interestOverTime;
}()
/**
* Get related queries for a keyword
*/
)
}, {
key: "relatedQueries",
value: (function () {
var _relatedQueries = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee7(keyword) {
var options,
_options$timeframe2,
timeframe,
_options$geo2,
geo,
_options$cat2,
cat,
_options$gprop2,
gprop,
_options$returnRaw2,
returnRaw,
_options$headers2,
headers,
_yield$this$_getToken3,
_yield$this$_getToken4,
token,
data,
_args8 = arguments;
return _regeneratorRuntime().wrap(function _callee7$(_context8) {
while (1) switch (_context8.prev = _context8.next) {
case 0:
options = _args8.length > 1 && _args8[1] !== undefined ? _args8[1] : {};
_options$timeframe2 = options.timeframe, timeframe = _options$timeframe2 === void 0 ? "today 12-m" : _options$timeframe2, _options$geo2 = options.geo, geo = _options$geo2 === void 0 ? "" : _options$geo2, _options$cat2 = options.cat, cat = _options$cat2 === void 0 ? 0 : _options$cat2, _options$gprop2 = options.gprop, gprop = _options$gprop2 === void 0 ? "" : _options$gprop2, _options$returnRaw2 = options.returnRaw, returnRaw = _options$returnRaw2 === void 0 ? false : _options$returnRaw2, _options$headers2 = options.headers, headers = _options$headers2 === void 0 ? {
referer: "https://trends.google.com/trends/explore"
} : _options$headers2;
_context8.next = 4;
return this._getTokenData(EMBED_QUERIES_URL, {
keyword: keyword,
timeframe: timeframe,
geo: geo,
cat: cat,
gprop: gprop
}, null, headers, true);
case 4:
_yield$this$_getToken3 = _context8.sent;
_yield$this$_getToken4 = _slicedToArray(_yield$this$_getToken3, 2);
token = _yield$this$_getToken4[0];
data = _yield$this$_getToken4[1];
if (!returnRaw) {
_context8.next = 10;
break;
}
return _context8.abrupt("return", [token, data]);
case 10:
return _context8.abrupt("return", TrendsDataConverter.relatedQueries(data));
case 11:
case "end":
return _context8.stop();
}
}, _callee7, this);
}));
function relatedQueries(_x7) {
return _relatedQueries.apply(this, arguments);
}
return relatedQueries;
}()
/**
* Get related topics for a keyword
*/
)
}, {
key: "relatedTopics",
value: (function () {
var _relatedTopics = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee8(keyword) {
var options,
_options$timeframe3,
timeframe,
_options$geo3,
geo,
_options$cat3,
cat,
_options$gprop3,
gprop,
_options$returnRaw3,
returnRaw,
_options$headers3,
headers,
_yield$this$_getToken5,
_yield$this$_getToken6,
token,
data,
_args9 = arguments;
return _regeneratorRuntime().wrap(function _callee8$(_context9) {
while (1) switch (_context9.prev = _context9.next) {
case 0:
options = _args9.length > 1 && _args9[1] !== undefined ? _args9[1] : {};
_options$timeframe3 = options.timeframe, timeframe = _options$timeframe3 === void 0 ? "today 12-m" : _options$timeframe3, _options$geo3 = options.geo, geo = _options$geo3 === void 0 ? "" : _options$geo3, _options$cat3 = options.cat, cat = _options$cat3 === void 0 ? 0 : _options$cat3, _options$gprop3 = options.gprop, gprop = _options$gprop3 === void 0 ? "" : _options$gprop3, _options$returnRaw3 = options.returnRaw, returnRaw = _options$returnRaw3 === void 0 ? false : _options$returnRaw3, _options$headers3 = options.headers, headers = _options$headers3 === void 0 ? {
referer: "https://trends.google.com/trends/explore"
} : _options$headers3;
_context9.next = 4;
return this._getTokenData(EMBED_TOPICS_URL, {
keyword: keyword,
timeframe: timeframe,
geo: geo,
cat: cat,
gprop: gprop
}, null, headers, true);
case 4:
_yield$this$_getToken5 = _context9.sent;
_yield$this$_getToken6 = _slicedToArray(_yield$this$_getToken5, 2);
token = _yield$this$_getToken6[0];
data = _yield$this$_getToken6[1];
if (!returnRaw) {
_context9.next = 10;
break;
}
return _context9.abrupt("return", [token, data]);
case 10:
return _context9.abrupt("return", TrendsDataConverter.relatedQueries(data));
case 11:
case "end":
return _context9.stop();
}
}, _callee8, this);
}));
function relatedTopics(_x8) {
return _relatedTopics.apply(this, arguments);
}
return relatedTopics;
}()
/**
* Get interest by region data
*/
)
}, {
key: "interestB