UNPKG

google-trends-api-client

Version:

An fully typed and easy to use client for the google trends api.

48 lines 2.4 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getRelatedTopics = void 0; const UnexpectedCodePathError_1 = require("../utils/errors/UnexpectedCodePathError"); const defineCommonGoogleTrendsApiParametersValue_1 = require("../assertions/defineCommonGoogleTrendsApiParametersValue"); const getExplorationWidgets_1 = require("./getExplorationWidgets"); const getFromApiCall_1 = require("./getFromApiCall"); /** * per google * > Users searching for your term also searched for these topics. * > You can sort by the following metrics: * > - TOP * > - The most popular topics. * > - Scoring is on a relative scale where a value of 100 is the most commonly searched topic, 50 is a query searched half as often as the most popular topic, and so on. * > - RISING * > - Related topics with the biggest increase in search frequency since the last time period. * > - Results marked "Breakout" had a tremendous increase, probably because these topics are new and had few (if any) prior searches. */ const getRelatedTopics = async (...args) => { // get the exploration widgets for this request, which give us the request token and params to get the related queries data const { widgets } = await (0, getExplorationWidgets_1.getExplorationWidgets)(...args); // find the relevant widget const relevantWidget = widgets.find((widget) => widget.id === 'RELATED_TOPICS'); if (!relevantWidget) throw new UnexpectedCodePathError_1.UnexpectedCodePathError('could not find widget with id RELATED_TOPICS'); // make the the request for the widgets data const response = await (0, getFromApiCall_1.getFromApiCall)({ path: 'trends/api/widgetdata/relatedsearches', agentOptions: args[1], queryParams: { ...(0, defineCommonGoogleTrendsApiParametersValue_1.defineCommonGoogleTrendsApiParametersValue)({ hl: args[0].hl, tz: args[0].tz, }), req: JSON.stringify(relevantWidget.request), token: relevantWidget.token, }, }); const result = JSON.parse(response); return { top: result.default.rankedList[0] .rankedKeyword, rising: result.default.rankedList[1] .rankedKeyword, }; }; exports.getRelatedTopics = getRelatedTopics; //# sourceMappingURL=getRelatedTopics.js.map