Skip to content

Commit

Permalink
fix requests_args usage for multiple methods
Browse files Browse the repository at this point in the history
  • Loading branch information
avant1 committed Jun 9, 2022
1 parent 0e887b8 commit b8a862d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
17 changes: 6 additions & 11 deletions pytrends/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -428,8 +428,7 @@ def trending_searches(self, pn='united_states'):
# forms = {'ajax': 1, 'pn': pn, 'htd': '', 'htv': 'l'}
req_json = self._get_data(
url=TrendReq.TRENDING_SEARCHES_URL,
method=TrendReq.GET_METHOD,
**self.requests_args
method=TrendReq.GET_METHOD
)[pn]
result_df = pd.DataFrame(req_json)
return result_df
Expand All @@ -441,8 +440,7 @@ def today_searches(self, pn='US'):
url=TrendReq.TODAY_SEARCHES_URL,
method=TrendReq.GET_METHOD,
trim_chars=5,
params=forms,
**self.requests_args
params=forms
)['default']['trendingSearchesDays'][0]['trendingSearches']
result_df = pd.DataFrame()
# parse the returned json
Expand Down Expand Up @@ -508,8 +506,7 @@ def top_charts(self, date, hl='en-US', tz=300, geo='GLOBAL'):
url=TrendReq.TOP_CHARTS_URL,
method=TrendReq.GET_METHOD,
trim_chars=5,
params=chart_payload,
**self.requests_args
params=chart_payload
)
try:
df = pd.DataFrame(req_json['topCharts'][0]['listItems'])
Expand All @@ -528,8 +525,7 @@ def suggestions(self, keyword):
url=TrendReq.SUGGESTIONS_URL + kw_param,
params=parameters,
method=TrendReq.GET_METHOD,
trim_chars=5,
**self.requests_args
trim_chars=5
)['default']['topics']
return req_json

Expand All @@ -542,8 +538,7 @@ def categories(self):
url=TrendReq.CATEGORIES_URL,
params=params,
method=TrendReq.GET_METHOD,
trim_chars=5,
**self.requests_args
trim_chars=5
)
return req_json

Expand All @@ -562,7 +557,7 @@ def get_historical_interest(self, keywords, year_start=2018, month_start=1,
# 7 days for hourly
# ~250 days for daily (270 seems to be max but sometimes breaks?)
# For weekly can pull any date range so no method required here

if frequency == 'hourly':
delta = timedelta(days=7)
elif frequency == 'daily':
Expand Down
9 changes: 9 additions & 0 deletions pytrends/test_trendReq.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,15 @@ def test_realtime_trending_searches(self):
pytrend.build_payload(kw_list=['pizza', 'bagel'])
self.assertIsNotNone(pytrend.realtime_trending_searches(pn='IN'))

def test_request_args_passing(self):
requests_args = {'headers': {
'User-Agent': 'pytrends',
}}
pytrend = TrendReq(requests_args=requests_args)
pytrend.build_payload(kw_list=['bananas'])
self.assertIsNotNone(pytrend.suggestions('bananas'))
self.assertIsNotNone(pytrend.trending_searches())

def test_top_charts(self):
pytrend = TrendReq()
pytrend.build_payload(kw_list=['pizza', 'bagel'])
Expand Down

0 comments on commit b8a862d

Please sign in to comment.