pat310 / google-trends-api

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

Downloading Topic information #69

Closed tsennyuen closed 7 years ago

tsennyuen commented 7 years ago

For interest over time, we can pass in search terms , start and end dates, Geo using syntax below.

How do you pass in a topic?

googleTrends.interestOverTime({keyword: string, startTime: Date, endTime: Date, geo: string}, cbFunc)

Requires an object as the first parameter with the following keys:

keyword - required - type string or array - the search term(s) of interest startTime - optional - type Date object - the start of the time range of interest (defaults to new Date('2004-01-01') if not supplied) endTime - optional - type Date object - the end of the time range of interest (defaults to new Date(Date.now()) if not supplied) geo - optional - type string - geocode for a country, region, or DMA depending on the granularity required (defaults to worldwide). For example, geo: 'US-CA-800' will target the Bakersfield, California, United States or geo: 'US' will just target the US. hl - optional - type string - preferred language code for results (defaults to english)

pat310 commented 7 years ago

@tsennyuen I think these are the only fields available from Google. Could you use the topic in the keyword field?

pat310 commented 7 years ago

@tsennyuen maybe this is related to #67 ?

tsennyuen commented 7 years ago

Hello Pat. #67 seems to be referring to category. Isn't topic and category two different things?

pat310 commented 7 years ago

I don't think there is a topic field to query on. Can you give me a high level example of what you are trying to do?

tsennyuen commented 7 years ago

This link is for TOPIC "Back to School", https://trends.google.com/trends/explore?geo=US&q=%2Fm%2F068pw8&hl=en-US&tz=Etc%2FGMT%2B7&tz=Etc%2FGMT%2B7

In the UI, if you try to edit the topic, you can do search term or topic

pat310 commented 7 years ago

@tsennyuen Ok I see what you are saying. It looks like if you make a GET request to a link like this (for the autocomplete results) [https://trends.google.com/trends/api/autocomplete/Back to school?hl=en-US&tz=240](https://trends.google.com/trends/api/autocomplete/Back to school?hl=en-US&tz=240), it will return to you a JSON listing of the available types to search on:

{"default":{"topics":[{"mid":"/m/0414j6","title":"Back to School","type":"1986 film"},{"mid":"/m/068pw8","title":"Back to school","type":"Topic"},{"mid":"/m/04vwgn","title":"Fight Back to School","type":"1991 film"},{"mid":"/m/05357_","title":"Tax holiday","type":"Holiday"},{"mid":"/m/02pb6kt","title":"Fight Back to School II","type":"1992 film"}]}}

Here you see for type, Topic, the value is "/m/068pw8". Then if you do a query with '/m/068pw8' as the search term, you get the results you are looking for.

I'm not sure if there is a more direct way of performing this query yet... I could make an additional method that returns the autocomplete results if there is interest, and then those results could be used in the interestOverTime method.

tsennyuen commented 7 years ago

Thank you. I believe that will throw an error in the API call because it has special characters. Would it need to be encoded?

pat310 commented 7 years ago

Hmm, in the request module the query string gets passed through node's querystring module which should encode the unsafe characters to percent encoding. I do get results when I run the query:

googleTrends.interestOverTime({keyword: '/m/068pw8'})
.then((res) => {
  console.log('this is res', res);
})

Are the results wrong?

pat310 commented 7 years ago

@tsennyuen I made a pull request to add another api method called autoComplete

tsennyuen commented 7 years ago

Thank you Patrick. We will do some testing and will let you know if this issue is resolved. You have been very helpful.

pat310 commented 7 years ago

@tsennyuen Awesome, thanks. Just open it back up if not resolved.