Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

removed keywords restriction in payloads #414

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 24 additions & 17 deletions pytrends/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def _get_data(self, url, method=GET_METHOD, trim_chars=0, **kwargs):
'response with code {0}.'.format(response.status_code),
response=response)

def build_payload(self, kw_list, cat=0, timeframe='today 5-y', geo='',
def build_payload(self, kw_list = [], cat=0, timeframe='today 5-y', geo='',
gprop=''):
"""Create the payload for related queries, interest over time and interest by region"""
if gprop not in ['', 'images', 'news', 'youtube', 'froogle']:
Expand All @@ -155,10 +155,15 @@ def build_payload(self, kw_list, cat=0, timeframe='today 5-y', geo='',
'req': {'comparisonItem': [], 'category': cat, 'property': gprop}
}

# build out json for each keyword
for kw in self.kw_list:
keyword_payload = {'keyword': kw, 'time': timeframe,
'geo': self.geo}
# build out json for each keyword if there are keywords
if len(kw_list) > 0:
for kw in self.kw_list:
keyword_payload = {'keyword': kw, 'time': timeframe,
'geo': self.geo}
self.token_payload['req']['comparisonItem'].append(keyword_payload)
# build out json if there are no keywords
else:
keyword_payload = {'keyword': '', 'time': timeframe, 'geo': self.geo}
self.token_payload['req']['comparisonItem'].append(keyword_payload)
# requests will mangle this if it is not a string
self.token_payload['req'] = json.dumps(self.token_payload['req'])
Expand Down Expand Up @@ -299,17 +304,13 @@ def interest_by_region(self, resolution='COUNTRY', inc_low_vol=False,

def related_topics(self):
"""Request data from Google's Related Topics section and return a dictionary of dataframes

If no top and/or rising related topics are found, the value for the key "top" and/or "rising" will be None
"""

# make the request
related_payload = dict()
result_dict = dict()
for request_json in self.related_topics_widget_list:
# ensure we know which keyword we are looking at rather than relying on order
kw = request_json['request']['restriction'][
'complexKeywordsRestriction']['keyword'][0]['value']
# convert to string as requests will mangle
related_payload['req'] = json.dumps(request_json['request'])
related_payload['token'] = request_json['token']
Expand Down Expand Up @@ -342,23 +343,24 @@ def related_topics(self):
except KeyError:
# in case no rising topics are found, the lines above will throw a KeyError
df_rising = None

result_dict[kw] = {'rising': df_rising, 'top': df_top}
if len(self.kw_list) > 0:
# ensure we know which keyword we are looking at rather than relying on order
kw = request_json['request']['restriction'][
'complexKeywordsRestriction']['keyword'][0]['value']
result_dict[kw] = {'rising': df_rising, 'top': df_top}
else:
result_dict = {'rising': df_rising, 'top': df_top}
return result_dict

def related_queries(self):
"""Request data from Google's Related Queries section and return a dictionary of dataframes

If no top and/or rising related queries are found, the value for the key "top" and/or "rising" will be None
"""

# make the request
related_payload = dict()
result_dict = dict()
for request_json in self.related_queries_widget_list:
# ensure we know which keyword we are looking at rather than relying on order
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This functionality doesn't seem to be replaced.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, you're right I was so focused on getting the result without keywords I didn't check the alternatives. Now without keywords is just optional, when there are keywords nothing changes:

https://github.com/tpilkati/pytrends/commit/6ca4733c1a23e45b6cfc084381466dcbd89e3d2b

if len(self.kw_list) > 0:
# ensure we know which keyword we are looking at rather than relying on order
kw = request_json['request']['restriction']['complexKeywordsRestriction']['keyword'][0]['value']
result_dict[kw] = {'rising': df_rising, 'top': df_top}
else:
result_dict = {'rising': df_rising, 'top': df_top}

kw = request_json['request']['restriction'][
'complexKeywordsRestriction']['keyword'][0]['value']
# convert to string as requests will mangle
related_payload['req'] = json.dumps(request_json['request'])
related_payload['token'] = request_json['token']
Expand Down Expand Up @@ -389,8 +391,13 @@ def related_queries(self):
except KeyError:
# in case no rising queries are found, the lines above will throw a KeyError
rising_df = None

result_dict[kw] = {'top': top_df, 'rising': rising_df}
if len(self.kw_list) > 0:
# ensure we know which keyword we are looking at rather than relying on order
kw = request_json['request']['restriction'][
'complexKeywordsRestriction']['keyword'][0]['value']
result_dict[kw] = {'top': top_df, 'rising': rising_df}
else:
result_dict = {'top': top_df, 'rising': rising_df}
return result_dict

def trending_searches(self, pn='united_states'):
Expand Down