pat310 / google-trends-api

An API layer on top of google trends
https://www.npmjs.com/package/google-trends-api
MIT License
877 stars 175 forks source link

Error when comparison contains both Search terms and Topics #75

Closed meetamit closed 7 years ago

meetamit commented 7 years ago

The error is "TypeError: Cannot read property 'requestOptions' of undefined"

This stems from the handling of`the parsed results.

The reason is because in Google's UI, and correspondingly in widgets JSON, there's a message at the top: "Note: This comparison contains both Search terms and Topics, which are measured differently."

In JSON, it looks like this

[
  {
    "text": {
      "text": "This comparison contains both Search terms and Topics, which are measured differently"
    },
    "url": "https://support.google.com/trends/answer/4359550",
    "id": "topics_note",
    "type": "fe_text",
    "title": "",
    "template": "fe_explore_note",
    "embedTemplate": "fe_embed",
    "version": "1",
    "isLong": true,
    "isCurated": false
  },
  {
    "request": {
      "time": "2016-08-26 2017-08-26",
      "resolution": "WEEK",
      "locale": "en-US",
      "comparisonItem": [
        {
          "geo": {},
          "complexKeywordsRestriction": {
            "keyword": [
              {
                "type": "ENTITY",
                "value": "/m/0128447t"
              }
            ]
          }
        },
...

As a result, the rest of the widgets get shifted by 1, so pos needs to be incremented as well.

A possible fix is to modify this try caluse of parseResults() to

var widgets = JSON.parse(e.slice(4)).widgets;
if (!widgets[0].request) { widgets.shift(); }
return widgets;
pat310 commented 7 years ago

Oh boy. What method/request are you making to get this error so I can replicate it?

On Aug 25, 2017, at 22:17, Amit Sch notifications@github.com wrote:

The error is "TypeError: Cannot read property 'requestOptions' of undefined"

This stems from the handling of`the parsed results.

The reason is because in Google's UI, and correspondingly in widgets JSON, there's a message at the top: "Note: This comparison contains both Search terms and Topics, which are measured differently."

In JSON, it looks like this

[ { "text": { "text": "This comparison contains both Search terms and Topics, which are measured differently" }, "url": "https://support.google.com/trends/answer/4359550", "id": "topics_note", "type": "fe_text", "title": "", "template": "fe_explore_note", "embedTemplate": "fe_embed", "version": "1", "isLong": true, "isCurated": false }, { "request": { "time": "2016-08-26 2017-08-26", "resolution": "WEEK", "locale": "en-US", "comparisonItem": [ { "geo": {}, "complexKeywordsRestriction": { "keyword": [ { "type": "ENTITY", "value": "/m/0128447t" } ] } }, As a result, the rest of the widgets get shifted by 1, so pos needs to be incremented as well.

A possible fix is to modify this try caluse of parseResults() to

var widgets = JSON.parse(e.slice(4)).widgets; if (!widgets[0].request) { widgets.shift(); } return widgets; — You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or mute the thread.

meetamit commented 7 years ago

Search Term (Amazon Fire TV) vs Topic ('/m/0h65vcz')

    api.interestOverTime({ keyword: ['/m/0h65vcz', 'Amazon Fire TV'] })

image

pat310 commented 7 years ago

@meetamit I made a PR in #76 to correct this issue if you want to check it out

meetamit commented 7 years ago

Thank you! I will check it out as soon as possible