Skip to content

Commit

Permalink
fix language option
Browse files Browse the repository at this point in the history
  • Loading branch information
nickyungyung committed Aug 12, 2018
1 parent 88e6b6a commit db7281d
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions pytrends/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ def related_topics(self):
related_payload['req'] = json.dumps(request_json['request'])
related_payload['token'] = request_json['token']
related_payload['tz'] = self.tz
related_payload['hl'] = self.hl

# parse the returned json
req_json = self._get_data(
Expand Down Expand Up @@ -377,29 +378,29 @@ def categories(self):
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):
"""Gets historical hourly data for interest by chunking requests to 1 week at a time (which is what Google allows)"""

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

# the timeframe has to be in 1 week intervals or Google will reject it
# the timeframe has to be in 1 week intervals or Google will reject it
delta = timedelta(days=7)

df = pd.DataFrame()

date_iterator = start_date
date_iterator += delta
date_iterator += delta

while True:
if (date_iterator > end_date):
# has retrieved all of the data
# has retrieved all of the data
break

# format date to comply with API call
# format date to comply with API call
start_date_str = start_date.strftime('%Y-%m-%dT%H')
date_iterator_str = date_iterator.strftime('%Y-%m-%dT%H')
tf = start_date_str + ' ' + date_iterator_str

try:
try:
self.build_payload(keywords,cat, tf, geo, gprop)
week_df = self.interest_over_time()
df = df.append(week_df)
Expand All @@ -408,10 +409,10 @@ def get_historical_interest(self, keywords, year_start=2018, month_start=1, day_
pass

start_date += delta
date_iterator += delta
date_iterator += delta

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

return df
return df

0 comments on commit db7281d

Please sign in to comment.