Skip to content

Commit

Permalink
Merge pull request #542 from rheophile10/master
Browse files Browse the repository at this point in the history
got rid of problematic function
  • Loading branch information
emlazzarin authored Nov 11, 2022
2 parents d30adbd + b61b49a commit 3f4a0c2
Showing 1 changed file with 8 additions and 77 deletions.
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"""
)

0 comments on commit 3f4a0c2

Please sign in to comment.