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 1 commit
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
25 changes: 12 additions & 13 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 @@ -307,9 +312,6 @@ def related_topics(self):
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 @@ -343,7 +345,7 @@ def related_topics(self):
# 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}
result_dict = {'rising': df_rising, 'top': df_top}
return result_dict

def related_queries(self):
Expand All @@ -356,9 +358,6 @@ def related_queries(self):
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 @@ -390,7 +389,7 @@ def related_queries(self):
# 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}
result_dict = {'top': top_df, 'rising': rising_df}
Copy link
Collaborator

Choose a reason for hiding this comment

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

It looks like you're fundamentally changing the form of what gets returned, even for conventional keyword-bearing use-cases.

return result_dict

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