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

got rid of problematic function #542

Merged
merged 2 commits into from
Nov 11, 2022
Merged
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
85 changes: 8 additions & 77 deletions pytrends/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -541,80 +541,11 @@ def categories(self):
)
return req_json

def get_historical_interest(self, keywords, year_start=2018, month_start=1,
day_start=1, hour_start=0, year_end=2018,
month_end=2, day_end=1, hour_end=0, cat=0,
geo='', gprop='', sleep=0, frequency='hourly'):
"""Gets historical hourly data for interest by chunking requests to 1 week at a time (which is what Google allows)"""

# construct datetime objects - raises ValueError if invalid parameters
initial_start_date = start_date = datetime(year_start, month_start,
day_start, hour_start)
end_date = datetime(year_end, month_end, day_end, hour_end)

# Timedeltas:
# 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':
delta = timedelta(days=250)
else:
raise(ValueError('Frequency must be hourly or daily'))

df = pd.DataFrame()

date_iterator = start_date
date_iterator += delta

while True:
# format date to comply with API call (different for hourly/daily)

if frequency == 'hourly':
start_date_str = start_date.strftime('%Y-%m-%dT%H')
date_iterator_str = date_iterator.strftime('%Y-%m-%dT%H')
elif frequency == 'daily':
start_date_str = start_date.strftime('%Y-%m-%d')
date_iterator_str = date_iterator.strftime('%Y-%m-%d')

tf = start_date_str + ' ' + date_iterator_str

try:
self.build_payload(keywords, cat, tf, geo, gprop)
week_df = self.interest_over_time()
df = pd.concat([df,week_df], ignore_index = True)
except Exception as e:
print(e)
pass

start_date += delta
date_iterator += delta

if (date_iterator > end_date):
# Run more days to get remaining data that would have been truncated if we stopped now
if frequency == 'hourly':
start_date_str = start_date.strftime('%Y-%m-%dT%H')
date_iterator_str = date_iterator.strftime('%Y-%m-%dT%H')
elif frequency == 'daily':
start_date_str = start_date.strftime('%Y-%m-%d')
date_iterator_str = date_iterator.strftime('%Y-%m-%d')

tf = start_date_str + ' ' + date_iterator_str

try:
self.build_payload(keywords, cat, tf, geo, gprop)
week_df = self.interest_over_time()
df = pd.concat([df,week_df] ,ignore_index = True)
except Exception as e:
print(e)
pass
break

# just in case you are rate-limited by Google. Recommended is 60 if you are.
if sleep > 0:
time.sleep(sleep)

# Return the dataframe with results from our timeframe
return df.loc[initial_start_date:end_date]
def get_historical_interest(self, *args, **kwargs):
raise NotImplementedError(
"""This method has been removed for incorrectness. It will be removed completely in v5.
If you'd like similar functionality, please try implementing it yourself and consider submitting a pull request to add it to pytrends.

There is discussion at:
https://github.com/GeneralMills/pytrends/pull/542"""
)